Dharanyadevi blogspot subsume with E-books, Notes, Lab Manual, Question Banks, Interview Tips, Viva Questions, Basics and Interview Questions for engineering students. For Any Help Contact dharanyadevi@gmail.com

SEARCH

Image

Thursday, August 30, 2012

Java Programming Lab Part 1

Ex. No: Rational Number Representation

Aim:

To write a program for Rational Number Representation

Algorithm:

Step 1: Start the Program

Step2:Define a Class called fraction

Step 3:In the class Define a Function frac() to find greatest common divisor

Step 4:Define another class called ration with main() function

Step 5:In the main() get the input()

Step 6:Create an Object for class fraction

Step 7:Calculate the numerator and denominator using the function frac() Step 6:Then print the number as numerator/denominator

Program:

import java.io.*;

classfraction

{

int frac(int a,int b)

{

int x,y,i;

for(i=min(a,b);i>0;i--)

{

x=a%i; y=b%i; if((x==0)&&(y==0)) break;

}

return i;

}

int min(int a,int b)

{ if(a>b) return b; else return a;

}

}

class ration

{

public static void main(String args[])

{

int a,b,c,nu,di;

System.out.println("Enter the numerator and denominator values:"); DataInputStream din=new DataInputStream(System.in);

String x=din.readline();

a=Integer.parseInt(x);

String y=din.readline() b=Integer.parseInt(y); fraction f=new fraction(); c=f.frac(a,b);

nu=a/c;

di=b/c;

System.out.println("The fraction is"+nu+"/"+di);

}

}

Result:

Thus the Program for rational number representation is executed Sucessfully

Output:

Enter the Numerator and Denominator Values:

35

5

The fraction is 7/1

Ex. No: Packages and JavaDoc

Aim:

To write a program to implement the lisp list

Algorithm:

Step 1:Start the Program

Step 2: Define a class called List

Step 3:In it define a method cons() for getting the elements to be inserted

Step 4:Define another method car() to display the first element list Step 5:Define cdr() method to display the remaining elements of list Step 6:Define another class lisp with main() function

Step 6:In it create an object for List class and get a choice

Step 7: If choice is 1,call the cons() function

Step 8:ElseIf choice is 2,call the car() function Step 9:ElseIf choice is 3,call the cdr() function Step 10:ElseIf choice is 4,Exit the program Step 11:Goto step 6

Step 12:Stop the program

Program:

import java.io.*; import java.util.*; import java.lang.String; class Lisp

{

public static void main(String[] args)throws IOException

{

int ch;

boolean x=true;

list lis=new list();

DataInputStream d=new DataInputStream(System.in);

System.out.println("Lisp list implementation"); System.out.println("\n\tOPTIONS\tACTIONS"); System.out.println("\t~~~~~~~~~\t\t~~~~~~"); System.out.println("\t1:cons\tInsert a new elements"); System.out.println("\t2:car\t\tShows the first element in the list"); System.out.println("\t3:cdr\t\tShows the rest of the element in the list"); System.out.println("\t4:exit\tEnds the program");

System.out.println();

while(x)

{

System.out.println("\nEnter the choice"); ch=Integer.parseInt(d.readLine()); switch(ch)

{

case 1: lis.cons(); break; case 2: lis.car(); break; case 3: lis.cdr();


break; case 4: x=false;

System.out.println("EXIT");

break;

default:

System.out.println("Invalid Choice");

}

}

}

}

class List

{

int in,n=0;

LinkedList l=neew LinkedList();

DataInputStream dis=new DataInputStream(System.in);

void cons()throws IOException

{

System.out.println("Enter the element to be inserted");

in=Interger.parseInt(dis.readLine());

l.addFirst(in);

n++;

System.out.println("After insertion"); System.out.println(l);

}

void car()

{

System.out.println("The first element in the list");

System.out.println(l.getFirst());

}

void cdr()

{

System.out.println("The rest of the element in the list are"); System.out.println("[");

for(int i=1;i<n;i++) System.out.println(l.get(i)+","); System.out.println("]");

}

}

Result:

Thus the program for Lisp List is implemented and executed successfully

Output

Lisp List Implementation

Option Action

1:cons Insert a new element

2:car Shows the first element in list

3:cdr Shows the rest of elements in list

4:exit End the Program

Enter your choice:1

Enter the Element to be Inserted:

5

After insertion

[5]

Enter your choice:1

Enter the Element to be inserted:

8

After insertion

[8 5]

Enter your choice:2

The first element is

8

Enter your choice:3

The rest of the elements are

[5]

Enter your choice:4

Exiting……

Ex. No: Currency Conversion

Aim:

To write a program for conversion of Currency ie from Rupee to Dollar

Algorithm:

Step 1:Start the program

Step 2:Define a class Rupee implementing the class Serializable

Step 3:In it define a method getRupee() to get the Rupee value to be Stored Step 4:Define another method to Display ie String toString of Serializable Step5:Define another class Dollar implementing Serializable

