Use of flush and close Methods of BufferedWriter Class in Java

Maruthi Krishna
Updated on 01-Aug-2019 13:17:24

2K+ Views

The BufferedWriter class of Java is used to write stream of characters to the specified destination (character-output stream). It initially stores all the characters in a buffer and pushes the contents of the buffer to the destination, making the writing of characters, arrays and Strings efficient.You can specify the required size of the buffer at the time of instantiating this class.The flush() methodWhile you are trying to write data to a Stream using the BufferedWriter object, after invoking the write() method the data will be buffered initially, nothing will be printed.The flush() method is used to push the contents of ... Read More

Java Primitive Types Range and Behavior

Maruthi Krishna
Updated on 01-Aug-2019 12:31:17

382 Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double.Java strictly specifies range and behaviors of all the primitive datatypes. Making the users choose the required datatypes based on the application thus reducing the unused occupancy of memory.For example, if you need to store an integer constant of single digit using integer would be a waste of memory instead, you can use byte type since 8 bits would be necessary to store it.ExampleFollowing Java example lists out the ranges of the primitive datatypes.public class ... Read More

Default Values of Numeric and Non-Numeric Primitive Data Types in Java

Maruthi Krishna
Updated on 01-Aug-2019 12:21:52

545 Views

When you create instance variables in Java you need to initialize them, else the compiler will initialize on your behalf with default values which are −byte: 0short: 0int: 0long: 0float: 0.0double: 0.0boolean: falsestring: nullExampleIn the following Java program prints the default values of the numeric and non-numeric primitive variables in java.public class DefaultValues {    byte byteVariable;    short shortVariable;    int intVariable;    long longVaraible;    float floatVariable;    double doubleVariable;    boolean boolVariable;    String stringVariable;    public static void main(String args[]){       DefaultValues obj = new DefaultValues();       System.out.println("Default values of numeric variables ... Read More

Call Superclass Constructor from a Constructor in Java

Maruthi Krishna
Updated on 01-Aug-2019 12:09:16

6K+ Views

Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.ExampleIn the following example we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo().class SuperClass{    public void demo() {       System.out.println("demo method");    } } public class SubClass extends SuperClass {    public static void main(String args[]) {       SubClass obj = new ... Read More

Ensure Child Class Overrides Super Class Method in Java

Maruthi Krishna
Updated on 01-Aug-2019 11:55:07

707 Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation (body) to it. If a class contains at least one abstract method, you must declare it abstract.In other words, if you extend an abstract class it is mandatory to implement (override) all the abstract methods in it or, declare it abstract else a compile time error will be generated for each abstract method (that you ... Read More

Is it Mandatory to Override Default Methods of an Interface in Java?

Maruthi Krishna
Updated on 01-Aug-2019 11:12:37

3K+ Views

The default methods are introduced in an interface since Java8. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{    public static int num = 100;    public default void display() {       System.out.println("display method of MyInterface");    } } public class InterfaceExample implements MyInterface{    public static void main(String ... Read More

Binary Array After M Range Toggle Operations

Arnab Chakraborty
Updated on 01-Aug-2019 08:31:49

252 Views

Here we will see one problem. We have one binary array. It has n elements. Each element will be either 0 or 1. Initially, all elements are 0. Now we will provide M commands. Each command will contain start and end indices. So command(a, b) is indicating that the command will be applied from element at position a to element at position b. The command will toggle the values. So it will toggle from ath index to bth index. This problem is simple. check the algorithm to get the concept.AlgorithmtoggleCommand(arr, a, b)Begin    for each element e from index a ... Read More

Biggest Square Inscribed in an Equilateral Triangle

Arnab Chakraborty
Updated on 01-Aug-2019 08:29:04

287 Views

Here we will see the area of the biggest square that can be inscribed in an equilateral triangle. The side of the triangle is ‘a’ and the side of the square is x.The side of the triangle ‘a’ is −So x is −Example#include #include using namespace std; float areaSquare(float a) { //a is side of triangle    if (a < 0 ) //if a is negative, then this is invalid       return -1;    float area = a / (1 + 2/sqrt(3));    return area; } int main() {    float a = 7;    cout

Biggest Reuleaux Triangle Within a Square

Arnab Chakraborty
Updated on 01-Aug-2019 08:26:17

131 Views

Here we will see the area of biggest Reuleaux triangle inscribed within a square. The side of the square is ‘a’. And the height of the Reuleaux triangle is h.The height of the Reuleaux triangle is same as a. So a = h. So the area of Reuleaux triangle is −Example#include #include using namespace std; float areaReuleaux(float a) { //side of square is a    if (a < 0) //if a is negative it is invalid       return -1;    float area = ((3.1415 - sqrt(3)) * (a) * (a))/2;    return area; } int main() {    float side = 8;    cout

Biggest Reuleaux Triangle Within a Square Inscribed in a Circle

Arnab Chakraborty
Updated on 01-Aug-2019 08:18:26

138 Views

Here we will see the area of biggest Reuleaux triangle inscribed within a square, That square is inscribed inside one circle. The side of the square is ‘a’. The radius of the circle is ‘r’. As we know that the diagonal of the square is the diameter of the circle. So −2𝑟 = 𝑎√2 𝑎 = 𝑟√2And the height of the Reuleaux triangle is h.The height of the Reuleaux triangle is same as a. So a = h. So the area of Reuleaux triangle is −Example#include #include using namespace std; float areaReuleaux(float r) { //radius of ciecle is ... Read More

Advertisements