Ex. No: Paint Program
Aim:
To develop a simple paint-like program that can draw basic graphical primitives in different dimensions and colors using java.
Algorithm:
Step 1. Start
Step 2. Create a class GraphicsProgram that extends Canvas. Step 3. Create an object for GraphicsProgram.
Step 4. Create a Frame for Frame class.
Step 5. Paint the shapes needed in the frame using paint function. Step 6. View the output.
Step 7. Stop.Program:
/*GraphicsProgram.java*/
import java.awt.*;
class GraphicsProgram extends Canvas
{
public GraphicsProgram()
{
setSize(200, 200);
setBackground(Color.white);
}
public static void main(String[] argS)
{ //GraphicsProgram class is now a type of canvas //since it extends the Canvas class //lets instantiate it
GraphicsProgram GP = new GraphicsProgram();
//create a new frame to which we will add a canvas
Frame aFrame = new Frame(); aFrame.setSize(300, 300);
//add the canvas aFrame.add(GP); aFrame.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.blue); g.drawLine(30, 30, 80, 80); g.drawRect(20, 150, 100, 100); g.fillRect(20, 150, 100, 100); g.fillOval(150, 20, 100, 100);
}
}
Result:
Thus a simple paint-like program that can draw basic graphical primitives in different dimensions and colors using java was developed.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac GraphicsProgram.java
C:\Program Files\Java\jdk1.6.0\bin>java GraphicsProgram
Ex. No: Template for Linked List
Aim:
To develop a template for linked-list class along with its methods in Java.
Algorithm:
Step 1. Start
Step 2. Create a class ListTest.
Step 3. Create a template for the Linked list. Step 4. Add elements to the string.
Step 5. Print the elements.
Step 6. Delete the elements from the linked list. Step 7. And repeat the operations as needed.
Step 8. View the output.
Step 9. Stop
Program:
//ListTest.java
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListTest
{
private static final String colors[] = { "black", "yellow", "green", "blue", "violet", "silver" };
private static final String colors2[] = { "gold", "white",
"brown", "blue", "gray", "silver" };
// set up and manipulate LinkedList objects public ListTest()
{
List< String > list1 = new LinkedList< String >(); List< String > list2 = new LinkedList< String >();
// add elements to list link for ( String color : colors )
list1.add( color );
// add elements to list link2 for ( String color : colors2 )
list2.add( color );
list1.addAll( list2 ); // concatenate lists list2 = null; // release resources printList( list1 ); // print list1 elements
convertToUppercaseStrings( list1 ); // convert to upper case string printList( list1 ); // print list1 elements
System.out.print( "\nDeleting elements 4 to 6..." ); removeItems( list1, 4, 7 ); // remove items 4-7 from list printList( list1 ); // print list1 elements printReversedList( list1 ); // print list in reverse order
} // end ListTest constructor
// output List contents
public void printList( List< String > list )
{
System.out.println( "\nlist: " );
for ( String color : list ) System.out.printf( "%s ", color );
System.out.println();
} // end method printList
// locate String objects and convert to uppercase
private void convertToUppercaseStrings( List< String > list )
{
ListIterator< String > iterator = list.listIterator();
while ( iterator.hasNext() )
{
String color = iterator.next(); // get item
iterator.set( color.toUpperCase() ); // convert to upper case
} // end while
} // end method convertToUppercaseStrings
// obtain sublist and use clear method to delete sublist items private void removeItems( List< String > list, int start, int end )
{
list.subList( start, end ).clear(); // remove items
} // end method removeItems
// print reversed list
private void printReversedList( List< String > list )
{
ListIterator< String > iterator = list.listIterator( list.size() ); System.out.println( "\nReversed List:" );
// print list in reverse order
while ( iterator.hasPrevious() ) System.out.printf( "%s ", iterator.previous() );
} // end method printReversedList
public static void main( String args[] )
{
new ListTest();
} // end main
} // end class ListTest
Result:
Thus a template for linked-list class along with its methods in Java was developed.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac ListTest.java
C:\Program Files\Java\jdk1.6.0\bin>java ListTest list:
black yellow green blue violet silver gold white brown blue gray silver list:
BLACK YELLOW GREEN BLUE VIOLET SILVER GOLD WHITE BROWN BLUE GRAY SILVER
Deleting elements 4 to 6... list:
BLACK YELLOW GREEN BLUE WHITE BROWN BLUE GRAY SILVER
Reversed List:
SILVER GRAY BLUE BROWN WHITE BLUE GREEN YELLOW BLACK
Ex. No: Multi-threaded Producer-Consumer application
Aim:
To design a thread-safe implementation of Queue class and develop a multi-threaded producer-consumer application that uses this Queue class.
Algorithm:
Step 1. Start
Step 2. Create a class Order that prints the order of the order.
Step 3. Create a class WaitPerson that waits and prints the order taken by the waitperson and prints the order number taken by the Waitperson.
Step 4. Create a class Restaurant that accepts an object from both Restaurant and
WaitPerson and provides service to the order number.
Step 5. View the output.
Step 6. Stop.
Program:
class Order {
private static int i = 0;
private int count = i++;
public Order() {
if (count == 10) {
System.out.println("Out of food, closing"); System.exit(0);
}
}
public String toString() {
return "Order " + count;
}
}
class WaitPerson extends Thread {
private Restaurant restaurant;
public WaitPerson(Restaurant r) {
restaurant = r;
start();
}
public void run() {
while (true) {
while (restaurant.order == null)
synchronized (this) {
try {
wait();
\
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Waitperson got " + restaurant.order);
restaurant.order = null;
}
}
}
class Chef extends Thread {
private Restaurant restaurant;
private WaitPerson waitPerson;
public Chef(Restaurant r, WaitPerson w) {
restaurant = r;
waitPerson = w;
start();
}
public void run() {
while (true) {
if (restaurant.order == null) { restaurant.order = new Order(); System.out.print("Order up! "); synchronized (waitPerson) { waitPerson.notify();
}
}
try {
sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class Restaurant {
Order order; // Package access
public static void main(String[] args) { Restaurant restaurant = new Restaurant();
WaitPerson waitPerson = new WaitPerson(restaurant);
Chef chef = new Chef(restaurant, waitPerson);
}
}
Result:
Thus a thread-safe implementation of Queue class and a multi-threaded producer-consumer application using this Queue class was implemented using Java.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac Restaurant.java
C:\Program Files\Java\jdk1.6.0\bin>java Restaurant
Order up! Waitperson got Order 0
Order up! Waitperson got Order 1
Order up! Waitperson got Order 2
Order up! Waitperson got Order 3
Order up! Waitperson got Order 4
Order up! Waitperson got Order 5
Order up! Waitperson got Order 6
Order up! Waitperson got Order 7
Order up! Waitperson got Order 8
Order up! Waitperson got Order 9
Out of food, closing
Ex. No: Multi-threading using Pipes
Aim:
To write a multi-threaded Java program to print all numbers below 100,000 that is both prime and Fibonacci number using pipes and the main thread should read both the pipes to identify numbers common to both.
Algorithm:
Step 1. Start
Step 2. Create a Fibonacci Thread that calculates the Fibonacci series and print it.
Step 3. Create a Prime number Thread that calculates the prime numbers and print it.
Step 4. In the main thread compare the output of Fibonacci thread and prime numbers
thread.
Step 5. If both the values are equal print it.
Step 6. Else move to the next value.
Step 7. Do steps 5 and 6 until the end of the series.
Step 8. View the output.
Step 9. Stop
Program:
// ThreadDemo.java
class FibonacciThread extends Thread
{
public int fib_res[]=new int[10]; FibonacciThread()
{
super("Fibonacci Thread"); System.out.println(this); start();
}
public void run()
{
try
{
int a=0,b=1,c,i=0; System.out.println("Fibonacci series :"+a); while(b<=10)
{
fib_res[i]=b;
System.out.println("Fibonacci series :"+b);
c=a+b; a=b; b=c;
i++; Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Fibonacci Thread Interrupted "+e);
}
System.out.println("Fibonacci Thread Exiting!!!");
}
}
class PrimeThread extends Thread
{
public int prime_res[]=new int[10]; PrimeThread()
{
super("Prime Thread"); System.out.println(this); start();
}
public void run()
{
try
{
int i,index=0;
for (i=1;i<10;i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i%j;
if (n==0)
{
break;
}
}
if(i == j)
{
prime_res[index]=i; System.out.println("Prime Number:"+i);
}
index++; Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Prime Thread Interrupted "+e);
}
System.out.println("Prime Thread Exiting!!!");
}
}
class ThreadDemo
{
public static void main(String args[])
{
try
{
FibonacciThread ft=new FibonacciThread(); PrimeThread pt=new PrimeThread(); Thread.sleep(15000);
System.out.println("The elements in the fibonacci and prime arrays are:");
for(int i=0;i
for(int j=0;j t
System.out.println("The common elements are :");
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(ft.fib_res[i]==pt.prime_res[j])
{
if(ft.fib_res[i]!=0) System.out.print(ft.fib_res[i]+"\t"); break;
}
else continue;
}
}
}
catch(InterruptedException e)
{
System.out.println("Interrupted!!!");
}
System.out.println(); System.out.println("End of main");
}
}
Result:
Thus a template for linked-list class along with its methods in Java was developed.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac ThreadDemo.java
C:\Program Files\Java\jdk1.6.0\bin>java ThreadDemo
Thread[Fibonacci Thread,5,main] Fibonacci series :0
Thread[Prime Thread,5,main] Fibonacci series :1
Fibonacci series :1
Fibonacci series :2
Prime Number:2
Fibonacci series :3
Fibonacci series :5
Prime Number:3
Fibonacci series :8
Fibonacci Thread Exiting!!!
Prime Number:5
Prime Number:7
Prime Thread Exiting!!!
The elements in the fibonacci and prime arrays are:
1 1 2 3 5 8 0 0 0 0
0 2 3 0 5 0 7 0 0 0
The common elements are :
2 3 5
End of main
Ex. No: Multi-threaded GUI application
Aim:
To develop a multi-threaded GUI application of your choice
Algorithm:
Step 1. Start
Step 2. Create a class ThreadExample that extends Applet
Step 3. Create objects needed for a clock.
Step 4. Create a layout and place the clock field in the center. Step 5. Start the clock thread.
Step 6. Run the thread which will print the date
Step 7. View the output in appletviewer.
Step 8. Stop.
Program:
//ThreadExample.java
import java.awt.*;
import java.applet.*;
// We need Date to get the current time. import java.util.Date;
/*<applet code="ThreadExample.class" width=600 height=600></applet>*/
public class ThreadExample extends Applet implements Runnable
{
// Define your thread.
Thread clockThread;
// This textfield will show the time. TextField clockField;
// Date will give us the current hours, minutes and seconds
Date date;
// This variable will remain true for as long
// we want the thread to run. boolean running = true;
public void init()
{
// a standard layout to place just one textfield setLayout(new BorderLayout());
clockField = new TextField();
add(clockField,"Center");
// Create the thread. clockThread= new Thread(this);
// and let it start running
clockThread.start();
}
public void destroy()
{
// will cause thread to stop looping running = false;
// destroy it. clockThread = null;
}
public void run()
{
// loop until told to stop
while (running)
{
// Construct the current date. date = new Date();
// Get the hours, minutes and hours
String time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
// Put that result in the textfield clockField.setText(time);
//Now the reason for threads try
{
// Wait 500milliseconds before continuing clockThread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
}
Result:
Thus a multi-threaded GUI application was developed using Java.
Output:
C:\Program Files\Java\jdk1.6.0\bin>javac ThreadExample.java
C:\Program Files\Java\jdk1.6.0\bin>appletviewer ThreadExample.java
No comments:
Post a Comment