Java Lab Excercisesv2
Java Lab Excercisesv2
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
setBackground(Color.BLUE);
setForeground(Color.RED);
// draw rectangle starting from (15, 10) that is 270 pixels wide and 20 pixels tall
}
Excercises
1. (Arithmetic) Write an applet that asks the user to enter two floating-point numbers, obtains the
two numbers from the user and draws their sum, product (multiplication), difference and quotient
(division).
2. (Comparing Numbers) Write an applet that asks the user to enter two floating-point numbers,
obtains the numbers from the user and displays the two numbers, then displays the larger number
followed by the words "is larger" as a string on the applet. If the numbers are equal, the applet
should print the message "These numbers are equal . "
3. (Drawing Rectangles) Write an applet that draws rectangles of different sizes and locations.
Java lab excercises - Threads
System.out.println("thread is running...");
t1.start();
System.out.println("thread is running...");
t1.start();
t2.start();
}
}
@Override
th1.start();
th2.start();
try {
th1.sleep(10);
th2.sleep(10);
} catch (InterruptedException e) {
//e.printStackTrace();
th1.setPriority(1);
th2.setPriority(10);
System.out.println(th1priority);
System.out.println("Thread1 Running");
System.out.println(th2priority);
System.out.println("Thread2 Running"); }}
Java lab excercises – Socket Programming
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
try{
pr.println(temp);
catch(Exception e){}
}}
Server Side Programming
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
try{
Socket sock=ser.accept();
String tmp=ed.readLine();
System.out.println("Welcome: "+tmp);
catch(Exception ex){}
}}
NB. Write the corresponding codes for each(for client/server) in separate files. After you code
successfully, you must run the Server side first then the client side.