You are on page 1of 8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1

Home

Technical Studies

Aptitude

Video

Online Gov Portals

Project

Lab Manuals

Salutary Links
Basics and Viva

Interview Questions
E-Books

Research

Question Bank
Softwares

Notes & for UG and PG


UGC

More Jobs

Soft Skills

IEEE & Elsevier Pape

Contact Us

Dharanyadevi blogspot subsume with E-books, Notes, La

SEARCH
Search

Like

Share 133peoplelikethis.

About Me
Dharanya
Villupuram, Hindu, India
Working as a lecturer in Anna
University, Villupuram!!!
http://annauniv.academia.edu/Dha
ranyadevi
View my complete profile

Showing posts with label Java Programming Lab Part 1. Show all posts
Thursday, August 30, 2012

JavaProgrammingLabPart1
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

Is This Website Is
Informative????

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
{

http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

Follow Us :)!!!

Jointhissi

withGoogleFriend

Members

Alreadyamember?

(SS) System So
Answers

.Net Assembly
Answer Part 9

.Net Deployme
and Answer Pa

1/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1
Yes
No
NeedToImprove
Satisfactory
Youmayselectmultipleanswers.
Vote Showresults
Votessofar:203
Dayslefttovote:2503

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:

Follow by Email
Emailaddress...

Submit

Total Pageviews

357,079

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

Whydomenlike
intelligent
women?
Because
oppositesattract.
KathyLette
kathylettequotes

.Net Interview
Part 3
.Net Interview
Part 6
.Net Interview
Part 7
.Net Interview
Part 8
.Net Interview
.Net Interview
Basics of C

Basics of Wire
C # Interview
C Interview

C Interview Qu
(1)

C Interview Qu
(1)

C Interview Qu
(1)

C Interview Qu
(1)

C Interview Qu
(1)

C Interview Qu
5 (1)

C Program to C

CCNA InterVie
(1)

COA 2 Marks (

COA Question

Step 8:ElseIf choice is 2,call the car() function Step 9:ElseIf choice is 3,call the cdr() function

Cognizant Tes

Step 10:ElseIf choice is 4,Exit the program Step 11:Goto step 6

Computer Gra

Step 12:Stop the program

Cheeky Quotes

.Net Interview
Part 2

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();

Computer Net

Computer Net

Computer Net
Points

Computer Net

Computer Net

CS1203 -SYST
B.E/B.Tech DE
May/Jun 2007
CS1302
Bank

CS1304-System
(1)
CS1312 Object
For EEE

CS2201 Data S
(1)

CS2401 Compu
CTS aptitude.

DBMS Lab Ma

Delete Adminis

Difference Bet

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);
http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

EC2303 Comp
Organization Q

Ec2303 Compu
Organization S

EC2305 Trans
Guides 2 and 1

EC2401 Wirele
Marks Questio

ECE EC1008 H
Nov/Dec 2009

Engineering Ec
Accounting MG

Engineering Ec
Management M
Question Bank

File Copy Prog

File Manageme
File Operation

File Program in

2/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1

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

First Year Basi

First Year Basi

First Year Basi

Free Download
Manager

Free Download
5.4.0.6 Full Ver
Serial Key

Graphics prog

Graphics.h in F

Graphics.h in L

High Speed Ne
Bank

High Speed Ne

How to Create
How to install
flash Drive

Installing Wind
Windows 7 or
Interview

Interview and
Answer Part 2

Interview and
Answers Part 1

Interview Ques
IT2401

Java Programm

Java Programm

Java Programm

Java Programm

Java Revision N

JAVA Threadin
Answer
Kernel

Lab Manual: O

Latest High Pe
i7 Processor

Linear Search
Using Function

Linux Vs UNIX

MAN and WAN

Microprocesso
Marks

Network Proto
and Answers

Networking Int
Answer

Number of Lin
Display the Re

Online File Sha

OOPS LAB for

OOPS Lab Man

another method to Display ie String toString of Serializable Step 8:Define the class currency

OOPS Questio

with main()

Operating Syst

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
http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

Operating Syst

Oracle Java Ce

P2P File Sharin


Palindrome in
Function

Question Bank

Radio Frequen

Recover - (Ctr
Deleted Files

Second Year Q
Management S

Second Year Q
Engineering an

Service Orient

Shell Script to
Number is Pali

Shutdown Prog
C (1)
SOA LAB

System Softwa

3/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1

{
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();
}

Third Year Que


Cryptography

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:

Web Technolo

Third Year Que


Programming a

Use Your Pend

Viva and Basic


Operating Syst

Viva and Basic


Part 4

Viva and Basic


Part 5

Viva and Basic


Structure Part

Viva and Basic


Structures

Viva and Basic

Viva and Basic


(1)

Viva and Basic


(1)

Viva and Basic


(1)

Viva and Basic

Viva and Basic

Viva and Basic

Viva and Basic

Viva and Basic


Part 2

Web Developm

What is System
Windows Run
Keys

Wireless Comm
Question Bank

Words and Cha


(1)

Write a C Prog
of Character

XML Interview

http://www.flip

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>,inthefirstlineofbothclasses.Step4.Savethe
.javafilesoftheclassesinafolderwiththesamenameas
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

http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

4/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1
.java file
Step 10. Go to bin dir and compile the main program
Step 11. To view the Javadoc , javadoc d irname>ckage_name>Step12.Ahtmlfileforthe
Javadocumentationwillbecreated.
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();

http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

5/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1
// 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) {

http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

6/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1
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

http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

7/8

05/12/2014

"HereToHelpYou":JavaProgrammingLabPart1
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


Posted by Dharanya at 11:50 PM

No comments:

Recommend this on Google

Labels: Java Programming Lab Part 1

Home

Older Posts

Subscribe to: Posts (Atom)

Refer this site 2 ur frndz

Dharanyadevi BlogSpot. Simple template. Powered by Blogger.

http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201

8/8