Monday, June 5, 2017

Object Oriented Principles

1)Inheritance will only work when the child class's constructor is accessible(public)
When Inheritance is applied all the methods in Parent class's are copied into child class

Class A
{
Members
}

Class B:A
{
Consuming members of A
}

A is the Super class or Parent Class

2)When Parent and Child both have constructors

Parent Class constructor is called first

3)Parent Class can not access Child class members

4)We can initialize Parent Class variable by using a child class instance
Class A
{
A a;
B b=new B();
a=b; //b is an instance variable and a is class variable
}

Monday, May 22, 2017

String Methods

1)charAt 2)str.toCharArray() 3) s.indexOf(c) 4)s.lastIndexOf(c)

Sunday, May 21, 2017

Ascii

Ascii has 255 Characters

1) Any Ascii character has its integer value from 0-255

Java Arrays

Type[] name = new Type[Size];
Example : boolean[] charSet = new boolean[256];
Note : 
1)Default value of Boolean array element is False
2) Default value of Int array element is 0

Thursday, May 11, 2017

Java Random numbers

Use
Import java.util.Random;


1)To fetch a random number from 0 to that number
Random().nextInt(int bound) generates a random integer from 0 (inclusive) to bound (exclusive).

2)To fetch a random number in a Range
private static int getRandomNumberInRange(int min, int max) {
 Random r = new Random();
 return r.nextInt((max - min) + 1) + min;
}
3)To fetch Random objects from an array
String []guess={"Red","Blue","Green"};
char[] randomguess=guess[random.nextInt(guess.length)].toCharArray();


Source : https://www.mkyong.com/java/java-generate-random-integers-in-a-range/


Monday, February 13, 2017

Java interesting Rules

1)  Java file can only have one Public with same Name of the file.
Can have any number of classes with different names...

2) A file can have any number of classes and any number of mains in each class

3)

Sunday, January 15, 2017

How to Read files in java

1.Using Scanner

Scanner sc= new Scanner(new File("filename.txt") )

Methods inside scanner

1)hasNext(),next(),nextLine(),useDelimiter()
hasNext()--returns true until there is a token

1) By default, a scanner uses white space (can be space,tab,or new line) to separate tokens

  Delimiter can also be changed by--- sc.useDelimiter(",\\s*");

Useful Stackoverflow links:
http://stackoverflow.com/questions/41668412/java-scanner-is-not-working-while-reading-a-file


2.Using BufferReader

br = new BufferedReader(new FileReader(FILENAME));
while ((strLine = br.readLine()) != null)   {
String[] arr = strLine.split("\\s+"); //Breaking each line into array of words
Item item = new Item(arr[0],arr[1],Integer.parseInt(arr[2]));
list.add(item);
}