DEPARTMENT OF
COMPUTER SCIENCE ENGINEERING
2011-2012
FIFTH SEMESTER
CONTENTS
SNO
|
DATE
|
NAME OF THE EXPERIMENT
|
PAGE NO
|
SIGN
|
1
|
|
Rational
Number Class
|
|
|
2
|
|
Date Class
|
|
|
3
|
|
Lisp-Like
List Operation
|
|
|
4
|
|
Java
Interface for ADT Stack
|
|
|
5
|
|
Vehicle
Class Hierarchy
|
|
|
6
|
|
Inheritance
|
|
|
7
|
|
Simple
Calculator in Java
|
|
|
8
|
|
Multi-Threaded
Java Program
|
|
|
9
|
|
Simple OPAC
System for Library
|
|
|
10
|
|
Multi-Threaded
Echo Server Client Program
|
|
|
EX.No:1
RATIONAL NUMBER CLASS
PROGRAM
Rational.java
import java.util.Scanner;
public class Rational
{
private int numerator;
private int denominator;
public Rational(int numerator,int denominator)
{
if(denominator==0)
{
throw new RuntimeException("Denominator is zero");
}
int g=gcd(numerator,denominator);
if(g==1)
{
System.out.println("No common divisor for numerator and
denominator");
this.numerator=numerator;
this.denominator=denominator;
}
else
{
this.numerator=numerator/g;
this.denominator=denominator/g;
}
}
public String toString()
{
return numerator+"/"+denominator ;
}
private static int gcd(int m,int n)
{
if(0==n)
return m;
else
return gcd(n,m%n);
}
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter numerator:");
int numerator=scanner.nextInt();
System.out.println("Enter denominator:");
int denominator=scanner.nextInt();
Rational rational;
rational=new Rational(numerator,denominator);
System.out.println("Efficient representation for the rational
number:"+rational);
}}
OUTPUT
EX.NO:2 DATE
CLASS
PROGRAM
DateTest.java
import java.util.Scanner;
import java.util.*;
class DateNK
{
void DateNK()
{
day=1;
month=1;
year=0;
}
DateNK(int aday,int amonth,int ayear)
{
day=aday;
month=amonth ;
year=ayear ;
}
int getno_of_days(int m)
{
if(m<=7)
{
if(m%2==0)
{
if(m==2)
{
i=leap(year);
if(i==1)
no_of_days=29;
else
no_of_days=28;
}
else
no_of_days=30;
}
else
no_of_days=31;
}
if(m>=8&&m<=12)
{
if(m%2==0)
no_of_days=31;
else
no_of_days=30;
}
return no_of_days;
}
void addMonth(int month1)
{
m=month1+month;
if(m>12)
{
while(m>12)
{
m=m-12;
year=year+1;
}
}
month=m;
check();
}
void addDays(int day1)
{
d=day1+day;
no_of_days=getno_of_days(month);
if(no_of_days<d)
{
while(no_of_days<d)
{
d=d-no_of_days;
addMonth(1);
no_of_days=getno_of_days(month);
}
}
day=d;
}
void subMonth(int month1)
{
m=month-month1;
if(m==0)
{
m=12;
year=year-1;
}
if(m<0)
{
while(m<0)
{
m=m+12;
year=year-1;
if(m==0)
{
m=12;
break;
}
}
}
month=m;
check();
}
void subDays(int day1)
{
d=day-day1;
if(d==0)
{
subMonth(1);
d=getno_of_days(month);
}
if(d<0)
{
while(d<0)
{
no_of_days=getno_of_days(month);
d=d-no_of_days;
subMonth(1);
if(d==0)
{
d=getno_of_days(month);
break;
}
}
}
day=d;
}
void check()
{
if(month==2)
{
if(day>28)
{
if(day==29)
{
i=leap(year);
if(i==0)
day=28;
}
else
day=28;
}
}
}
void display()
{
System.out.println("\nCurrent Date is : "+day+" :
"+month+" : "+year);
System.out.println("\nNo Of Days in Month "+month+" is :
"+getno_of_days(month));
}
int leap(int y)
{
if(y%4==0)
return 1;
else
return 0;
}
int month,year,day,m,d,i,no_of_days;
}
class DateTest
{
public static void main(String[] args)
{
int m;
DateNK d=new DateNK(25,12,1991);
d.display();
Scanner s=new Scanner(System.in);
System.out.println("\nEnter No of Days to Add : ");
m=s.nextInt();
d.addDays(m);
d.display();
System.out.println("\nEnter No of Month to Add : ");
m=s.nextInt();
d.addMonth(m);
d.display();
System.out.println("\nEnter No of Day to Sub : ");
m=s.nextInt();
d.subDays(m);
d.display();
System.out.println("\nEnter No of Month to Sub : ");
m=s.nextInt();
d.subMonth(m);
d.display();
}
}
OUTPUT
EX.NO:3 LISP
– LIKE LIST OPERATION
PROGRAM
LispCommands.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Logger;
public class LispCommands
{
private String[] tokenList;
private static Logger LOGGER=Logger.getLogger(LispCommands.class.getName());
public LispCommands()
{
}
private void car()
{
LOGGER.info(tokenList[0]);
}
private void cdr()
{
List<String>list=Arrays.asList(tokenList);
ArrayList<String>slist=new ArrayList<String>(list);
slist.remove(0);
display(slist);
}
private void cons(String args)
{
List<String>arrayList=new
ArrayList<String>(Arrays.asList(tokenList));
arrayList.add(args);
Collections.reverse(arrayList);
display(arrayList);
}
private void parse(String args)
{
ArrayList<String>tokenList=new ArrayList<String>();
if(args!=null)
{
StringTokenizer tokens=new StringTokenizer(args,"[]");
while(tokens.hasMoreElements())
{
StringTokenizer commaTokens=new StringTokenizer(tokens.nextToken(),",");
while(commaTokens.hasMoreElements())
{
String token=commaTokens.nextToken();
if(token!=null&&!token.trim().equals(""))
{
tokenList.add(token.trim());
}
}
}
}
this.tokenList=tokenList.toArray(new String[0]);
}
private void display(Object result)
{
System.out.println();
if(result instanceof String)
{
LOGGER.info(result.toString());
}
else
if(result.getClass().getName().equals("java.util.ArrayList"))
{
LOGGER.info(result.toString());
}
}
public static void main(String[] args)
{
LispCommands L=new LispCommands();
L.parse("[3,0,2,5]");
L.car();
L.cdr();
L.cons("7");
}
}
OUTPUT
EX.NO:4 JAVA
INTERFACE FOR ADT STACK
PROGRAM
ADTStack.java
interface ADTStack
{
public void push(Object item);
public Object pop();
}
ADTArray.java
public class ADTArray implements
ADTStack
{
Object[] array;
int index;
public ADTArray()
{
this.array=new Object[128];
this.index=0;
}
public Object pop()
{
index--;
return array[index];
}
public void push(Object item)
{
array[index]=item;
index++;
}
public static void main(String[] args)
{
ADTStack stack=new ADTArray();
stack.push("Hi");
stack.push(new Integer (100));
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
ADTList.java
public class ADTList implements ADTStack
{
private StackElement top;
private int count=0;
public void push(Object obj)
{
StackElement stackElement=new
StackElement(obj);
stackElement.next=top;
top=stackElement;
count++;
}
public Object pop()
{
if(top==null)
return null;
Object obj=top.value;
top=top.next;
count--;
return obj;
}
class StackElement
{
Object value;
StackElement next;
public StackElement(Object obj)
{
value=obj;
next=null;
}
}
public static void main(String[] args)
{
ADTStack stack=new ADTList();
stack.push("Hi");
stack.push(new Integer (100));
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
OUTPUT
EX.No:5
VEHICLE CLASS HIERARCHY
PROGRAM
Test.java
import java.io.*;
import java.util.*;
abstract class vehicle
{
String name;
double weight;
int wheels;
abstract void display();
}
class car extends vehicle
{
public int passenger;
public car(String name,int wheels,int passenger,double weight)
{
this.name=name;
this.weight=weight;
this.passenger=passenger;
this.wheels=wheels;
}
public void display()
{
System.out.println("Name="+name);
System.out.println("Weight="+weight);
System.out.println("Passenger="+passenger);
System.out.println("Wheels="+wheels);
}
}
class truck extends vehicle
{
public double payload;
public truck(String name,int wheels,double payload,double weight)
{
this.name=name;
this.weight=weight;
this.payload=payload;
this.wheels=wheels;
}
public void display()
{
System.out.println("Name="+name);
System.out.println("Weight="+weight);
System.out.println("Payload="+payload);
System.out.println("Wheels="+wheels);
}
}
class Test
{
public static void main(String args[])
{
car car=new car("Maruthi",4,4,1000);
truck trk=new truck("S10",8,2300,4000);
System.out.println("Car...");
car.display();
System.out.println("\nTruck...");
trk.display();
}
}
OUTPUT
EX.NO:6
INHERITANCE
PROGRAM
Currency.java
import java.io.Serializable;
import java.io.*;
public abstract class Currency
implements Serializable
{
protected static final double
DOLLER_RUPEE_EXCHANGERATE=44.50;
public Currency(double money)
{
super();
this.money=money;
}
protected double money;
public abstract double getValue();
public abstract String
getPrintableValue();
}
Rupee.java
class Rupee extends Currency
{
public Rupee(double amount)
{
super(amount);
}
public double getValue()
{
return this.money;
}
public String getPrintableValue()
{
String
Strvalue="ObjectName:Rupee\nINR:Rs"+getValue()+"\n===============";
return Strvalue;
}
}
Doller.java
class Doller extends Currency
{
public Doller(double money)
{
super(money);
}
public double getValue()
{
return(this.money*DOLLER_RUPEE_EXCHANGERATE);
}
public String getPrintableValue()
{
String
Strvalue="ObjectName:Doller\nUSD:$"+this.money+"\nINR:Rs"+getValue()+"\n....................\n";
return Strvalue;
}
}
StoreCurrency.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Random;
public class StoreCurrency
{
public static void main(String[]
args)throws FileNotFoundException,IOException
{
Currency Currency=null;
ObjectOutputStream out=new
ObjectOutputStream(new FileOutputStream(new File("Currency.dat")));
Random random=new Random();
for(int i=0;i<10;i++)
{
int decide=random.nextInt();
double value=(random.nextDouble()*10);
if((decide%2)==0)
{
Currency=new Rupee(value);
}
else
{
Currency=new Doller(value);
}
out.writeObject(Currency);
}
out.writeObject(null);
out.close();
}
}
ReadCurrency.java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ReadCurrency
{
public static void main(String[]
args)throws IOException,ClassNotFoundException
{
Currency Currency=null;
ObjectInputStream in=new
ObjectInputStream(new FileInputStream(new File("Currency.dat")));
while((Currency=(Currency)in.readObject())!=null)
{
System.out.println(Currency.getPrintableValue());
}
in.close();
}
}
OUTPUT
EX.NO:7
SIMPLE CALCULATOR IN JAVA
PROGRAM
Calculator.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CalculatorFrame frame=new
CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel=new
CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
result=0;
lastCommand="=";
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,4));
addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command);
addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command);
add(panel,BorderLayout.CENTER);
}
private void addButton(String
label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements
ActionListener
{
public void actionPerformed(ActionEvent
event)
{
String input=event.getActionCommand();
if(start)
{
display.setText("");
start=false;
}
display.setText(display.getText()+input);
}
}
private class CommandAction implements
ActionListener
{
public void actionPerformed(ActionEvent
event)
{
String command=event.getActionCommand();
if(start)
{
if(command.equals("-"))
{
display.setText(command);
start=false;
}
else
lastCommand=command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand=command;
start=true;
}
}
}
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;
display.setText(""+result);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
OUTPUT
EX.NO:8
MULTI-THREADED JAVA PROGRAM
PROGRAM
FibonacciGenerator.java
import java.io.IOException;
import java.io.PipedWriter;
public class FibonacciGenerator extends
Thread
{
private PipedWriter fibWriter=new
PipedWriter();
public PipedWriter getPipedWriter()
{
return fibWriter;
}
public void run()
{
super.run();
generateFibonacci();
}
private int f(int n)
{
if(n<2)
{
return n;
}
else
{
return f(n-1)+f(n-2);
}
}
public void generateFibonacci()
{
for(int
i=2,fibValue=0;(fibValue=f(i))<10000;i++)
{
try
{
fibWriter.write(fibValue);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
new FibonacciGenerator().generateFibonacci();
}
}
Receiver.java
import java.io.IOException;
import java.io.PipedReader;
class Receiver extends Thread
{
private PipedReader fibReader;
private PipedReader primeReader;
public Receiver(FibonacciGenerator
fib,PrimeGenerator prime)throws IOException
{
fibReader=new
PipedReader(fib.getPipedWriter());
primeReader=new
PipedReader(prime.getPipedWriter());
}
public void run()
{
int prime=0;
int fib=0;
try
{
prime=primeReader.read();
fib=fibReader.read();
}
catch(IOException e1)
{
e1.printStackTrace();
}
while(true)
{
try
{
if(prime==fib)
{
System.out.println("Match::"+prime);
prime=primeReader.read();
fib=fibReader.read();
}
else if(fib<prime)
{
fib=fibReader.read();
}
else
{
prime=primeReader.read();
}
}
catch(IOException e)
{
System.exit(-1);
}
}
}
}
PrimeGenerator.java
import java.io.IOException;
import java.io.PipedWriter;
public class PrimeGenerator extends
Thread
{
private PipedWriter primeWriter=new
PipedWriter();
public PipedWriter getPipedWriter()
{
return primeWriter;
}
public void run()
{
super.run();
generatePrime();
}
public void generatePrime()
{
for(int i=2;i<10000;i++)
{
if(isPrime(i)){
try
{
primeWriter.write(i);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}
private boolean isPrime(int n)
{
boolean prime=true;
int sqrtValue=(int)Math.sqrt(n);
for(int i=2;i<=sqrtValue;i++)
{
if(n%i==0)
{
prime=false;
}
}
return prime;
}
public static void main(String[]
args)throws IOException
{
PrimeGenerator gene=new
PrimeGenerator();
gene.generatePrime();
}
}
Main.java
import java.io.IOException;
public class Main
{
public static void main(String[]
args)throws IOException
{
FibonacciGenerator
fibonacciGenerator=new FibonacciGenerator();
PrimeGenerator primeGenerator=new
PrimeGenerator();
Receiver receiver=new Receiver(fibonacciGenerator,primeGenerator);
fibonacciGenerator.start();
primeGenerator.start();
receiver.start();
}
}
OUTPUT
No comments:
Post a Comment