Windows / Linux operating system .... JP Lab - LM. 7 c). Program: Stack class:
package SQ; import java.io.*; ... E:\SQ>path=C:\Program Files\Java\jdk1.5.0\bin.
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
IT2305 – JAVA PROGRAMMING LABORATORY LABORATORY MANUAL FOR V SEMESTER B.TECH / IT ACADEMIC YEAR: 2013 – 2014 (ODD) (FOR PRIVATE CIRCULATION ONLY) ANNA UNIVERSITY, CHENNAI.
DEPARTMENT OF INFORMATION TECHNOLOGY
DR.NAVALAR NEDUNCHEZHIAYN COLLEGE OF ENGINEERING, THOLUDUR-606303, CUDDALORE DIST.
1
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES DO’S o
Without Prior permission do not enter into the Laboratory.
o
While entering into the LAB students should wear their ID cards.
o
The Students should come with proper uniform.
o
Students should sign in the LOGIN REGISTER before entering into the laboratory.
o
Students should come with observation and record note book to the laboratory.
o
Students should maintain silence inside the laboratory.
o
After completing the laboratory exercise, make sure to shutdown the system properly.
DONT’S o Students bringing the bags inside the laboratory.. o Students wearing slippers/shoes insides the laboratory. o Students using the computers in an improper way. o Students scribbling on the desk and mishandling the chairs. o Students using mobile phones inside the laboratory. o Students making noise inside the laboratory.
2
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
HARDWARE REQUIREMENTS:
Pentium IV with 2 GB RAM, 160 GB HARD Disk, Monitor 1024* 768 color 60Hz SOFTWARE REQUIREMENTS:
Windows / Linux operating system JDK 1.6 (or above) UNIVERSITY PRACTICAL EXAMINATION
Allotment of marks Internal assessment Practical examination Total
= 20 marks = 80 marks --------------------= 100 marks ---------------------
INTERNAL ASSESSMENT (20 marks) Staff should maintain assessment register and the HOD should monitor it
Split up of Internal Marks Observation Record Note Modal Exam Attendance
= 3 marks = 7 marks = 5 marks = 5 marks --------------------= 20 marks ---------------------
Total
UNIVERSITY EXAMINATION The exam will be conducted for 100 marks. Then the marks will be calculated to 80 marks
Split up of Practical Examination Marks Aim and Procedure
=
30 marks
Program
=
30 marks
Execution
=
20 marks
Output & result
=
10 marks
Viva voce
= 10 marks --------------------= 100 marks ---------------------
Total
3
Dr.N.N.C.E
IT / V Sem
IT2305 – JAVA PROGRAMMING LABORATORY
LIST OF EXPERIMENTS 1. Java package with simple stack and queue class 2. Complex number manipulation 3. Date class similar to java.util package 4. Implementing dynamic polymorphism in java 5. Java interface for ADT stack 6. DNA file creation 7. Developing a simple paint like program using applet 8. Developing a scientific calculator 9. Developing a template for linked list 10. Develop a multi threaded producer consumer Application 11. Generating prime numbers and Fibonacci series 12. Multithreaded GUI application
4
JP Lab - LM
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
S. No
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
NAME OF THE EXPERIMENTS
JAVA PACKAGE WITH SIMPLE STACK AND QUEUE CLASS COMPLEX NUMBER MANIPULATION DATE CLASS SIMILAR TO JAVA.UTIL PACKAGE IMPLEMENTING DYNAMIC POLYMORPHISM IN JAVA JAVA INTERFACE FOR ADT STACK DNA FILE CREATION DEVELOPING A SIMPLE PAINT LIKE PROGRAM USING APPLET DEVELOPING A SCIENTIFIC CALCULATOR DEVELOPING A TEMPLATE FOR LINKED LIST DEVELOP A MULTI THREADED PRODUCER CONSUMER APLICATION GENERATING PRIME NUMBERS AND FIBONACCI SERIES MULTITHREADED GUI APPLICATION OPERATORS AND EXPRESSIONS DECISION MAKING STRUCTURES
5
Page No
CONTENTS
6 10 13 16 19 23 26 31 42 44 47 50 54 56
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
Exercise Number: 1 Title of the exercise
: JAVA PACKAGE WITH SIMPLE STACK AND QUEUE CLASS
Date of the exercise
:
OBJECTIVE (AIM) OF THE EXPERIMENT • To write program to develop a java package for the stack and queue classes. FACILITIES REQUIRED AND PROCEDURE a)
Facilities Required: S.No.
Facilities required
Quantity
1
System
1
2
O/S
3
S/W name
Windows / Linux OS JDK 1.6
b) Procedure: Step Details of the step no. STACK CLASS Start the program. 1 Create a class with the class name Stack to demonstrate the application of the stack. 2 Create a constructor to initialize the top value of the stack. 3 Define the method push (). 4 If the top value is equal to 9, print stack is full. Otherwise push the value in to the 5 stack. Define the method pop (). 6 If the top value is less than zero, print stack underflow. Otherwise decrement the 7 value of the stack. Create a main class with the class name teststack. 8 Create an object for the class Stack to call the methods define inside that class. 9 Call the methods push () & pop () using the objects. 10 Stop the program. 11 QUEUE CLASS Start the program. 1 Create a class with the name queue implement to demonstrate about a application of 2 queue. Declare a variable str as string and num as integer. 3 Create an object for a class queue implement. 4 Create a constructor to of the Linked List class. This class is used by importing the 5 java.util. package. This constructor is used for constructing an empty list. Get the value from the command line by using a variable str. 6 Using try and catch block to handle exception using IOException. 7 Print the elements in the queue using the statement. 8 Stop the program. 9 6
Dr.N.N.C.E
IT / V Sem
c) Program: Stack class: package SQ; import java.io.*; class Stack{ int stck[]=new int[10]; int tos; Stack(){ tos=-1;} void push(int item) { if(tos==9) System.out.println("Stack is full"); else stck[++tos]=item; } int pop(){ if(tosjavac QueueImplement.java E:\SQ>java SQ.QueueImplement Enter number of elements : 4 Enter elements : 2 4 6 8 First element :2 Last element :8 Rest elements in the list : 4 6 d) Result: Thus the Stack and queue program was compiled and executed successfuly. VIVA – QUESTIONS AND ANSWERS: 1. What is difference between Stack and Queue in java ? S.NO
Stack
Queue
1.
A Stack represents a Collection of objects that
A Queue is also a Collection of Objects
are in LIFO (Last In First Out Order).
similar to a Stack.
The Stack class provides operations that allow
Queues typically order the elements
testing for zero elements, inspection of it's top
contained within in FIFO order but this
most element, removal of it's top most element,
is not always the case.
2.
and the addition of elements.
2. What is Stack? A Stack is a first-in, last-out data structure that pops elements in the opposite order than they were pushed. By default, a Stack uses an Array for its internal storage, although this can easily be changed. 3. What is queue? A Queue is a first-in, first-out data structure that pops elements in the same order than they were pushed. By default, a Queue uses an SList for its internal storage, although this can easily be changed.
9
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
Exercise Number: 2 Title of the exercise : COMPLEX NUMBER MANIPULATION Date of the exercise : OBJECTIVE (AIM) OF THE EXPERIMENT •
To write a program to perform addition, subtraction, multiplication in complex numbers using constructors.
FACILITIES REQUIRED AND PROCEDURE a) Facilities Required: S.No.
Facilities required
Quantity
1
System
1
2
O/S
3
S/W name
Windows / Linux OS JDK 1.6
b) Procedure: Step no.
Details of the step
1
Start the program
2
Create a class with a class name Complex Number.
3
Create a constructor with the arguments a and b with integer data type.
4
Define a method getcomplexvalue( ) to get the values of a and b.
5
7
Define static method named as addition, subtraction,multiplication to perform particular function defined inside the method. Create an object for the class Complex Number and pass the values in the argument list. Call the method by using object to get the values.
8
Print the result.
9
Stop the program.
6
c) Program:/ import java.io.*; public class ComplexNumber { private int a; private int b; public ComplexNumber(){ 10
Dr.N.N.C.E IT / V Sem JP Lab - LM } public ComplexNumber(int a, int b){ this.a =a; this.b=b; } public String getComplexValue(){ if(this.b < 0){ return a+""+b+"i"; } else{ return a+"+"+b+"i";} } public static String addition(ComplexNumber num1, ComplexNumber num2){ int a1= num1.a+num2.a; int b1= num1.b+num2.b; if(b1= bounds.getMaxX()) { x = bounds.getMaxX() - XSIZE; dx = -dx; } if (y < bounds.getMinY()) { y = bounds.getMinY(); dy = -dy; } if (y + YSIZE >= bounds.getMaxY()) { y = bounds.getMaxY() - YSIZE; dy = -dy; } } public Ellipse2D getShape() { return new Ellipse2D.Double(x, y, XSIZE, YSIZE); } private static final int XSIZE = 15; private static final int YSIZE = 15; private double x = 0; private double y = 0; private double dx = 1; private double dy = 1; } class BallComponent extends JComponent { public void add(Ball b) { balls.add(b);} public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (Ball b : balls) { g2.fill(b.getShape()); } } private ArrayList balls = new ArrayList(); } public class BounceThread { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() 51
JP Lab - LM
Dr.N.N.C.E IT / V Sem { public void run() { JFrame frame = new BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);} }); }} class BallRunnable implements Runnable { public BallRunnable(Ball aBall, Component aComponent) { ball = aBall; component = aComponent; } public void run(){ try { for (int i = 1; i javac AreaRect.java D:\java>java AreaRect 12 6 length of rectangle = 12 breadth of rectangle = 6 area of rectangle= 72 d) Result: Thus the java program to find the area of rectangle was successfully executed and verified. VIVA – QUESTIONS AND ANSWERS: 1. What is a ternary operator? The operator that takes three arguments is called as ternary operator. The conditional operators is the only ternary operator available in java. 2. What is the use of Integer.parseInt() method? It is used to convert the string object into integer value.
55
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
Exercise Number: 14 Title of the exercise
: DECISION MAKING STRUCTURE
Date of the exercise : OBJECTIVE (AIM) OF THE EXPERIMENT •
To write a java program to read an integer and find whether the number is odd or even.
FACILITIES REQUIRED AND PROCEDURE a) Facilities Required: S.No.
Facilities required
Quantity
1
System
1
2
O/S
3
S/W name
Windows / Linux OS JDK 1.6
b) Procedure: Step no.
Details of the step
1
Start the program.
2 3
Create a class with the class name oddt to demonstrate the application of the decision making structure. Create a constructor for initialization.
4
Initialize the parseInt value to convert the string object into integer value.
5
Print the output value.
6
Stop the program.
c) Program: import java.io.*; class odd { public static void main(String args[])throws IOException { BufferedReader din = new BufferedReader(new InputStreamReader(System.in)); int n; system.out.println(“enter a number:”); 56
Dr.N.N.C.E n= Integer.parseInt(din.readLine());
IT / V Sem
JP Lab - LM
if(n%2==0) system.out.println(n+”is an even no”); else system.out.println(n+”is an odd no:”); } } OUTPUT: D:\java>javac odd.java D:\java>java odd Enter a no: 67 67 is an odd no d) Result: Thus the java program to read an integer and find whether the number is odd or even was successfully executed and verified. VIVA – QUESTIONS AND ANSWERS: 1. What are decision making statements? The statements that are used to execute only a block of code and leaving others based on the condition. 2. What are the various decision making statements available in java? Simple if, if-else, nested if..else, if else ladder and switch statement.
57
Dr.N.N.C.E
IT / V Sem
JP Lab - LM
IT2305 – JAVA PROGRAMMING LABORATORY MODEL QUESTION SET 1. Create Java package with simple stack and queue class 2. Write a Java program to perform Complex number manipulation 3. Write a Java program for Date class similar to java.util package 4. Write a Java program for Implementing dynamic polymorphism in java 5. Write a Java program for ADT stack using Java interface 6. Write a Java program for DNA file creation 7. Develop a simple paint like program using applet 8. Develop a scientific calculator using java 9. Developing a template for linked list 10. Develop a multi threaded producer consumer Application 11. Write a Java program for generating prime numbers and Fibonacci series 12. Write a Java program for Multithreaded GUI application
58