0% found this document useful (0 votes)
17 views

Java Lab Manual-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Lab Manual-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

Page No:

Reg No:

EX NO-1 TO GENERATE RATIONAL NUMBERS

/*This class for Rational numbers


@Author ONLINE
@Version 1.0
*/
import java.io.*;
import java.util.*;
class Rational
{
int n,d;
/* This constructor to get Numerator and Denominator value*/
Rational(int x,int y)
{
n=x;
d=y;
System.out.println("NUMERATOR="+n);
System.out.println("DENOMINATOR="+d);
System.out.println("BEFORE SIMPLIFICATION="+n+"/"+d);
}
/* This method finds the reduce form of Numerator and Denominator value*/

public void reducedform()


{
if(n<d)
{
for(int i=2;i<=n;i++)
{
while((n%i)==0 && (d%i)==0)
{
n=n/i;
d=d/i;
}
}
}
else
{
for(int i=2;i<=d;i++)
{
while((n%i)==0 && (d%i)==0)
{
n=n/i;
d=d/i;
}
Page No:
Reg No:

}
}
}

void display()
{
System.out.println("Reduced form="+n+"/"+d);
}
public static void main(String args[])
{
Scanner S=new Scanner(System.in);
System.out.println("***********ENTER NUMERATOR
VALUE*****************");
int A=S.nextInt();
System.out.println("***********ENTER NUMERATOR
VALUE*****************");
int B=S.nextInt();
Rational R=new Rational(A,B);
R.reducedform();
R.display();
}
}

/***************OUTPUT***************************
D:\ONLINE>javac Rational.java

D:\ONLINE>java Rational
***********ENTER NUMERATOR VALUE*****************
50
***********ENTER NUMERATOR VALUE*****************
100
NUMERATOR=50
DENOMINATOR=100
BEFORE SIMPLIFICATION=50/100
Reduced form=1/2

D:\ONLINE>java Rational
***********ENTER NUMERATOR VALUE*****************
60
***********ENTER NUMERATOR VALUE*****************
20
NUMERATOR=60
DENOMINATOR=20
BEFORE SIMPLIFICATION=60/20
Page No:
Reg No:

Reduced form=3/1

*************************************************/

EX NO-2 TO DEVELOP USER DEFINED PACKAGE-


DATE CLASS

/****************************PACKAGE FILE***************************/
package Date;
import java.util.GregorianCalendar;

public class CurrentDate


{
public void MDY()
{
GregorianCalendar cal = new GregorianCalendar();
int month = cal.get(GregorianCalendar.MONTH);
int year = cal.get(GregorianCalendar.YEAR);
int day = cal.get(GregorianCalendar.DAY_OF_MONTH);
System.out.println("Current date : " + day + "/" + (month + 1) + "/" + year);
}
public void HMS()
{
GregorianCalendar cal = new GregorianCalendar();
String am_pm;
int hour = cal.get(GregorianCalendar.HOUR);
int minute = cal.get(GregorianCalendar.MINUTE);
int second = cal.get(GregorianCalendar.SECOND);
if(cal.get(GregorianCalendar.AM_PM) == 0)
am_pm = "PM";
else
am_pm = "AM";
System.out.println("Current Time : " + hour + ":" + minute + ":" + second + " " +
am_pm);

}
}
/****APPLICATION PROGRAM(IMPORTED USER DEFINED PACKAGE)*******/
import Date.*;
class DISPLAY
{
public static void main(String args[])
{
Page No:
Reg No:

CurrentDate D=new CurrentDate();


System.out.println("**************");
D.MDY();
System.out.println("**************");
D.HMS();
}
}

/******************OUTPUT*****************

D:\ONLINE>CD DATE

D:\ONLINE\Date>javac CurrentDate.java

D:\ONLINE\Date>cd..

D:\ONLINE>javac DISPLAY.java

D:\ONLINE>java DISPLAY
**************
Current date : 23/9/2010
**************
Current Time : 10:10:27 AM

D:\ONLINE>

****************************************/
Page No:
Reg No:

EX NO-3 TO DEVELOP LISP-LIKE LIST\

import java.util.*;
class Lisp
{
public int car(List l)
{
Object ob=l.get(0);
String st=ob.toString();
return Integer.parseInt(st);
}
public List cdr(List l)
{
Object ob=l.remove(0);
Object obj[]=l.toArray();
List list=Arrays.asList(obj);

return list;
}

public static void main(String[] args)


{
List<Integer> l=new ArrayList<Integer>();
l.add(3);
l.add(0);
l.add(2);
l.add(5);
Lisp L=new Lisp();
int val=L.car(l);
System.out.println(val);
List list=L.cdr(l);
System.out.print(list);
}
}
/***************OUTPUT*****************
Page No:
Reg No:

D:\ONLINE>javac Lisp.java

D:\ONLINE>java Lisp
3
[0, 2, 5]
D:\ONLINE>
***************************************/

/****************EX NO-4(ADT STACK)***************************/


import java.util.*;
import java.lang.*;
interface stackDemo
{
public void push(int v);
public void pop();
}

class linkedlist implements stackDemo


{
LinkedList list = new LinkedList();
public void push(int v)
{
list.addFirst(v);
System.out.println(list);
}

public void pop()


{
list.removeFirst();
System.out.println(list);
}

class arraylist implements stackDemo


{
static int n=0;
ArrayList list = new ArrayList();
public void push(int v)
{
Page No:
Reg No:

list.add(v);
n++;
System.out.println(list);
}
public void pop()
{
list.remove(--n);
System.out.println(list);
}
}

class stackall
{
public static void main(String args[])
{
int ch,lk,ak;
System.out.println("STACK IMPLEMENTATION USING ARRAY AND LINKED
LIST");
System.out.println("ENTER YOUR CHOICE");
System.out.println("1.STACK IMPLEMENTATION USING LINKED LIST");
System.out.println("2.STACK IMPLEMENTATION USING ARRAY");
Scanner S=new Scanner(System.in);
ch=S.nextInt();
switch(ch)
{

case 1:
System.out.println("1.STACK IMPLEMENTATION USING LINKED LIST");
linkedlist L= new linkedlist();
Scanner T=new Scanner(System.in);
do
{
System.out.println("ENTER YOUR CHOICE 1.PUSH 2.POP");
lk=T.nextInt();

switch(lk)
{
case 1:
System.out.print("Enter elements : ");
int n = T.nextInt();
L.push(n);
break;
Page No:
Reg No:

case 2:
try
{
L.pop();
System.out.println(L);
}
catch(NoSuchElementException k)
{
System.out.println("EMPTY STACK:"+k);
}
break;
default:
System.out.println("INVALID CHOICE");
break;
}
}while(lk<=2);

case 2:
System.out.println("1.STACK IMPLEMENTATION USING Array");
arraylist A= new arraylist();
Scanner x=new Scanner(System.in);
do
{
System.out.println("ENTER YOUR CHOICE 1.PUSH 2.POP");

ak=x.nextInt();

switch(ak)
{
case 1:
System.out.print("Enter elements : ");
int u = x.nextInt();
A.push(u);
break;

case 2:
try
{
A.pop();
System.out.println(A);
}
catch(ArrayIndexOutOfBoundsException km)
{
System.out.println("EMPTY STACK:"+km);
}
break;
Page No:
Reg No:

default:
System.out.println("INVALID CHOICE");
break;
}
}while(ak<=2);
default:
System.out.println("INVALID CHOICE");
break;

}
}
}

/***************OUTPUT*****************
Q:\ONLINE>javac stackall.java
Q:\ONLINE>java stackall
STACK IMPLEMENTATION USING ARRAY AND LINKED LIST
ENTER YOUR CHOICE
1.STACK IMPLEMENTATION USING LINKED LIST
2.STACK IMPLEMENTATION USING ARRAY
1
1.STACK IMPLEMENTATION USING LINKED LIST
ENTER YOUR CHOICE 1.PUSH 2.POP
1
Enter elements : 25
[25]
ENTER YOUR CHOICE 1.PUSH 2.POP
1
Enter elements : 50
[50, 25]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
[25]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
[]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
EMPTY STACK:java.util.NoSuchElementException
ENTER YOUR CHOICE 1.PUSH 2.POP
6
INVALID CHOICE
1.STACK IMPLEMENTATION USING Array
Page No:
Reg No:

ENTER YOUR CHOICE 1.PUSH 2.POP


1
Enter elements : 90
[90]
ENTER YOUR CHOICE 1.PUSH 2.POP
1
Enter elements : 66
[90, 66]
ENTER YOUR CHOICE 1.PUSH 2.POP
1
Enter elements : 88
[90, 66, 88]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
[90, 66]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
[90]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
[]
ENTER YOUR CHOICE 1.PUSH 2.POP
2
EMPTY STACK:java.lang.ArrayIndexOutOfBoundsException: -1
ENTER YOUR CHOICE 1.PUSH 2.POP
9
INVALID CHOICE
INVALID CHOICE

Q:\ONLINE>
********************************************/
Page No:
Reg No:

EX NO-5 TO DEVELOP INHERITANCE AND


POLYMORPHISM

import java.io.*;
import java.util.*;
abstract class vehicle
{
String name;
double weight;
int wheels;
abstract void display();
}
class car extends vehicle
{
public car(String name,double weight,int wheels)
{
this.name=name;
this.weight=weight;
this.wheels=wheels;
System.out.println("NAME:"+name);
System.out.println("WEIGHT:"+weight);
System.out.println("NO.OF WHEELS:"+wheels);
}
public void display()
{
System.out.println("*********CAR DETAILS*********");
Page No:
Reg No:

System.out.println("NAME:"+name);
System.out.println("WEIGHT:"+weight);
System.out.println("NO.OF WHEELS:"+wheels);
}
}
class Trucks extends vehicle
{
public Trucks(String name,double weight,int wheels)
{
this.name=name;
this.weight=weight;
this.wheels=wheels;
System.out.println("NAME:"+name);
System.out.println("WEIGHT:"+weight);
System.out.println("NO.OF WHEELS:"+wheels);
}
public void display()
{
System.out.println("*********TRUCKS DETAILS*********");
System.out.println("NAME:"+name);
System.out.println("WEIGHT:"+weight);
System.out.println("NO.OF WHEELS:"+wheels);
}
}
class hierarchy
{
public static void main(String arg[])
{
Scanner S=new Scanner(System.in);
System.out.println("***********CAR**********");
System.out.println("***********ENTER CAR NAME**********");
String x=S.next();
System.out.println("***********ENTER CAR WEIGHT**********");
double y=S.nextDouble();
System.out.println("***********ENTER NO.OF WHEELS IN CAR**********");
int z=S.nextInt();
System.out.println("***********ENTER TRUCKS NAME**********");
String u=S.next();
System.out.println("***********ENTER TRUCKS WEIGHT**********");
Double v=S.nextDouble();
System.out.println("***********ENTER NO.OF WHEELS IN TRUCKS
**********");
int w=S.nextInt();
car C=new car(x,y,z);
Trucks T=new Trucks(u,v,w);
C.display();
Page No:
Reg No:

T.display();
}
}

/***************OUTPUT*****************

Q:\ONLINE>javac hierarchy.java

Q:\ONLINE>java hierarchy
***********CAR**********
***********ENTER CAR NAME**********
BENZ
***********ENTER CAR WEIGHT**********
10000
***********ENTER NO.OF WHEELS IN CAR**********
4
***********ENTER TRUCKS NAME**********
mahindra
***********ENTER TRUCKS WEIGHT**********
50000
***********ENTER NO.OF WHEELS IN TRUCKS **********
4
NAME:BENZ
Page No:
Reg No:

WEIGHT:10000.0
NO.OF WHEELS:4
NAME:mahindra
WEIGHT:50000.0
NO.OF WHEELS:4
*********CAR DETAILS*********
NAME:BENZ
WEIGHT:10000.0
NO.OF WHEELS:4
*********TRUCKS DETAILS*********
NAME:mahindra
WEIGHT:50000.0
NO.OF WHEELS:4

Q:\ONLINE>
*******************************************/

EX NO-6( OBJECT SERIALIZATION)


/*****************SerializationWrite.java**********************************/
import java.io.*;
import java.util.*;

class Currency implements Serializable


{
protected String currency;
protected int amount;

public Currency(String cur, int amt)


{
this.currency = cur;
this.amount = amt;
}
public String toString()
{
return currency + amount;
}
public String dollarToRupee(int amt)
{
Page No:
Reg No:

return "Rs" + amt * 45;


}
}
class Rupee extends Currency
{
public Rupee(int amt)
{
super("Rs",amt);
}
}
class Dollar extends Currency
{
public Dollar(int amt)
{
super("$",amt);
}
}
public class SerializationWrite
{
public static void main(String args[])
{
Random r = new Random();
try
{
Currency currency;
FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing to file using Object Serialization:");
for(int i=1;i<=10;i++)
{
Object[] obj = { new Rupee(r.nextInt(200)),new Dollar(r.nextInt(200)) };
currency = (Currency)obj[r.nextInt(2)]; // RANDOM Objects for Rs and $
System.out.println(currency);
oos.writeObject(currency);
oos.flush();
}
oos.close();
}
catch(Exception e)
{
System.out.println("Exception during Serialization: " + e);
}
}
}
Page No:
Reg No:

/***************** SerializationRead.java**********************************/
import java.io.*;
import java.util.*;

public class SerializationRead


{
public static void main(String args[])
{
try
{
Currency obj;
FileInputStream fis = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Reading from file using Object Serialization:");
for(int i=1;i<=10;i++)
{
obj = (Currency)ois.readObject();
if((obj.currency).equals("$"))
System.out.println(obj + " = " + obj.dollarToRupee(obj.amount));
else
System.out.println(obj + " = " + obj);

}
ois.close();
}
catch(Exception e)
{
System.out.println("Exception during deserialization." + e);
}
}
}

/***************OUTPUT*****************

Q:\ONLINE>javac SerializationWrite.java

Q:\ONLINE>java SerializationWrite
Writing to file using Object Serialization:
Rs65
$173
Rs30
Page No:
Reg No:

$73
$48
Rs180
$65
Rs39
$32
$44

Q:\ONLINE>javac SerializationRead.java

Q:\ONLINE>java SerializationRead
Reading from file using Object Serialization:
Rs65 = Rs65
$173 = Rs7785
Rs30 = Rs30
$73 = Rs3285
$48 = Rs2160
Rs180 = Rs180
$65 = Rs2925
Rs39 = Rs39
$32 = Rs1440
$44 = Rs1980

Q:\ONLINE>

*******************************************************/

EX NO-7(SCIENTIFIC CALCULATOR )
*******************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class calculatorpanel extends JPanel
{
JButton display;
JPanel panel;
double result;
String lastcommand,a;
boolean start;
public calculatorpanel()
{
setLayout(new BorderLayout());
result=0;
lastcommand="=";
Page No:
Reg No:

start=true;
display=new JButton("0");
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert=new insertAction();
ActionListener command=new commandAction();
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
addbutton("sin",command);addbutton("7",insert);addbutton("8",insert);
addbutton("9",insert);addbutton("/",command);
addbutton("cos",command);addbutton("4",insert);addbutton("5",insert);
addbutton("6",insert);addbutton("*",command);
addbutton("tan",command);addbutton("1",insert);addbutton("2",insert);
addbutton("3",insert);addbutton("+",command);
addbutton("sqrt",command);addbutton("0",insert);addbutton(".",insert);
addbutton("=",command);addbutton("-",command);
add(panel,BorderLayout.CENTER);
}
void addbutton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
public void calculate(double x)
{
if(lastcommand.equals("+"))
result+=x;
else if(lastcommand.equals("-"))
result-=x;
else if(lastcommand.equals("*"))
result*=x;
else if(lastcommand.equals("/"))
result/=x;
else if(lastcommand.equals("="))
result=x;
else if(lastcommand.equals("sin"))
result=Math.sin(Math.toRadians(x));
else if(lastcommand.equals("cos"))
result=Math.cos(Math.toRadians(x));
else if(lastcommand.equals("tan"))
result=Math.tan(Math.toRadians(x));
else if(lastcommand.equals("sqrt"))
result=Math.sqrt(x);
display.setText(" "+result);
}
Page No:
Reg No:

class insertAction implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
String input=e.getActionCommand();
if(start)
{
display.setText(" ");
start=false;
}
display.setText(display.getText()+input);
}
}
class commandAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(start)
{
if(command.equals("-"))
{
display.setText(command);
start=false;
}
else
lastcommand=command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastcommand=command;
start=true;
}
}
}
}
class calculatorframe extends JFrame
{
public calculatorframe()
{
setSize(350,250);
setTitle("Calculator");
setLocationByPlatform(true);
Toolkit kit=Toolkit.getDefaultToolkit();
Image im=kit.getImage("U:\\calicon.jpg");
Page No:
Reg No:

setIconImage(im);
calculatorpanel panel=new calculatorpanel();
add(panel);
}
}
class calci
{
public static void main(String args[])
{
calculatorframe cf=new calculatorframe();
cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cf.setVisible(true);
}
}
/***************OUTPUT*****************
Q:\ONLINE>javac calci.java

Q:\ONLINE>java calci

EX NO-8(MULTITHREADED--PRIME AND
FIBONACCI NUMBER)********/
import java.util.*;
import java.io.*;

class Fibonacci extends Thread


{
private PipedWriter out = new PipedWriter();
public PipedWriter getPipedWriter()
{
return out;
}
Page No:
Reg No:

public void run()


{
Thread t = Thread.currentThread();
t.setName("Fibonacci");
System.out.println(t.getName() + " thread started");
int fibo1=0,fibo2=1,fibo=0;
while(true)
{
try
{
fibo = fibo1 + fibo2;
if(fibo>100000)
{
out.close();
break;
}
out.write(fibo);
sleep(100);
}
catch(Exception e)
{
System.out.println("Fibonacci:"+e);
}
fibo1=fibo2;
fibo2=fibo;
}
System.out.println(t.getName() + " thread exiting");

}
}
class Prime extends Thread
{
private PipedWriter out1 = new PipedWriter();
public PipedWriter getPipedWriter()
{
return out1;
}
public void run()
{
Thread t= Thread.currentThread();
t.setName("Prime");
System.out.println(t.getName() + " thread Started...");
int prime=1;
while(true)
{
try
Page No:
Reg No:

{
if(prime>100000)
{
out1.close();
break;
}
if(isPrime(prime))
out1.write(prime);
prime++;
sleep(0);
}
catch(Exception e)
{
System.out.println(t.getName() + " thread exiting.");
System.exit(0);
}
}
}
public boolean isPrime(int n)
{
int m=(int)Math.round(Math.sqrt(n));
if(n==1 || n==2)
return true;
for(int i=2;i<=m;i++)
if(n%i==0)
return false;
return true;
}
}
public class PipedIo
{
public static void main(String[] args) throws Exception
{
Thread t=Thread.currentThread();
t.setName("Main");
System.out.println(t.getName() + " thread Started...");
Fibonacci fibonacci = new Fibonacci();
Prime prime = new Prime();
PipedReader fpr = new PipedReader(fibonacci.getPipedWriter());
PipedReader ppr = new PipedReader(prime.getPipedWriter());
fibonacci.start();
prime.start();
int fib=fpr.read(), prm=ppr.read();
System.out.println("The numbers common to PRIME and FIBONACCI:");
while((fib!=-1) && (prm!=-1))
{
Page No:
Reg No:

while(prm<=fib)
{
if(fib==prm)
System.out.println(prm);
prm=ppr.read();
}
fib=fpr.read();
}
System.out.println(t.getName() + " thread exiting");
}
}
/***************OUTPUT*****************

Q:\ONLINE>javac PipedIo.java

Q:\ONLINE>java PipedIo
Main thread Started...
Fibonacci thread started
Prime thread Started...
The numbers common to PRIME and FIBONACCI:
1
2
3
5
13
89
233
1597
Fibonacci thread exiting
28657
Main thread exiting
Prime thread exiting.

**************************************************/

/
Page No:
Reg No:

You might also like