Showing posts with label IT2305 JAVA PROGRAMMING. Show all posts
Showing posts with label IT2305 JAVA PROGRAMMING. Show all posts

Tuesday, 27 September 2011

MULTI THREADING


MULTI THREADING
Aim:
            To write a java program to implement multithreading.
  Algorithm:
Step1: Start.
Step2: Define a class A that extends Thread.
Step3: Call run(). Within this , use for loop to display the value of I from  to 5.
Step4: Define a class B that extends Thread.
Step5: Call run() to display the value of j from 1to 10.
Step6: Define class C that extends Thread.
Step7: Call run() to display the value of k from 1 to 15.
Step8: In the main() call A, B, C classes using start() with the help of an object.
Step9: Stop.
Program:
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("from Thread A:i="+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=10;j++)
{
System.out.println("from Thread B:j="+j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=15;k++)
{
System.out.println("from Thread C:k="+k);
}
System.out.println("Exit from C");
}
}
class ex1
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}


OUTPUT:

Z:\anitha>javac ex1.java
Z:\anitha>java ex1
from Thread A:i=1
from Thread B:j=1
from Thread C:k=1
from Thread A:i=2
from Thread B:j=2
from Thread C:k=2
from Thread A:i=3
from Thread B:j=3
from Thread C:k=3
from Thread A:i=4
from Thread B:j=4
from Thread C:k=4
from Thread A:i=5
from Thread B:j=5
from Thread C:k=5
Exit from A
from Thread B:j=6
from Thread C:k=6
from Thread B:j=7
from Thread C:k=7
from Thread B:j=8
from Thread C:k=8
from Thread B:j=9
from Thread C:k=9
from Thread B:j=10
from Thread C:k=10
Exit from B
from Thread C:k=11
from Thread C:k=12
from Thread C:k=13
from Thread C:k=14
from Thread C:k=15
Exit from C

JAVAI/O


              JAVAI/O
Aim:
            To write a java program to implement the concept of I/O.
Algorithm:
Step1: Start.
Step2: Import the classes util and io.
Step3: Define a class inventory which define the void main().
Step4: Get the item code, no of  items and item cost from the user.
Step5: Using the DataOutputStream class write the item details inside the file invent.dat.
Step6: Using the DataInputStream class retrieve the stored details from the file and calculate the total cost.
Step7: Display the item code, no of  items, cost and total cost in the console.
Step8: Stop.
Program:
import java.util.*;
import java.io.*;
class inventory
{
static DataInputStream din=new DataInputStream(System.in);
static StringTokenizer st;
public static void main(String args[]) throws IOException
{
DataOutputStream dos= new DataOutputStream(new FileOutputStream("invent.dat"));
System.out.println("enter code number:");
st=new StringTokenizer(din.readLine());
int code= Integer.parseInt(st.nextToken());

System.out.println("enter number of items:");
st=new StringTokenizer(din.readLine());
int items= Integer.parseInt(st.nextToken());

System.out.println("enter cost:");
st=new StringTokenizer(din.readLine());
double cost= new Double(st.nextToken()).doubleValue();
dos.writeInt(code);
dos.writeInt(items);
dos.writeDouble(cost);
dos.close();

DataInputStream dis = new DataInputStream(new FileInputStream("invent.dat"));
int codeNumber = dis.readInt();
int totalItems = dis.readInt();
double itemCost = dis.readDouble();
double totalCost = totalItems * itemCost;
dis.close();
System.out.println();
System.out.println("Code Number:"+ codeNumber);
System.out.println("Item Cost :"+ itemCost);
System.out.println("totalitems :"+ totalItems);
System.out.println("totalcost :"+totalCost);
}
}

Output:
Z:\anitha>javac -d inventory.java
Z:\anitha>java inventory
enter code number:
101
enter number of items:
10
enter cost:
400
Code Number:101
Item Cost :400.0
totalitems :10
totalcost :4000.0

EXCEPTION HANDLING


Exno:                                            EXCEPTION HANDLING
Aim:
            To write a java program to implement the concept of exception handling.
Algorithm:
Step1: Start.
Step2: Create a class MyException which extends the Exception class.
Step3: Define a user defined Exception.
Step4: In void main(), peform an arithmetic operation.
Step5: In try block check the result is less than 0.01.
Step6: In catch block catch the exception and call the user defined exception.
Step7: In finally block display the message.
Step8: Stop.
Program:
import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class TestMyException
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float) x/(float) y;
if(z<0.01)
{
throw new MyException("number is too small");
}
}
catch (MyException e)
{
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("i am always here");
}
}
}

Output:
Z:\anitha>javac TestMyException.java
Z:\anitha>java TestMyException
Caught my exception
number is too small
i am always here

MULTIPLE INHERITANCE


Exno:                                       MULTIPLE INHERITANCE

