1.Class concept File name: Arithmetic.java

Λογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 4 χρόνια και 5 μήνες)

517 εμφανίσεις

1.

Class concept

File name:
Arithmetic
.java

public class Arithmetic

{

Arithmetic()

{

System.out.println("Object created to perform arithmetic operations ");

}

public static void main(String[] args) throws Exception

{

Arithmetic value = new
Arithmetic();

System.out.println("Addition of three int values : "+ value.add(10,2,3));

System.out.println("Multiplication of float values : "+ value.multiply(20.2f,3.4f));

System.out.println("Addition of two int values : "+ value.add(4,89));

Syste
m.out.println("Multiplication of int values : "+ value.multiply(20,3));

}

public int multiply(int a , int b)

{

return a*b;

}

public int add(int a , int b)

{

return a+b;

}

public float multiply(float a , float b)

{

return a*b;

}

public int

add(int a , int b ,int c)

{

return a+b+c;

}

}

2.

Inheritance and dynamic Polymorphism File name:

Triangle.java

class BaseArea

{

public int length,breadth;

BaseArea()

{

this.length=0;

this.breadth=0;

}

public int areaOf(int length,int breadt
h)

{

return length*breadth;

}

}

public class Triangle extends BaseArea

{

public static void main(String[] args) throws Exception

{

Triangle area = new Triangle();

BaseArea dimension = new BaseArea();

dimension.length=20;

dimension.breadth=20;

System.out.print("The area of triangle is: ");

System.out.println(area.areaOf(dimension.length,dimension.breadth));

}

public int areaOf(int base,int height)

{

return (int)(0.5*base*height);

}

}

3.

Abstract class File name:

CombinationOfNumber.jav
a

abstract class Combinations

