Ex.
No:1 IMPLEMENTATION OF RATIONAL NUMBERS
Date:
AIM:
To develop Rational number class in Java and Use JavaDoc comment for documentation.
ALGORITHM:
STEP 1: Start the program
STEP 2: Get numerator and denominator inputs from the user
STEP 3: If denominator is zero, it throws an exception
STEP 4: Else find the Rational number using gcd.
STEP 5: Print the given number in the rational number format.
STEP 5: Stop the program
PROGRAM:
import [Link];
class Rational
{
private int numerator;
private int denominator;
public Rational(int n, int d)
{
numerator = n;
denominator = d;
if (denominator == 0)
{
throw new RuntimeException("Denominator is zero");
}
}
public void findRational()
{
int g = gcd(numerator, denominator);
if (g == 1)
{
[Link]("No Common Divisor for Numerator and Denominator");
}
else
{
numerator = numerator / g;
denominator = denominator / g;
}
}
private int gcd(int m, int n)
{
int d;
while(m % n != 0)
{
d = m % n;
m = n;
n = d;
}
return n;
}
public void display()
{
[Link]("The rational number:"+numerator + "/" + denominator);
}
}
public class JavaApplication22
{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter Numerator : ");
int numerator = [Link]();
[Link]("Enter Denominator : ");
int denominator = [Link]();
Rational rational = new Rational(numerator, denominator);
[Link]();
[Link]();
} Aim & Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total
[Link] IMPLEMENTATION OF DATE CLASS
Date:
AIM:
To develop Date class in Java similar to the one available in [Link] package.
ALGORITHM:
STEP 1: Start the program
STEP 2: Create a class named Mydate and get the date, month and year from the user
STEP 3: Use setDate member method to check whether the date, month and year is valid.
STEP 4: Check for the year is leap year
STEP 5: If the date is valid,Print the date in the dd-mm-yyyy format
STEP 6: Stop the program
PROGRAM:
[Link].*;
classMyDate
{
privateint day;
privateint month;
privateint year;
int[] DaysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30,31};
public void setDate(int d, int m, int y)
{
boolean valid = isValid(d, m, y);
if (valid)
{
day = d;
month = m;
year = y;
}
else
{
throw new RuntimeException("Invalid Date Format");
}
}
privatebooleanisValid(int d, int m, int y)
{
setLeapYearDays(y);
if (m < 1 || m > 12)
{
return false;
}
if (d < 1 || d >DaysInMonth[m])
{
return false;
}
else
{
return true;
}
}
private void setLeapYearDays(int y)
{
booleanisLeapYear;
if (y % 400 == 0)
{
isLeapYear = true;
}
else if (y % 100 == 0)
{
isLeapYear = false;
}
else if (y % 4 == 0)
{
isLeapYear = true;
}
else
{
isLeapYear = false;
}
if (isLeapYear)
{
DaysInMonth[2] = 29;
}
else
{
DaysInMonth[2] = 28;
}
}
String dateFormat()
{
return day + "-" + month + "-" + year;
}
}
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter the day: ");
int d = [Link]();
[Link]("Enter the month: ");
int m = [Link]();
[Link]("Enter the year: ");
int y = [Link]();
MyDate today = new MyDate();
[Link](d,m,y);
[Link]("Input Date : " +[Link]());
}
Aim & Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total
[Link] IMPLEMENTATION OF LISP-LIKE-LIST
Date:
AIM:
To implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons'. If L
is a list [3, 0, 2, 5], [Link]() returns 3, while [Link]() returns [0,2,5].
ALGORITHM:
STEP 1: Create a node of a list having data part and link part.
STEP 2: Create a menu having the following choices : insert, car, cdr, adjoin and display.
STEP 3: Read the choice from the user and call the respective m ethods.
STEP 4: Create another class which implements the same interface to implement the concept of
stack through linked list.
STEP 5: To insert Create an object of node and append to the list.
STEP 6: To Perform CAR, Return the first node data.
STEP 7: To perform CDR, Return all the node (data part) in the list except the first node.
PROGRAM:
import [Link].*;
class Lisp
{
//To perform car() function
public int car(List I)
{
Object ob=[Link](0);
String st=[Link]();
[Link]("Using a [Link]() function");
return [Link](st);
}
public List cdr(List I)
{
Object ob=[Link](0);
Object obj[]=[Link]();
[Link]("Using a [Link]() function");
List list=[Link](obj);
return list;
}
}
class Demo
{
public static void main(String[] args)
{
//To add the integer value in the ArrayList
List<Integer> I=new ArrayList<Integer>();
[Link](3);
[Link](0);
[Link](2);
[Link](5);
//To print the Return values of Car(),Cdr() functions
Lisp L=new Lisp();
int val=[Link](I);
[Link](val);
List list=[Link](I);
[Link](list);
}
}
Aim & Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total
[Link] IMPLEMENTATION OF JAVA INTERFACE FOR ADT STACK
Date:
AIM:
To design a Java interface for ADT Stack. Develop two different classes that implement this
interface, one using array and the other using linked-list.
ALGORITHM:
STEP 1: Create an interface which consists of three methods namely PUSH, POP
STEP 2: Create a class which implements the above interface to implement the concept of stack
through Array
STEP 3: Define all the methods of the interface to push any element, to pop the top element and to
display the elements present in the stack.
STEP 4: Create another class which implements the same interface to implement the concept of
stack through linked list.
STEP 5: Repeat STEP 4 for the above said class also.
STEP 6: In the main class, get the choice from the user to choose whether array implementation or
linked list implementation of the stack.
STEP 7: Call the methods appropriately according to the choices made by the user in the previous
step.
STEP 8: Repeat step 6 and step 7 until the user stops execution.
PROGRAM:
import [Link].*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(top==10)
[Link]("overflow"+top);
else
{
stack[++top]=item;
[Link]("item pushed");
[Link]( "value of top is"+top);
}
}
public void pop()
{
if(top<=0)
[Link]("underflow");
else
{
stack[top]=top--;
[Link]("item popped");
}
}
public void display()
{
for(int i=1;i<=top;i++)
{
[Link]("element:"+stack[i]);
}
}
}
class liststack implements stackoperation
{
node top,q;
int count;
public void push(int i)
{
node n=new node(i);
[Link]=top;
top=n;
count++;
}
public void pop()
{
if(top==null)
[Link]("under flow");
else
{
int p=[Link];
top=[Link];
count--;
[Link]("popped element:"+p);
}
}
void display()
{
for(q=top;q!=null;q=[Link])
{
[Link]("the elements are:"+[Link]);
}
}
class node
{
int data;
node link;
node(int i)
{
data=i;
link=null;
}
}
}
class Demo
{
public static void main(String args[])throws IOException
{
int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream([Link]);
do
{
try
{
[Link]("----------------------------------");
[Link]("[Link] [Link] [Link]");
[Link]("-----------------------------------");
[Link]("enter ur choice:");
int c=[Link]([Link]());
Astack s=new Astack();
switch(c)
{
case 1:
do
{
if(p==1)
break;
[Link]("ARRAY STACK");
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter ur choice:");
ch=[Link]([Link]());
switch(ch)
{
case 1:
[Link]("enter the value to push:");
int i=[Link]([Link]());
[Link](i);
break;
case 2:
[Link]();
break;
case 3:
[Link]("the elements are:");
[Link]();
break;
case 4:
p=1;
continue;
}
}while(x!=0);
break;
case 2:
liststack l=new liststack();
do
{
if(t==1)
break;
[Link]("LIST STACK:");
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter your choice:");
ch=[Link]([Link]());
switch(ch)
{
case 1:
[Link]("enter the value for push:");
int a=[Link]([Link]());
[Link](a);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
t=1;
continue;
}
}while(x!=0);
break;
case 3:
[Link](0);
}
}
catch(IOException e)
{
[Link]("io error");
}
}while(x!=0);
}
}
Aim & Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total
[Link] IMPLEMENTATION OF POLYMORPHISM
Date:
AIM:
To design a Vehicle class hierarchy in Java. Write a test program to demonstrate
Polymorphism.
ALGORITHM:
STEP 1: Start the program
STEP 2: Create a base class named vehicle with constructor to initialize the value and display
method
STEP 3: Extend two classes namely car and lorry from the base class.
STEP 4: Define the method display under the class car by displaying all the entered values.
STEP 5: Define the method display under the class Lorry by displaying all the entered values..
STEP 6: Create an object reference
STEP 7: Create an object for the derived class and assign the object to the base class reference
STEP 8: stop the program
PROGRAM
class Vehicle
{
String regno;
String brand;
int year;
int price;
Vehicle()
{
}
Vehicle(String reg, String brd, int yr, int pr)
{
regno=reg;
brand=brd;
year=yr;
price=pr;
}
void display()
{
[Link]("registration no: " + regno);
[Link]("brand: " + brand);
[Link]("year: " + year);
[Link]("price: " + price);
}
}
class Car extends Vehicle
{
int totalseats;
Car(String reg, String brd, int yr, int pr,int seat)
{
super(reg,brd,yr,pr);
totalseats=seat;
}
void display()
{
[Link]("Car Details");
[Link]("***********");
[Link]();
[Link]("total no of seats: " +totalseats);
}
}
class Lorry extends Vehicle
{
int MaxLoad;
Lorry(String reg, String brd, int yr, int pr, int max)
{
super(reg,brd,yr,pr);
MaxLoad=max;
}
void display()
{
[Link]("\nLorry Details");
[Link]("*************");
[Link]();
[Link]("maxload : " +MaxLoad+"tone");
}
}
class Bike extends Vehicle
{
int cc;
Bike(String reg, String brd, int yr, int pr, int c)
{
super(reg,brd,yr,pr);
cc=c;
}
void display()
{
[Link]("\nBike Details");
[Link]("************");
[Link]();
[Link]("cc : " +cc);
}
}
public class Vehicledemo {
public static void main(String[] args) {
// TODO code application logic here
Vehicle veh = new Vehicle();
Car ca=new Car("TN 10 1010","Maruti",2008,500000,5);
Lorry lo=new Lorry("TN 10 1010","Tata",2009,1000000,15);
Bike bi=new Bike("TN 10 1010","Pulsar",2013,80000,180);
veh=ca;
[Link]();
veh=lo;
[Link]();
veh=bi;
[Link]();
}
Aim & Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total
[Link] IMPLEMENTATION OF OBJECT SERILIZATION
Date:
AIM:
To design classes for Currency, Rupee, and Dollar. Write a program that randomly generates
Rupee and Dollar objects and write them into a file using object serialization.
ALGORITHM :
STEP 1: Create a class named currency that implements the serializable interface and also it is
the base class for rupee and dollar classes.
STEP 2: Create an object for ObjectOutputStream to open a file in write mode using
FileOutputStream.
STEP 3: Read the user choice to enter rupee or dollar amount.
STEP 4: Generate random numbers as the value of rupee or dollar.
STEP 5: If choice is rupee then, append "Rs" to the value generated, else if choice is dollar
append "$" to the value generated.
STEP 6: Display the appended String and also write it into the file opened using the
writeObject() method.
STEP 7: Close the file.
STEP 8: Create a class named currency that implements the serializable interface and also it is
the base class for rupee and dollar classes.
STEP 9: Create an object for ObjectInputStream to open the file created in program1 in read
mode using FileInputStream.
STEP 10: If the file does not exist or if it is empty show exceptions.
STEP 11: While the End of file is not reached, do the following...
(i) If the value read is a dollar convert into rupee and print to the user otherwise print the rupee as
such.
STEP 12: End the program.
PROGRAM
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
class Rupee
{
public Rupee()
{
try
{
Object object=new Object();
object="45";
ObjectOutput out=new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](object);
[Link]();
}
catch(Exception e)
{
[Link]();
}
}
}
class Dollar
{
public Dollar()
{
try
{
Object object=new Object(); object="45";
ObjectOutput out=new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](object);
[Link]();
}
catch(Exception e)
{
[Link]();
}
}
}
public class Currencytest {
public static void main(String[] args) {
new Rupee();
new Dollar();
[Link]("Select Input Type");
[Link]("\[Link]\[Link]\n\n");
Scanner input=new Scanner([Link]);
int choice=[Link]();
if(choice==1)
{
[Link]("Enter No of Dollar:");
int noDollar=[Link]();
String value="";
String filename="[Link]";
FileInputStream fis=null;
ObjectInputStream in=null;
try
{
fis=new FileInputStream(filename);
in=new ObjectInputStream(fis);
value=(String)[Link](); [Link]();
}
catch(IOException ex)
{
[Link]();
}
catch(ClassNotFoundException ex)
{
[Link]();
}
[Link]("The Equal Rupee
s:"+noDollar*([Link](value)));
[Link]();
}
else if(choice==2)
{
[Link]("Enter Rupee:");
int noRupee=[Link]();
[Link]("The Rupee is:"+noRupee); [Link]();
}
}
}
& Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total
[Link] IMPLEMENTATION OF SCENTIFIC CALCULATOR USING
Date: EVENT DRIVEN PROGRAMMING
AIM:
To develop a scientific calculator using even-driven programming paradigm of Java.
ALGORITHM:
STEP 1: Create a panel consisting of Buttons for various scientific operations.
STEP 2: Create Button actions.
STEP 3: Place the panel onto a frame.
STEP 4: Associate each Button click with the corresponding actionlistener.
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new
CalculatorPanel(); add(panel);
pack();
}
}
class CalculatorPanel extends JPanel
{
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
public CalculatorPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand ="=";
start = true;
display = new JButton("0");
[Link](false);
add(display,
[Link]);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();
[Link](new GridLayout(6,5));
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("CE", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("m+", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("m-", command);
addButton("0", insert);
addButton(".", insert);
addButton("+/-",command);
addButton("+", command);
addButton("n!", command);
addButton("pow", command);
addButton("1/x", insert);
addButton("SQRT", insert);
addButton("log", insert);
addButton("%",command);
addButton("sin", insert);
addButton("cos", insert);
addButton("tan",insert);
addButton("x2", insert);
addButton("=", command);
add(panel, [Link]);
}
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
[Link](listener);
[Link](button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = [Link](); if
(start==true)
{
[Link]("");
start = false;
}
if([Link]("1/x"))
[Link](""+1/[Link]([Link]()));
else if([Link]("SQRT"))
[Link](""+[Link]([Link]([Link]())));
else if([Link]("log"))
[Link](""+[Link]([Link]([Link]())));
else if([Link]("x2"))
[Link](""+[Link]([Link]())*[Link]
Double([Link]()));
else if([Link]("sin"))
{
Double angle=[Link]([Link]())*2.0*[Link]/360.0;
[Link](""+[Link](angle));
}
else if([Link]("cos"))
{
Double angle= [Link]([Link]())*2.0*[Link]/360.0;
[Link](""+[Link](angle));
}
else if([Link]("tan"))
{
Double angle=[Link]([Link]())*2.0*[Link]/360.0;
[Link](""+[Link](angle));
}
else
[Link]([Link]() + input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = [Link]();
if (start==true)
{
if ([Link]("-"))
{
[Link](command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate([Link]([Link]()));
lastCommand = command;
start = true;
}
}
}
public void calculate(double x)
{
if ([Link]("+"))
result += x;
else if ([Link]("-"))
result -= x;
else if ([Link]("*"))
result *= x;
else if ([Link]("/"))
result /= x;
else if ([Link]("="))
result = x;
else if ([Link]("CE"))
result = 0.0;
else if ([Link]("m+"))
result = result;
else if ([Link]("m-"))
result = 0.0;
else if ([Link]("pow"))
{
double powval=1.0;
for(double i=0.0;i<x;i++)
powval*=result;
result=powval;
}
[Link](""+ result);
}
}
Aim & Algorithm
Program Coding
Compilation and Debugging
Execution and Results
Viva
Total