0% found this document useful (0 votes)
9 views

Java Lab Excercisesv2

Java lab excercisesV2

Uploaded by

King Of queens
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Lab Excercisesv2

Java lab excercisesV2

Uploaded by

King Of queens
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java lab excercises - APPLETS

1. A simple applet to display text.

import java.applet.Applet;

import java.awt.Graphics;

public class SimpleApplet extends Applet {

public void paint(Graphics g)

g.drawString("A simple Applet", 20, 20);

2. A simple applet to display text by changing background and foreground color.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Color;

public class NewApplet1 extends Applet {

public void paint(Graphics g){

g.drawString("Welcome to my Applet!", 25,25);

setBackground(Color.BLUE);

setForeground(Color.RED);

3. A simple applet (Japplet )to add two numbers.

import java.awt.Graphics; // program uses class Graphics

import javax.swing.JApplet; // program uses class JApplet

import javax.swing.JOptionPane; // program uses class JOptionPane


public class AdditionApplet extends JApplet

private double sum; // sum of values entered by user

// initialize applet by obtaining values from user

public void init()

// obtain first number from user

String firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" );

// obtain second number from user

String secondNumber = JOptionPane.showInputDialog("Enter second floating-point value" );

// convert numbers from type String to type double

double number1 = Double.parseDouble( firstNumber );

double number2 = Double.parseDouble( secondNumber );

sum = number1 + number2; // add numbers

// draw results in a rectangle on applet’s background

public void paint( Graphics g )

super.paint( g ); // call superclass version of method paint

// draw rectangle starting from (15, 10) that is 270 pixels wide and 20 pixels tall

g.drawRect( 15, 10, 270, 20 );

g.drawString( "The sum is " + sum, 25, 25 );

}
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

Creating new thread by extending thread

public class thread1 extends Thread{

public void run(){

System.out.println("thread is running...");

public static void main(String args[]){

thread1 t1=new thread1();

t1.start();

Creating a new thread by implementing runnable interface

public class thread2 implements Runnable {

public void run(){

System.out.println("thread is running...");

public static void main(String args[]){

thread2 m1=new thread2();

thread2 m2=new thread2();

Thread t1 =new Thread(m1);

Thread t2 = new Thread(m2);

t1.start();

t2.start();

}
}

Implementing different thread methods ---- sleep(), setPriority(), getPriority() …..

public class thread5 implements Runnable {

@Override

public void run() {

public static void main(String[] args) {

Thread th1 = new Thread();

Thread th2 = new Thread();

th1.start();

th2.start();

try {

th1.sleep(10);

th2.sleep(10);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

th1.setPriority(1);

th2.setPriority(10);

int th1priority = th1.getPriority();

int th2priority = th2.getPriority();

System.out.println(th1priority);

System.out.println("Thread1 Running");

System.out.println(th2priority);

System.out.println("Thread2 Running"); }}
Java lab excercises – Socket Programming

Establishing Client – Server Communication

Client Side Programming


import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java.net.Socket;

public class client1 {

public static void main(String[] args) {

try{

Socket ss=new Socket("localhost",9999);

PrintStream pr = new PrintStream(ss.getOutputStream());

System.out.println("Enter Ur name: ");

InputStreamReader rd= new InputStreamReader(System.in);

BufferedReader ed = new BufferedReader(rd);

String temp = ed.readLine();

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;

public class server1 {

public static void main(String[] args){

try{

ServerSocket ser = new ServerSocket(9999);

Socket sock=ser.accept();

BufferedReader ed = new BufferedReader (new InputStreamReader(sock.getInputStream()));

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.

You might also like