File

advertisement
Department of Information Technology
IT2305 JAVA PROGRAMMING LAB
LAB MANUAL
OXFORD Engineering COLLEGE
Pirattiyur, Trichy-620 009.
List of Experiments:
1. Develop a Java package with simple Stack and Queue classes. Use JavaDoc
comments for documentation.
2. Design a class for Complex numbers in Java. In addition to methods for basic
operations on complex numbers, provide a method to return the number of active
objects created.
3. Design a Date class similar to the one provided in the java.util package.
4. Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle,
Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate
dynamic polymorphism.
5. Design a Java interface for ADT Stack. Develop two different classes that implement
this interface, one using array and the other using linked-list. Provide necessary
exception handling in both the implementations.
6. Write a Java program to read a file that contains DNA sequences of arbitrary length
one per line (note that each DNA sequence is just a String). Your program should
sort the sequences in descending order with respect to the number of 'TATA'
subsequences present. Finally write the sequences in sorted order into another file.
7. Develop a simple paint-like program that can draw basic graphical primitives in
different dimensions and colors. Use appropriate menu and buttons.
8. Develop a scientific calculator using even-driven programming paradigm of Java.
9. Develop a template for linked-list class along with its methods in Java.
10. Design a thread-safe implementation of Queue class. Write a multi-threaded
producer-consumer application that uses this Queue class.
11. Write a multi-threaded Java program to print all numbers below 100,000 that are
both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a
thread that generates prime numbers below 100,000 and writes them into a pipe.
Design another thread that generates fibonacci numbers and writes them to another
pipe. The main thread should read both the pipes to identify numbers common to
both.
12. Develop a multi-threaded GUI application of your choice.
Ex.No:1
JAVA PACKAGE WITH SIMPLE STACK AND QUEUE CLASS
AIM:
To write program to develop a java package for the stack and queue classes.
ALGORITHM:
Stack class:
1. Start the program.
2. Create a class with the class name Stack to demonstrate the application of the stack.
3. Create a constructor to initialize the top value of the stack.
4. Define the method push ().
5. If the top value is equal to 9, print stack is full. Otherwise push the value in to the stack.
6. Define the method pop ().
7. If the top value is less than zero, print stack underflow. Otherwise decrement the value of the
stack.
8. Create a main class with the class name teststack.
9. Create an object for the class Stack to call the methods define inside that class.
10. Call the methods push () & pop () using the objects.
11. Stop the program.
Queue class:
1. Start the program.
2. Create a class with the name queue implement to demonstrate about a application of queue.
3. Declare a variable str as string and num as integer.
4. Create an object for a class queue implement.
5. Create a constructor to of the Linked List class. This class is used by importing the java.util.
package. This constructor is used for constructing an empty list.
6. Get the value from the command line by using a variable str.
7. Using try and catch block to handle exception using IOException.
8. Print the elements in the queue using the statement.
9. Stop the program.
OUTPUT:
Stack class:
E:\SQ>path=C:\Program Files\Java\jdk1.5.0\bin
E:\SQ>set path=.;E:\;
E:\SQ>javac TestStack.java
E:\SQ>java SQ.TestStack
Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2
19
18
17
16
15
14
13
12
11
10
Queue class:
E:\SQ>javac 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 :
46
Result:
Thus the Stack and queue program was compiled and executed successfuly.
Ex.No:2
COMPLEX NUMBER MANIPULATION
AIM:
To write a program to perform addition, subtraction, multiplication in complex numbers
using constructors.
ALGORITHM:
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: Define static method named as addition, subtraction,multiplication to perform particular
function defined inside the method.
6: Create an object for the class Complex Number and pass the values in the argument list.
7: Call the method by using object to get the values.
8: Print the result.
9: Stop the program.
OUTPUT:
E:\Javaexecute>javac ComplexNumber.java
E:\Javaexecute>java ComplexNumber
-2-3i
-4-5i
Addition of both Complex Numbers are :-6-8i
Substraction of both Complex Numbers are :2+2i
Multiplication of both Complex Numbers are :-7+22i
Result:
Thus the complex number program was executed and compiled successfully.
Ex.No:3
DATE CLASS SIMILAR TO JAVA.UTIL PACKAGE
AIM:
To write a java program to display the dates of before and after days for a given date.
ALGORITHM:
1. Start the program.
2. Create a class with name Date
3. Declare the variables month, day, year as private and create constructor with arguments.
4. Declare an abstract method area() inside the class.
5. Create the classes Rectangle, Triangle, Circle, and Ellipse to find the area.
6. Define abstract method area() inside the subclasses and call the constructor of class Shape
using super keyword.
7. In main(), create the objects for all classes and pass values to fields of constructors.
8. Create a reference variable figuref for abstract class.
9. Using reference variable of class Shape, call the method area() of all subclasses
10. Print the areas for all shapes.
11. Stop the program.
OUTPUT:
E:\SQ>java AbstractAreas
Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
Inside Area for Circle.
Area is 78.5
Inside Area for Ellipse.
Area is 153.86
Inside Area for Square.
Area is 36.0
Result:
Thus the Dynamic polymorphism was compiled and executed successfuly.
Ex.No:5
JAVA INTERFACE FOR ADT STACK
AIM:
To write the java program for ADT stack using interface.
ALGORITHM:
1: Start the program
2: Create an interface which consists of three methods namely PUSH, POP and DISPLAY
3: Create a class which implements the above interface to implement the concept of stack
through Array
4: Define all the methods of the interface to push any element, to pop the to element and to
display the elements present in the stack.
5: Create another class which implements the same interface to implement the concept of stack
through linked list.
6: Repeat STEP 4 for the above said class also.
7: In the main class, get the choice from the user to choose whether array implementation or
linked list implementation of the stack.
8: Call the methods appropriately according to the choices madeby the user in the previous
step.
9: Repeat step 6 and step 7 until the user stops his/her execution.
10: Stop the program
OUTPUT:
E:\SQ>java Stackadt
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
1
enter element
4
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
1
enter element
5
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
1
enter element
7
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
4
elements are 4 5 7
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
1
enter element
8
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
2
popped element8
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
4
elements are 4 5 7
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
3
popped element7
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
4
elements are 4 5 7
enter ur choice for 1.push 2.pop 3.peek 4.display 5.exit
5
Result:
Thus the Interface for ADT stack program was compiled and executed successfully.
Ex. No: 6
DNA File Creation
Aim:
To write a Java program to read a file that contains DNA sequences of arbitrary length
one per line ,sort the sequences in descending order with respect to the number of 'TATA'
subsequences present and finally write the sequences in sorted order into another file.
Algorithm:
1. Start
2. Create an input text file “DNA.txt” and add the sequence as needed.
3. Read each line from the file to a string array.
4. Count the number of TATA sequence in each line and store the count in another array.
5. Arrange the data in the count array in descending order and also arrange the data in the
string array in parallel with the count array.
6. Write the sorted DNA sequence to the output file “File.txt”
7. View the output file.
8. Stop.
Input:
/*dna.txt*/
attaaattaaaaaaaattata
taaattaataaattagaattaaa
aaaaaaacatcgtaaa
tcaatatatataat
ctaaaataaac
gataaaaacaaataa
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac FileDemo.java
C:\Program Files\Java\jdk1.6.0\bin>java FileDemo
Reading data from DNA file.............
Swapping data from DNA file.............
Writing Sorted DNA sequence to Output file.................
Writing of Sorted DNA sequence to Output file has been completed................
/*File.txt*/
Tcaatatatataat
attaaattaaaaaaaattata
aaaaaaacatcgtaaa
taaattaataaattagaattaaa
ctaaaataaac
gataaaaacaaataa
Result:
Thus the DNA sequences were read sorted and output was given to an output file.
Ex.No:7
DEVELOPING A SIMPLE PAINT LIKE PROGRAM USING APPLET
Aim:
To develop a simple paint like program using applet in java.
Algorithm:
1. Import the required class and packages.
2. Create a class Drawtest for creating an applet.
3. Initialize panles and controls in the init() method.
4. Define the destroy() method to destroy the same.
5. Create an instance for draw test class and call the init(0 and start() methods in the main
method.
6. Add a new frame to the applet window and resize it to 300x300.
7. Declare a method get applet infor to display the applet information.
8. Declare two constants LINES,POINTS which are going to be the modes.
9. Define the paint method and perfoem the required operations.
10.Display the result according to the mode selected.
OUTPUT:
Result:
Thus the Applet-Paint program was compiled and executed successfully.
Ex.No:8
DEVELOPING A SCIENTIFIC CALCULATOR
Aim:
To develop a scientific calculator in java.
Algorithm:
1.Import the necessary packages.
2. Create a class calculator JFrame class and ActionListener interface.
3. Create a new font using String name,int style,int size).
4. Set up the JMenu bar and add Mnemenics to them by using setMnemonic() method.
5. Set the frame layout for the applet and set the background color to gray using
setBackground() method .
6. Create a container using JPanel().
7. Create numeric JButtons for operators and numbers using JButtons().
8. Set all the Numbered JButtons to blue .the rest to red usig setForeGround() method.
9. Declare the method getNumberInDisplay() to get by the calculator.
10.Use display result to display the method.
11.Declare the main function and create the instance for calculator and set the attributes
setTitle(),setSize(),setResizable().
12.Stop the program.
OUTPUT:
Result:
Thus the Scientific calculator program was compiled and executed successfully.
Ex.No:9
DEVELOPING A TEMPLATE FOR LINKED LIST
Aim:
To develop a template for linked list in java.
Algorithm:
1. Import the necessary packages.
2. Declare the main function within the class Linked list demo.
3. Insert items in to the list using add() method use addLast() to isert at the end.
4. Display the contents of the lists.
5. Use remove() method to declare a specific element.
6. Use remove() first(0,removeLast() to delete the element at the first and at the last.
7. Display the lsit and hadle the exceptions .
OUTPUT:
E:\SQ>java LinkedListDemo
Original contents of l1:[F, A2, B, D, E, C, Z, A]
Contents of l1 after deflection: [A2, B, E, C, Z, A]
l1 after deleting first and last: [B, E, C, Z]
l1 after change: [B, E, CChanged, Z]
Result:
Thus the Linked list program was compiled and executed successfully.
Ex.No:10
DEVELOP A MULTI THREADED PRODUCER CONSUMER APPLICATION
Aim:
To develop a multithreaded producer and consumer application in java.
Algorithm:
1. Import the necessary packages.
2. Create two threads producer and consumer.
3. Declare a constant MAXQUEUE in producer thread.
4. Create an instance for vector named messages to implement a queue.
5. In the run() method of producer thread put the messages on the queue using putmessages()
and suspend the thread for one sec using sleep(1000).
6. Declare the getmessage() method and check whether there is any message if no wait for
one.
7. After receiving the message remove the message and return it.
8. Create a constructor to assign values to them.
9. In the run method of consumer class call the getmessage() method of producer thread and
print the message.
10.Suspend the thread for 2 sec using sleep(2000).
11.Create two instances with different name for consumer thread.
12.Display the message.
OUTPUT:
E:\SQ>java Consumer
Two got message: Wed Sep 01 11:57:54 GMT+05:30 2010
One got message: Wed Sep 01 11:57:55 GMT+05:30 2010
Two got message: Wed Sep 01 11:57:56 GMT+05:30 2010
One got message: Wed Sep 01 11:57:57 GMT+05:30 2010
Two got message: Wed Sep 01 11:57:58 GMT+05:30 2010
One got message: Wed Sep 01 11:57:59 GMT+05:30 2010
Two got message: Wed Sep 01 11:58:00 GMT+05:30 2010
One got message: Wed Sep 01 11:58:01 GMT+05:30 2010
Two got message: Wed Sep 01 11:58:02 GMT+05:30 2010
One got message: Wed Sep 01 11:58:03 GMT+05:30 2010
One got message: Wed Sep 01 11:58:04 GMT+05:30 2010
Two got message: Wed Sep 01 11:58:18 GMT+05:30 2010
One got message: Wed Sep 01 11:58:19 GMT+05:30 2010
Two got message: Wed Sep 01 11:58:20 GMT+05:30 2010
One got message: Wed Sep 01 11:58:21 GMT+05:30 2010
Two got message: Wed Sep 01 11:58:22 GMT+05:30 2010
Result:
Thus the Multithreaded program was compiled and executed successfully.
Ex.No:11
GENERATING PRIME NUMBERS AND FIBONACCI SERIES
Aim:
To generate prime numbers and fibonacci series using java.
Algorithm:
1. Import the necessary packages.
2. Create tow threads MyThread1 and MyThread2.
3. In MyThread1 create instances for piped reader and piped writer.
4. Create a constructor and assign values to them.
5. In mythread2 create instances for pipedreader and piped writer.
6. Create a constructor and assign values to them.
7. Declare a class Multithreaded programming and create two list using ArrayList.
8. Create instances for pipedreader and pipedwriter prl and pwl respectively.
9. Get the Fibonacci series from the stream pr2 and store them in lsit2 by using retainAll()
method and print them.
OUTPUT:
E:\SQ>java MultithreadedProgram
Prime Numbers:
2
3
5
7
Fibonacci Numbers:
1
2
3
5
8
13
21
34
55
Elements common to both lists are:
2
3
5
Result:
Thus the program was compiled and executed successfully.
Ex.No:12
MULTITHREADED GUI APPLICATION
Aim:
To develop a multithreaded GUI application in java.
Algorithm:
1. Create the class ball which implements a ball which moves and bounces of the edges of a
rectangle.
2. Using the move() method move the ball to the nect postion by reversing the direction if it bits
one of the edges.
3. Get the shapes of the ball at its current position.
4. Invoke the runnable() method to show the animated bouncing balls.
5. Using the sleep() method assign the time for ball to bounce in a given interval of time.
6. Construct the frame with the component for showing bouncing ball and start and close
buttons.
7. Add the thread to the frame and button to the container.
8. Create a constructor to assign values to them.
9. Add a bouncing ball to the canvas and starts a thread to make it bounce.
10.Display the output.
OUTPUT:
Result:
Thus the Multithreaded GUI application was compiled and executed successfully.
Download