Aim:
To write a java program to implement the concept of multiple inheritance.
Algorithm:
Step1: Start.
Step2: Define a class stu to get the name and roll no and display the same.
Step3: Define a class test that acts as a sub-class of stu. Get the marks m1, m2 and display the same.
Step4: Define an interface sports that carries the spors weightage.
Step5: Define class  result that extends class test and implements interface sports.
Step6: Calculate the total marks in class result.
Step7: Define class final pgm and void main() as public static.
Step8: Declare an object ‘s’ to class result and invoke getdata(), getmark() and display() to print the output.
Step9: Stop.
Program:
class stu
{
String name;
int rollno;
void getdata(String s,int r)
{
name=s;
rollno=r;
}
void putdata()
{
System.out.println("\nName="+name);
System.out.println("\nRoll No="+rollno);
}
}
class test extends stu
{
int m1,m2;
void getmarks(int x,int y)
{
m1=x;
m2=y;
}
void putmarks()
{
System.out.println("\nMark1="+m1);
System.out.println("\nMark2="+m2);
}
}
interface sports
{
final int sp=6;
abstract void putsp();
}
class result extends test implements sports
{
int tot;
public void putsp()
{
System.out.println("\nSports wt="+sp);
}
void display()
{
tot=m1+m2+sp;
putdata();
putmarks();
putsp();
System.out.println("\nTotal="+tot);
}
}
class ex2
{
public static void main(String args[])
{
result s=new result();
s.getdata("Anitha",101);
s.getmarks(98,99);
s.display();
}
}


OUTPUT:

Z:\anitha>javac ex2.java
Z:\anitha>java ex2
Name=Anitha
Roll No=101
Mark1=98
Mark2=99
Sports wt=6
Total=197

SINGLE INHERITANCE


SINGLE INHERITANCE
Aim:
            To write a java program to implement the concept of single inheritance.
Algorithm:
Step1: Start.
Step2: Create a class room with members length, breadth and area().
Step3: Create a class bedroom which inherits the member length and breadth and has member height and volume().
Step4: Create  a class inhertest in which the void main() is declared as public static.
Step5: Define the objecs of class bedroom and display the result of area and volume.
Step6: Stop.
Program:
class room
{
int length;
int breadth;
room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
class bedroom extends room
{
int heigth;
bedroom(int x,int y,int z)
{
super(x,y);
heigth=z;
}
int volume()
{
return(length*breadth*heigth);
}
}
class inhertest
{
public static void main(String args[])
{
bedroom room1=new bedroom(10,10,10);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("Area="+area1);
System.out.println("Volume="+volume1);
}
}


OUTPUT:
Z:\anitha>javac inhertest.java
Z:\anitha>java inhertest
Area=100
Volume=1000

INTERFACES


Exno:                                                   INTERFACES
Aim:
            To write a java program to implement the concept of  interfaces.
Algorithm:
Step1: Start.
Step2: Create a interface calculate and declare he method area().
Step3: Create the class rectangle and triangle which implements the interface calculate.
Step4: within these classes define the method area().
Step5: Create the objects for the rectangle and triangle and find the area.
Step6: Print the area of rectangle and triangle.
Step7: Stop.
Program:
import java.io.*;
interface calculat
{
public double area(double len,double hgt);
}
class rectangle implements calculat
{
public double area(double len,double hgt)
{
return len*hgt;
}
}
class triangle implements calculat
{
public double area(double len,double hgt)
{
return 0.5*len*hgt;
}
}
public class calculate
{
public static void main(String args[])
{
rectangle r=new rectangle();
triangle t=new triangle();
System.out.println(r.area(40,50));
System.out.println(t.area(20,30));
}
}

Output:
Z:\anitha>javac calculate.java
Z:\anitha>java calculate
2000.0
300.0

DESIGNING PACKAGES

Aim:
            To write the java program to implement the concept of packages.
Algorithm:
Step1: Start.
Step2: Declare the packages p1, p2, p3, p4 implementing the classes float point, relational operators, increment operator, decrement operator as public.
Step3: Import the 4 packages in a separately in the main().
Step4: Create objects for the classes of the different packages and display the result by calling the display function of respective classes.
Step5: create a sub directory under the min directory and name the folder as package name.
Step6: Stop.
Program:
package p1;
public class floatpoint1
{
public void displayA()
{
float a=20.5F,b=6.4F;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("a+b="+(a+b));
System.out.println("a-b="+(a-b));
System.out.println("a*b="+(a*b));
System.out.println("a/b="+(a/b));
System.out.println("a%b="+(a%b));
}
}
package p2;
public class relationaloperators
{
public void displayB()
{
float a=15.0F,b=20.75F,c=15.0F;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("a<b="+(a<b));
System.out.println("a>b="+(a>b));
System.out.println("a==c"+(a==c));
System.out.println("a<=c"+(a<=c));
System.out.println("a>=b"+(a>=b));
System.out.println("b!=c"+(b!=c));
System.out.println("b==a+c"+(b==a+c));
}
}
package p3;
public class incrementoperator
{
public void displayC()
{
float a=10,b=20;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("++a="+ ++a);
System.out.println("b++="+ b++);
System.out.println("a="+a);
System.out.println("b="+b);
}
}
package p4;
public class decrementoperator
{
public void displayD()
{
float a=10,b=20;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("--a="+ --a);
System.out.println("b--="+b--);
System.out.println("a="+a);
System.out.println("a="+a);
}
}


Output:
Z:\anitha\package1>javac operators
Z:\anitha\package1>java operators
a=20.5
b=6.4
a+b=26.9
a-b=14.1
a*b=131.2
a/b=3.203125
a%b=1.2999997
a=15.0
b=20.75
c=15.0
a<b=true
a>b=false
a==ctrue
a<=ctrue
a>=bfalse
b!=ctrue
b==a+cfalse
a=10.0
b=20.0
++a=11.0
b++=20.0
a=11.0
b=21.0
a=10.0
b=20.0
--a=9.0
b--=20.0
a=9.0
a=9.0