EX: NO. 1 Rational Number Class in Java Date
EX: NO. 1 Rational Number Class in Java Date
EX: NO. 1 Rational Number Class in Java Date
DATE:
AIM:
ALGORITHM:
import java.io.*;
import java.math.*;
public class TestRationalClass
{
private int num;
private int den;
RESULT:
Thus the program to develop a rational number class with methods to perform the
basic arithmetic operations was executed and the output was verified successfully.
EX: NO. 2 Date class in Java
DATE:
AIM:
To develop Date class in Java similar to the one available in java.util package. Use
JavaDoc comments.
ALGORITHM:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class BasicDateFormatting
{
public static void main(String[] args)throws Exception
{
Date today=Calendar.getInstance().getTime();
DateFormat shortFormatter=
SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
DateFormat longFormatter=
SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG);
DateFormat mediumFormatter=
SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM,SimpleD
ateFormat.LONG);
System.out.println(shortFormatter.format(today));
System.out.println(longFormatter.format(today));
System.out.println(mediumFormatter.format(today));
String DateAsText=shortFormatter.format(today);
Date TextAsDate=shortFormatter.parse(DateAsText);
System.out.println(TextAsDate);
}
}
OUTPUT:
RESULT:
Thus the program to develop a Date class was executed and the output was
verified successfully.
EX: NO. 3 Lisp-like list in Java
DATE:
AIM:
To implement Lisp-like list in Java. To perform the basic operations such as 'car',
'cdr', and 'cons'.
ALGORITHM:
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 LispCommands()
{
}
tokenList.add(token.trim());
}
}
}
this.tokenList = tokenList.toArray(new String[0]);
}
RESULT:
Thus the program to implement Lisp-like list in Java was executed and the output
was verified successfully.
EX: NO. 4 Design a Java interface for ADT Stack
DATE:
AIM:
To design a Java interface for ADT Stack and to develop two different classes
that implements this interface, one using array and the other using linked-list.
ALGORITHM:
import java.io.*;
import java.util.*;
interface Mystack
{
public void pop();
public void push();
public void display();
}
class Stack_array implements Mystack
{
final static int n=5;
int stack[]=new int[n];
int top=-1;
public void push()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
if(top==(n-1))
{
System.out.println(" Stack Overflow");
return;
}
else
{
System.out.println("Enter the element");
int ele=Integer.parseInt(br.readLine());
stack[++top]=ele;
}
}
catch(IOException e)
{
System.out.println("e");
}
}
public void pop()
{
if(top<0)
{
System.out.println("Stack underflow");
return;
}
else
{
int popper=stack[top];
top--;
System.out.println("Popped element:" +popper);
}
}
RESULT:
Thus the program to implement Stack ADT using array and linked list was
executed and the output was verified successfully.
EX: NO. 5 Design a Vehicle class hierarchy in Java
DATE:
AIM:
ALGORITHM:
import java.io.*;
class Vehicle
{
String regno;
int model;
Vehicle(String r, int m)
{
regno=r;
model=m;
}
void display()
{
System.out.println("Registration no: "+regno);
System.out.println("Model no: "+model);
}
}
RESULT:
DATE:
AIM:
. To design classes namely Currency, Rupee, and Dollar. To write a program that
randomly generates Rupee and Dollar objects and writes them into a file using object
serialization. To write another program to read that file, and to convert to Rupee if it
reads a Dollar, while leave the value as it is if it reads a Rupee.
ALGORITHM:
Currency.java
import java.io.Serializable;
public abstract class Currency implements Serializable
{
protected static final Double DOLLAR_RUPEE_EXCHAGERATE = 44.445D;
public Currency(Double money)
{
super();
this.money = money;
}
protected Double money;
public abstract Double getValue ();
public abstract String getPrintableValue();
}
Rupee.java
Dollar.java
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 Dollar(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:
RESULT:
Thus the program to generate objects randomly and to write them into a file using
object serialization was executed. The file was read and the required rupee-dollar
conversions were performed and the output was verified successfully.