Step 6:In it define method getDollar() to get the value of Dollar to be stored Step 7:Define another method to Display ie String toString of Serializable Step 8:Define the class currency with main()

Step 9:Create object for each Dollar and Rupee class

Step 10: Using the Serializable class method calculate and display the equivalent value

Step 11:Stop the program

Program:

import java.io.*;

class Rupee implements Serializable

{

String rs;

public void getRupee()throws IOException

{

DataInputStream dis1=new DataInputStream(System.in);

System.out.println("Enter the rs. to store");

rs=dis1.readLine();

}

public String toString()

{

return "Enter Rupee value="+rs;

}

}

class Dollar implements Serializable

{

String dl;

int d;

public void getDollar()throws IOException

{

DataInputStream dis2=new DataInputStream(System.in); System.out.println("Enter the $ to store:"); dl=dis2.readLine();

d=Integer.parseInt(dl);

}

public String toString()

{

return "Entered Doller value="+dl+"and Eqivalent Rupee value="+d*50;

}

}

class Currency

{

public static void main(String args[])throws IOException

{

Rupee rin=new Rupee();

rin.getRupee();

Dollar din=new Dollar();

din.getDollar();

try

{

FileOutputStream fos=new FileOutputStream("file1.dat"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(rin);

oos.flush();

oos.writeObject(din); oos.flush(); oos.close();

}

catch(Exception e)

{

System.out.println("Exception during serialization:"+e); System.out.println(0);

}

Rupee rout;

Dollar dout;

try

{

FileInputStream fis=new FileInputStream("file1.dat");

ObjectInputStream ois=new ObjectInputStream(fos); rout=(Rupee)ois.readObject(); System.out.println(rout); dout=(Dollar)ois.readObject(); System.out.println(dout);

ois.close();

}

catch(Exception e)

{

System.out.println("Exception during deserialization:"+e);

System.out.println(0);

}

}

}

Result:

Thus the program for currency conversion is implemented and executed successfully

Output:

Enter the Rs to store:

12

Enter the $ to store:

50

Entered Rupee value=12

Entered Dollar value=50

Equivalent value=2500

Ex. No: Packages and JavaDoc

Aim:

To develop a Java package with simple Stack and Queue classes and Use JavaDoc comments for documentation.

Algorithm:

Step 1. Start

Step 2. Create two classes, Stack and Queue.

Step 3. Add the line, package ckage_name>, in the first line of both classes. Step 4. Save the .java files of the classes in a folder with the same name as

package_name.

Step 5. Create objects using main program for both Stack and Queue classes in separate files, import the package in the main as import package_name.class_name; and save them in the same path as that of the package.

Step 6. Add comments to the classes in the package using Java doc

Step 7. Set the path variable to the path where execution will happen.

Step 8. Change the directory to package_name.

Step 9. Compile the classes. The .class file must be in the same location as that of the

.java file

Step 10. Go to bin dir and compile the main program

Step 11. To view the Javadoc , javadoc –d ir name> ckage_name> Step 12. A html file for the Java documentation will be created.

Step 13. Stop.

Program:

/*Queue.java*/

package pack;

/**

Queue with insert and delete operations

@param otherPerson The person being compared to the calling object.

@return Returns true if the calling object equals otherPerson.

/

public class Queue

{

int queue[]=new int[10];

int foq,roq;

public Queue()

{

foq=-1;

roq=-1;

}

public void ins(int item)

{

if(foq==9 && roq==9) System.out.println("Queue is full"); else if(foq==-1 && roq==-1)

{

queue[++roq]=item;

++foq;

}

else queue[++roq]=item;

}

public void del()

{

int k;

if(foq==-1)

System.out.println("Q is empty");

else

{

k=queue[foq++]; System.out.println("Deleted value:"+k);

}

}

}

/*Stack.java*/

/** This class defines an integer stack that can hold 10 values.*/

package pack;

public class Stack {

int stck[] = new int[10];

int tos;

// Initialize top-of-stack public Stack() {

tos = -1;

}

// Push an item onto the stack public void push(int item) { if(tos==9) System.out.println("Stack is full.");

else

stck[++tos] = item;

}

// Pop an item from the stack public int pop() {

if(tos < 0) {

System.out.println("Stack underflow.");

return 0;

}

else

return stck[tos--];

}

}

/*Qpackage.java*/ import pack.Queue; class Qpackage

{

public static void main(String args[])

{

Queue q1=new Queue();

// Add some numbers onto the Queue for(int i=0; i<10; i++)

q1.ins(i);

// Add those numbers off the Queue

for(int i=0; i<10; i++)

q1.del();

}

}

/*Spackage.java*/ import pack.Stack; class StackPackage

