// Find out why it throws exception at list.remove(d);
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
ArrayList<Comparable> list=new ArrayList<Comparable>();
//ArrayList<Integer> list=new ArrayList<Integer>();
int a=8;
String b="Anand";
boolean c=true;
list.add(a);
list.add(b);
list.add(c);
int size=list.size();
System.out.println("The size of list is : "+size);
int indexc=list.indexOf(c);
System.out.println("The Index of c is : "+indexc);
char d='A';
list.add(2, d);
System.out.println("The size of list is : "+size);
indexc=list.indexOf(c);
System.out.println("The Index of c is : "+indexc);
System.out.println("The value of d is : "+list.get(list.indexOf(d)));
list.remove(d);
//System.out.println("The Index of c is : "+list.get(list.indexOf(d)));
Iterator<Comparable> itr=list.iterator();
while(itr.hasNext())
{
System.out.println("The elements of list are: "+itr.next());
}
}
}
Sunday, February 26, 2012
Saturday, February 25, 2012
Static Variable Example
public class StaticExample {
public static void main(String [] args)
{
StaticMethod sm =new StaticMethod();
StaticMethod sm1 =new StaticMethod();
sm.getValue(25,25);
sm1.getValue(30,30);
int val1=sm.var1;
int val2=sm1.var1;
int val3=sm.var2;
int val4=sm1.var2;
System.out.println("The value of 1st static instance: "+val1+"" +
" The value of 2nd static instance: "+val2);
System.out.println("The value of 1st non static instance: "+val3+"" +
" The value of 2nd non static instance: "+val4);
}
}
class StaticMethod {
static int var1;
int var2;
public void getValue(int val1, int val2)
{
var1=val1;
var2=val2;
//return a;
}
}
PrimeNumbers with Scanner
import java.util.*;
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of prime numbers you want ");
n = in.nextInt();
if ( n >= 1 )
{
System.out.println("First "+n+" prime numbers are :-");
System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of prime numbers you want ");
n = in.nextInt();
if ( n >= 1 )
{
System.out.println("First "+n+" prime numbers are :-");
System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
Print output without semicolon
interface abc
{
int a=10;
}
public class SemiColon implements abc{
public static void main(String [] args ){
if (System.out.printf("Hello World", null).toString().length() > 20){ }
//Example of ternary operator
int a=2;
int b=3;
int c=4;
System.out.println((a>b?a:b)>c?(a>b?a:b):c);
}
}
{
int a=10;
}
public class SemiColon implements abc{
public static void main(String [] args ){
if (System.out.printf("Hello World", null).toString().length() > 20){ }
//Example of ternary operator
int a=2;
int b=3;
int c=4;
System.out.println((a>b?a:b)>c?(a>b?a:b):c);
}
}
OverrideStatic
//Static method cannot be overridden. Look into the following code:
class ParentClass {
public void method1(){
System.out.println("Parent Method1");
}
public static void method2(){
System.out.println("Parent Method2");
}
}
class ChildClass extends ParentClass{
public void method1(){
System.out.println("Child Method1");
}
public static void method2(){
System.out.println("Child Method2");
}
}
public class OverrideStatic {
public static void main(String args[]){
ParentClass parent;
ChildClass child = new ChildClass();
parent = child;
parent.method1();
parent.method2();
}
}
class ParentClass {
public void method1(){
System.out.println("Parent Method1");
}
public static void method2(){
System.out.println("Parent Method2");
}
}
class ChildClass extends ParentClass{
public void method1(){
System.out.println("Child Method1");
}
public static void method2(){
System.out.println("Child Method2");
}
}
public class OverrideStatic {
public static void main(String args[]){
ParentClass parent;
ChildClass child = new ChildClass();
parent = child;
parent.method1();
parent.method2();
}
}
Why-is-the-tostring-method-being-called-when-i-print-an-object
//Example why-is-the-tostring-method-being-called-when-i-print-an-object
//Because
public class ABC {
public static void main(String[] args) {
Coin1 q = new Quarter1();
System.out.println("I am calling get value on coin " + q.getABC());
System.out.println(q);
}
}
class Money1 {
private int value;
public Money1(int v) {
value=v;
}
public int getValue()
{
return value;
}
protected int myValue() {
return value;
}
public String toString()
{
return "Anand";
}
}
class Coin1 extends Money1 {
public Coin1(int value) {
super(value);
System.out.println("I am a coin, my value is " + getValue());
}
public int getABC() {
return 23;
}
}
class Quarter1 extends Coin1 {
public Quarter1 () {
super(25);
}
public int getValue() {
return myValue();
}
public String toString() {
return "A Quarter is "+getValue();
}
}
//Because
PrintStream.println has an overload that takes an Object, and then calls //itstoString methodpublic class ABC {
public static void main(String[] args) {
Coin1 q = new Quarter1();
System.out.println("I am calling get value on coin " + q.getABC());
System.out.println(q);
}
}
class Money1 {
private int value;
public Money1(int v) {
value=v;
}
public int getValue()
{
return value;
}
protected int myValue() {
return value;
}
public String toString()
{
return "Anand";
}
}
class Coin1 extends Money1 {
public Coin1(int value) {
super(value);
System.out.println("I am a coin, my value is " + getValue());
}
public int getABC() {
return 23;
}
}
class Quarter1 extends Coin1 {
public Quarter1 () {
super(25);
}
public int getValue() {
return myValue();
}
public String toString() {
return "A Quarter is "+getValue();
}
}
Constructor Calling
public class Test {
public static void main(String args[])
{
//Constructor related code block
System.out.println(args.length);
Parent a=new Parent();
System.out.println("1");
Child b=new Child();
System.out.println("2");
Parent c=new Child();
System.out.println("3");
//Child d=(Child)new Parent();
Parent d=(Parent) new Child();
System.out.println("4");
a.Method(34);
System.out.println("5");
b.Method(56);
System.out.println("6");
c.Method(102);
System.out.println("7");
d.Method(123);
}
}
public class Parent {
public Parent()
{
this("Anand");
System.out.println("Parent Constructor");
}
public Parent(String args)
{
System.out.println("Parent Constructor with args");
}
public void Method(int args)
{
System.out.println("Parent");
System.out.println(args);
}
}
public static void main(String args[])
{
//Constructor related code block
System.out.println(args.length);
Parent a=new Parent();
System.out.println("1");
Child b=new Child();
System.out.println("2");
Parent c=new Child();
System.out.println("3");
//Child d=(Child)new Parent();
Parent d=(Parent) new Child();
System.out.println("4");
a.Method(34);
System.out.println("5");
b.Method(56);
System.out.println("6");
c.Method(102);
System.out.println("7");
d.Method(123);
}
}
public class Parent {
public Parent()
{
this("Anand");
System.out.println("Parent Constructor");
}
public Parent(String args)
{
System.out.println("Parent Constructor with args");
}
public void Method(int args)
{
System.out.println("Parent");
System.out.println(args);
}
}
public class Child extends Parent{
public Child()
{
this("Anand");
//super();
//this.thisCall();
//Child("Annad");
System.out.println("Child Constructor");
}
public Child(String args)
{
System.out.println("Child Constructor calling with args cons");
}
public void thisCall()
{
System.out.println("Child Constructor calling this");
}
public void Method(int args)
{
System.out.println("Child");
System.out.println(args);
}
}
SerializationDemo
import java.io.*;
public class SerilizationExm {
public static void main(String args[])
{
int i=10;
int j=0;
long start = System.currentTimeMillis ();
//Serializing object
MyClass2 a= new MyClass2();
try {
MyClass1 object1=new MyClass1("Anand",27,1984,a);
System.out.println("Object1: "+object1);
FileOutputStream fos=new FileOutputStream("Stream");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(object1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
MyClass1 object2;
//System.exit (0);
//Runtime.getRuntime().exit(0);
FileInputStream fis=new FileInputStream("Stream1");
ObjectInputStream ois=new ObjectInputStream(fis);
object2=(MyClass1)ois.readObject();
System.out.println("Object2: "+object2);
//System.exit (0);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("File name mentioned does not exists");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
System.out.println("This is finally Method"+i/j);
}
catch(ArithmeticException e)
{
System.out.println("Exception in finally");
//e.printStackTrace();
}
}
long end = System.currentTimeMillis ();
System.out.println ("Time taken for execution is " + (end - start)+" In milli seconds");
}
}
class MyClass1 implements Serializable
{
String s;
static int c=2;
String d;
int a;
long b;
public MyClass1(String ab,int bc,long ac,MyClass2 abc )
{
s=ab;
a=bc;
b=ac;
d=abc.aMethod();
}
public String toString()
{
//d=(new MyClass2()).aMethod();
return "s="+s+";a="+a+";b="+b+";c="+c+";d="+d;
}
}
class MyClass2
{
public String aMethod()
{
return "Prakash";
}
}
public class SerilizationExm {
public static void main(String args[])
{
int i=10;
int j=0;
long start = System.currentTimeMillis ();
//Serializing object
MyClass2 a= new MyClass2();
try {
MyClass1 object1=new MyClass1("Anand",27,1984,a);
System.out.println("Object1: "+object1);
FileOutputStream fos=new FileOutputStream("Stream");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(object1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
MyClass1 object2;
//System.exit (0);
//Runtime.getRuntime().exit(0);
FileInputStream fis=new FileInputStream("Stream1");
ObjectInputStream ois=new ObjectInputStream(fis);
object2=(MyClass1)ois.readObject();
System.out.println("Object2: "+object2);
//System.exit (0);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("File name mentioned does not exists");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
System.out.println("This is finally Method"+i/j);
}
catch(ArithmeticException e)
{
System.out.println("Exception in finally");
//e.printStackTrace();
}
}
long end = System.currentTimeMillis ();
System.out.println ("Time taken for execution is " + (end - start)+" In milli seconds");
}
}
class MyClass1 implements Serializable
{
String s;
static int c=2;
String d;
int a;
long b;
public MyClass1(String ab,int bc,long ac,MyClass2 abc )
{
s=ab;
a=bc;
b=ac;
d=abc.aMethod();
}
public String toString()
{
//d=(new MyClass2()).aMethod();
return "s="+s+";a="+a+";b="+b+";c="+c+";d="+d;
}
}
class MyClass2
{
public String aMethod()
{
return "Prakash";
}
}
Friday, February 24, 2012
ArrayinArray
public class ArraySort {
public static void main(String []args)
{
int length = 9999;
int search[]={0,0,0,0};
int pattern[]= {1,3,2,4};
int data[]= {62,58,3,15,6,5,60,1,7,8,22,3,9,4,1,2,9,7,35,4,2,8,80,6,5,3,73,6,1};
for ( int i=0; i<data.length; i++ )
{
if ( data[i] == pattern[0] || data[i] == pattern[1] || data[i] == pattern[2] || data[i] == pattern[3] )
{
for(int k=0; k<4; k++)
{
search[k]=0;
}
for(int j=i; j<data.length; j++)
{
if ( data[j] == pattern[0] )
search[0]=1;
if ( data[j] == pattern[1] )
search[1]=1;
if ( data[j] == pattern[2] )
search[2]=1;
if ( data[j] == pattern[3] )
search[3]=1;
if (search[0] == 1 && search[1] == 1 && search[2] == 1 && search[3] == 1)
{
if (length > j-i+1)
{
length=j-i+1;
break;
}
}
}
}
}
System.out.println ("The Smallest length for the pattern is :"+length);
}
}
Subscribe to:
Comments (Atom)