{

abstract public int nCr(int n , int r) ;

public void display(int value)

{

System.out.println("
\
n Value is : "+ value);

}

}

public class CombinationOfNumber extends Combinations

{

public static void main(String []
args) throws Exception

{

CombinationOfNumber number = new CombinationOfNumber();

number.display(number.nCr(3,2));

}

public int nCr(int n , int r)

{

if(n==r || r==0)

return 1;

else

return nCr(n
-
1,r
-
1) + nCr(n
-
1,r);

}

}

4.

Multithreading
File name:

SampleThread.java

class MultiThreading extends Thread

{

public void run()

{

for(int i=0;i<10;i++)

{

System.out.println("child thread");

Thread.yield();

}

}

}

class MyRunnable implements Runnable

{

public void run()

{

for(int

i=0; i<=10 ;i++)

System.out.println("child thread");

}

}

public class ThreadDemo

{

public static void main(String a[])

{

MyRunnable r=new MyRunnable();

Thread t=new Thread(r);

t.start();

for(int i=0 ; i<=10 ; i++)

{

System.out.println("
main thread");

}

}

}

class SampleThread

{

public static void main (String args[])

{

MultiThreading t1 = new MultiThreading();

t1.start();

MyRunnable r=new MyRunnable();

Thread t2 = new Thread(r);

try

{

Thread.sleep(1000);

}

catch(
InterruptedException e)

{

System.out.println("I got interrupted");

}

for(int i=0;i<10;i++);

{

System.out.println("main thread");

}

}

}

5.

Thread synchronization File name:

ThreadSynchronisation.java

class Display

{

public synchronized void

show(String name)

{

for(int i=0;i<2;i++)

{

System.out.println("Thread");

try

{

Thread.sleep(2000);

}

catch(InterruptedException e){}

System.out.println(name);

}

}

}

class MyThread1 extends Thread

{

Display d;

String name;

MyThread1(Display d, String name)

{

this.d=d;

this.name=name;

}

public void run()

{

d.show(name);

}

}

public class ThreadSynchronisation

{

public static void main(String a[])throws Exception

{

Display d=new Display();

MyThread1 t1=new MyThread1(d,"java");

MyThread1 t2=new MyThread1(d,"Sun");

t1.start();

t2.start();

}

}

6.

StringTokenizer File name:

StringTokenizing.java

import java.util.*;

publ
ic class StringTokenizing

{

public static void main(String[] args)

{

StringTokenizer stringTokenizer = new StringTokenizer("You are tokenizing a string");

System.out.println("The total no. of tokens generated : " +
stringTokenizer.countTokens());

while(stringTokenizer.hasMoreTokens())

{

System.out.println(stringTokenizer.nextToken());

}

}

}

7.

LinkedList File name:

AddLinkedListElements.java

import java.util.*;

public class AddLinkedListElements

{

public static void main(String args[])
throws Exception

{

LinkedList ll = new LinkedList();

int sum = 0;

ll.add(new Integer(1));

ll.add(new Integer(2));

ll.add(new Integer(3));

ll.add(new Integer(4));

System.out.println("Original contents of ll: " + ll);

while(ll.size()!=0)

{

Integer temp = (Integer)(ll.removeFirst());

int a = temp.intValue();

sum = sum + a;

}

System.out.println("Sum is : " + sum);

}

}

8.

TreeSet File name:

TreeOrder.java

import java.util.*;

public class TreeOrder

{

public

static void main(String args[])

{

TreeSet treeset = new TreeSet();

ArrayList array_obj = new ArrayList();

Vector vector_obj = new Vector();

array_obj.add(new Integer(2));

array_obj.add(new Integer(1));

array_obj.add(new Integer(3));

vector
_obj.add(new Integer(5));

vector_obj.add(new Integer(6));

treeset.addAll(vector_obj);

treeset.addAll(array_obj);

System.out.println(array_obj + " " + vector_obj);

System.out.println(treeset);

}

}

9.

HashSet and Iterator class File name:

Hashset.j
ava

import java.util.*;

public class Hashset

{

public static void doHashset()

{

final int MAX = 10;

HashSet set_obj = new HashSet();

for (int i = 0; i < MAX; i++)

{

System.out.println("
-

Storing Integer(" + i +

")");

set_obj.add(new Integer(i));

}

Iterator i = set_obj.iterator();

while (i.hasNext())

{

System.out.println(i.next());

}

}

public static void main(String[] args)

{

doHashset();

}

}

10.

Filter and Buffered I/O streams File name:

NumberOfLines.java

import java.io.*;

public class NumberOfLines

{

public static void main(String[] args) throws IOException

{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in
));

System.out.println("
\
n enter a file name");

String filename = in.readLine();

File file = new File(filename);

if(file.exists())

{

FileReader fr = new FileReader(file);

LineNumberReader lr = new LineNumberReader(fr);

int count = 0;

while(lr.readLine()!= null)

count++;

System.out.println("
\
n number of lines : " + count);

}

else

System.out.println("
\
n File does not exist");

}

}

11.

Serialization File name:

Serialization.java

import java.io.*;

public class Serialization

{

public static void main(String args[])

{

try

{

Serialized obj_1 = new Serialized("Serialized");

System.out.println("object1: " + obj_1);

FileOutputStream fos = new FileOutputStream("serial");

ObjectOutputStream oos = new
ObjectOutputStream(fos);

oos.writeObject(obj_1);

oos.close();

}

catch(Exception e)

{

System.out.println("Exception during serialization: " + e);

}

try

{

Serialized obj_2;

FileInputStream fis = new FileInputStream("serial");

O
bjectInputStream ois = new ObjectInputStream(fis);

obj_2 = (Serialized)ois.readObject();

ois.close();

System.out.println("object2: " + obj_2);

}

catch(Exception e)

{

System.out.println("Exception during deserialization: " + e);

}

}

}

class Serialized implements Serializable

{

String s;

public Serialized(String s)

{

this.s = s;

}

public String toString()

{

return "s=" + s ;

}

}