{

public static void main(String args[])

{

Stack mystack1 = new Stack(); Stack mystack2 = new Stack();

// push some numbers onto the stack for(int i=0; i<10; i++)

mystack1.push(i); for(int i=10; i<20; i++) mystack2.push(i);

// pop those numbers off the stack

System.out.println("Stack in mystack1:");

for(int i=0; i<10; i++) System.out.println(mystack1.pop());

System.out.println("Stack in mystack2:");

for(int i=0; i<10; i++)

System.out.println(mystack2.pop());

}

}

Result:

Thus a package was created with Stack and Queue classes and comments were added using

Javadoc and the package was executed successfully.

Output:

C:\Program Files\Java\jdk1.6.0\bin\pack>javac Queue.java

C:\Program Files\Java\jdk1.6.0\bin\pack>cd.. C:\Program Files\Java\jdk1.6.0\bin>javac package.java

C:\Program Files\Java\jdk1.6.0\bin>java Qpackage

Deleted value:0

Deleted value:1

Deleted value:2

Deleted value:3

Deleted value:4

Deleted value:5

Deleted value:6

Deleted value:7

Deleted value:8

Deleted value:9

Ex. No: Number of Active Objects

Aim:

To design a class for Complex numbers in Java with all methods for basic Operations on complex numbers and also with a method that returns the number of active Objects created.

Algorithm:

Step 1. Start

Step 2. Create a class ComplexNumber with an empty constructor and another constructor with values.

Step 3. Add static variable count and initialize it to 0.

Step 4. Add functions for addition, subtraction, multiplication and division of two objects.

Step 5. this pointer points to the active object.

Step 6. Create 2 objects by passing values.

Step 7. Get the choice from the user. 1 for Addition, 2 for Subtraction, 3 for Division and 4 for Multiplication.

Step 8. Pass this input to a switch case block where the corresponding functions can be called.

Step 9. Print the number of objects created by printing the count value.

Step 10. Stop.

Program:

import java.io.*;

import java.lang.*;

class ComplexNumber {

public double a , b;

public static int count = 0;

//complex number: a + bi

public ComplexNumber() {

this.a = 0;

this.b = 0;

}

public ComplexNumber(int a, int b) {

this.a = a; this.b = b; count++;

}

public void add(ComplexNumber c2) {

this.a += c2.a;

this.b += c2.b;

}

public void subtract(ComplexNumber c2) {

this.a -= c2.a;

this.b -= c2.b;

}

public void divide(ComplexNumber c2) {

double c = this.a;

this.a = (this.a * c2.a + this.b * c2.b)/(c2.a * c2.a + c2.b * c2.b);

this.b = (c * c2.b*-1+ this.b * c2.a)/(c2.a * c2.a + c2.b * c2.b);

}

public void multiply(ComplexNumber c2) {

double c = this.a;

this.a = this.a * c2.a - this.b * c2.b;

this.b = c * c2.b + this.b - c2.a;

}

public String toString1(){

return a + " + " + b + "i";

}

}

class comp

{

public static void main(String args[])throws IOException

{

ComplexNumber c1=new ComplexNumber(3,4); ComplexNumber c2=new ComplexNumber(1,5); DataInputStream in = new DataInputStream (System.in);

System.out.println("Enter 1. Addition 2.Subtraction 3.Division 4.Multiplication:"); String numberip = in.readLine();

int i = Integer.parseInt(numberip);

String a;

switch (i)

{

case 1: c1.add(c2); a=c1.toString1(); System.out.println(a); break;


case 2: c1.subtract(c2); a=c1.toString1(); System.out.println(a); break;

case 3: c1.divide(c2); a=c1.toString1(); System.out.println(a); break;

case 4: c1.multiply(c2); a=c1.toString1(); System.out.println(a); break;

default:

System.out.println("Unknown object!");

break;

}

System.out.println("No of active objects in this program"); System.out.println(c1.count);

}

}

Result:

Thus a class for Complex numbers with all methods for basic operations on complex numbers and also with a method that returns the number of active objects was created and executed successfully.

Output:

C:\Program Files\Java\jdk1.6.0\bin>javac comp.java Note: comp.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

C:\Program Files\Java\jdk1.6.0\bin>java comp

Enter 1. Addition 2.Subtraction 3.Division 4.Multiplication:

1

4.0 + 9.0i

No of active objects in this program

2

C:\Program Files\Java\jdk1.6.0\bin>java comp

Enter 1. Addition 2.Subtraction 3.Division 4.Multiplication:

2

2.0 + -1.0i

No of active objects in this program

2

C:\Program Files\Java\jdk1.6.0\bin>java comp

Enter 1. Addition 2.Subtraction 3.Division 4.Multiplication:

3

0.8846153846153846 + -0.4230769230769231i

No of active objects in this program

2

C:\Program Files\Java\jdk1.6.0\bin>java comp

Enter 1. Addition 2.Subtraction 3.Division 4.Multiplication:

4

-17.0 + 18.0i

No of active objects in this program

2

C:\Program Files\Java\jdk1.6.0\bin>


Remaining programs is continued in Part 2

No comments:

Post a Comment

Refer this site 2 ur frndz