Monday, 5 October 2015

Java Program that finds the Age of a person

/**
 *
 */

/**
 *
 *
 */
public class PersonAge {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Age a = new Age(10);
a.check();
}
}



public class Age {
private int a;
Age(int i){
a=i;
}
void check(){
if(a<=20)System.out.println("She/He is below 20");
else if((a>20) && (a<50))
System.out.println("She/He is between 20 and 50");
else
System.out.println("She/He is above 50");
}
}

Output:-
She/He is below 20

Java parameterised constructor example

/**
 *
 */

/**
 * @author U0123154
 *
 */
public class ParamConstructor_24 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ConstructorDemo i = new ConstructorDemo(10,20);

}

}



public class ConstructorDemo {
private int i;
private int k;
ConstructorDemo(int i,int k)
{
this.i=i;
this.k=k;
System.out.println("The \"i\" value is " +i);
System.out.println("The \"j\" value is " +k);
}
}

Output:-
The "i" value is 10
The "j" value is 20

For instance variables there will be separate copy for each object created in heap (Not like static objects)

/**
 *
 */

/**
 * @author U0123154
 *
 */
public class Inst {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo obj1 = new Demo();
Demo obj2 = new Demo();
obj1.i++;
obj2.i--;
obj1.display();
obj2.display();
}

}



public class Demo {
public int i=10;
void display(){
System.out.println("The value of i is  " +i);
}
}


Output:-
The value of i is  11
The value of i is  9

Two dimensional string array in java

/**
 *
 */
import java.io.*;
import java.util.*;
/**
 * @author U0123154
 *
 */
public class Twodimstringarry {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String array[][] = new String[3][4];
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
array[i][j] = br.readLine();

}

}
for(int m=0;m<3;m++){
for(int n=0;n<4;n++){
System.out.println("The name of the Person is " +array[m][n]);

}

}

int k=array.length;
System.out.println("The length is " +k);
}

}


Output:-
Sai
Nagendra
Bhavani
Prasad
Battipati
Chinna
Chaya
Krishna
Prameela
Bindhu
Ravi
Kumar
The name of the Person is Sai
The name of the Person is Nagendra
The name of the Person is Bhavani
The name of the Person is Prasad
The name of the Person is Battipati
The name of the Person is Chinna
The name of the Person is Chaya
The name of the Person is Krishna
The name of the Person is Prameela
The name of the Person is Bindhu
The name of the Person is Ravi
The name of the Person is Kumar
The length is 3


Two dimensional array in java

/**
 *
 */
import java.io.*;
import java.util.*;
/**
 * @author U0123154
 *
 */
public class Twodimarry{

/**
* @param args
*/
public static void main(String[] args)  throws IOException {
// TODO Auto-generated method stub
int arry[][] = new int [3][4];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
arry[i][j] = Integer.parseInt(br.readLine());
}
}
for (int k=0;k<3;k++){
for(int l=0;l<4;l++){
System.out.println("The value of the intefer is " +arry[k][l]);
}
}
int m = arry.length;
System.out.println("The array length is " +m);


}

}


Output:-
1
2
3
4
5
6
7
8
9
0
1
2
The value of the intefer is 1
The value of the intefer is 2
The value of the intefer is 3
The value of the intefer is 4
The value of the intefer is 5
The value of the intefer is 6
The value of the intefer is 7
The value of the intefer is 8
The value of the intefer is 9
The value of the intefer is 0
The value of the intefer is 1
The value of the intefer is 2
The array length is 3

First java OOPS program (Object creation for a class)

/**
 *
 */

/**
 * @author U0123154
 *
 */
public class ObjectsandClasses {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
First a = new First();
a.deom(10);
}
}


Class:-
//import java.lang.*;
public class First {
private int i;
int k;
void deom(int i)
{
this.i=i;
System.out.println("Variable is initilized and it's value is " +i);
}
}



Output:-
Variable is initilized and it's value is 10

String Buffer class is mutable

/**
 *
 */
import java.io.*;
/**
 * @author U0123154
 *
 */
public class Stringbuffer {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
System.out.println("The hashCode of the string buffer is " +sb.hashCode());
System.out.println("Enter a string");
String sb1 = br.readLine();
sb = sb.append(sb1);
System.out.println("The hashCode of the string buffer is " +sb.hashCode());
System.out.println("The value is  "+sb);
sb = sb.reverse();
System.out.println("The hashCode of the string buffer is " +sb.hashCode());
System.out.println("The value after reversing "+sb);
}
}



Output:-
The hashCode of the string buffer is 366712642
Enter a string
Apple is good for health
The hashCode of the string buffer is 366712642
The value is  Apple is good for health
The hashCode of the string buffer is 366712642
The value after reversing htlaeh rof doog si elppA