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

Java Lab Record 2024

The document contains a series of Java programming exercises, each with a specific aim, algorithm, and program code. Topics include conversions, triangle checks, simple interest calculations, constructor overloading, string methods, and inheritance concepts. Each exercise concludes with the output and a statement confirming successful execution.

Uploaded by

murali.dhiviya96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java Lab Record 2024

The document contains a series of Java programming exercises, each with a specific aim, algorithm, and program code. Topics include conversions, triangle checks, simple interest calculations, constructor overloading, string methods, and inheritance concepts. Each exercise concludes with the output and a statement confirming successful execution.

Uploaded by

murali.dhiviya96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

INDEX

SN DATE NAME OF THE PAG SIGNATURE


E
O PROGRAM NO

1. PROGRAM TO CONVERT FAHRENHEIT TO CELSIUS 1


(a)
PROGRAM TO PERFORM AUTOMATIC TYPE CONVERSION
1. 2
(b)
1. PROGRAM TO PERFORM BINARY TO GRAY CONVERSION 3
(c)
2 PROGRAM TO CHECK FOR EQUILATERAL TRIANGLE 5
(a)
3 PROGRAM TO FIND LARGEST AND SMALLEST NUMBERS 6

4 PROGRAM TO CALCULATE SIMPLE INTEREST USING CLASS 7

5 PROGRAM TO PERFORM CONSTRUCTOR OVERLOADING 8

PROGRAM TO ILLUSTRATE THE USE OF STRING CLASS


6 9
AND METHODS

7 PROGRAM TO ILLUSTRATE THE USE OF SINGLE INHERITANCE 11


(a)
PROGRAM TO ILLUSTRATE THE USE OF
7 13
MULTILEVEL INHERITANCE
(b)
8 PROGRAMTOILLUSTRATETHEUSEOFMULTIPLEINTERFACE 15

EXCEPTION HANDLING MECHANISM IN JAVA USING


9 17
NESTED TRY BLOCK

10( IMPLEMENTING THE JAVA MULTITHREADING CONCEPT 19


a)
10( CREATING THREADS USING RUNNABLE INTERFACE 20
a)
11( PROGRAM USING VECTOR CLASS 22
a)
11( ADDING INTEGER ELEMENTS IN VECTOR 24
b)
IMPLEMENTING STACK IN JAVA USING STACK CLASS
11( 26
c)
12 DISPLAYING DATE AND TIME USING JAVA CALENDAR CLASS 27

13( EVENT HANDLING USING ACTIONLISTENER INTERFACE 28


a)
13( EVENT HANDLING USING MOUSELISTENER INTERFACE 29
b)
AWT LABEL CONTROL
14( 31
a)
14( AWT BUTTON CONTROL 32
b)
15( BORDER LAYOUT 33
a)
15( FLOW LAYOUT 35
b)
EX.NO: 01(A) NAME :
DATE: REG.NO:

PROGRAM TO CONVERT FAHRENHEIT TO CELSIUS

AIM:
To convert Fahrenheit to Celsius .

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable
Step 3: Read the temperature value using scanner object as sc.nextInt() and store it in the
variable a.
Step 4: In the main method, call the celsius method as celsius (a), then celsius(double f)
method returns the Celsius temperature.
Step 6: Print the output
Step 7: End the program

PROGRAM:
import java.util.Scanner;
class
FahrenheittoCelsius
{
public static void main(String arg[])
{
double a,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Fahrenheit
temperature");
a=sc.nextDouble();
System.out.println("Celsius temperature is = "+celsius(a));

}
static double celsius(double f)
{
return (f-32)*5/9;
}
}
OUTPUT:

RESULT: Thus the required output has been acquired successfully.


2

EX.NO: 01(B) NAME :


DATE: REG.NO:

PROGRAM TO PERFORM AUTOMATIC TYPE CONVERSION

AIM:
To convert Fahrenheit to Celsius .

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable a,d and initialize it.
Step 3: Perform type conversion of integer to long by assigning a to l , long to float
by assigning l to f, and float to double by assigning d1 = f.
Step 6: Print the output variables
Step 7: End the program

PROGRAM:

import
java.util.*; public
class Main
{
public static void main(String[] args)
{
int a = 50;
double d =
100;
// Automatic type
conversion long l = a;

// Automatic type
conversion float f = l;
double d1 = f;

System.out.println("Int value "+a);


System.out.println("Long value "+l);
System.out.println("Float value "+f);
System.out.println("Double value "
+d1);
}
}
OUTPUT:

RESULT: Thus the required output has been acquired successfully.


3

EX.NO: 1(c) NAME :


DATE: REG.NO:

PROGRAM TO PERFORM BINARY TO GRAY CONVERSION

AIM:
To convert the binary no to gray.

ALGORITHM:

Step 1: Start the program


Step 2: Declare the variable a,d and initialize it.
Step 3: If n=0 then gray=0.
Step 4: Else if the last two bits are opposite to each other then gray = 1 + (10
* binaryToGray(n/10)).
Step 5: Else if the last two bits are same then gray = 10 * binaryToGray (n/10)
Step 6: End the program

PROGRAM:
import java.io.*;
import java.util.Scanner;
import static
java.lang.StrictMath.pow; public class
Main {
public static void main(String[] args)
{ int n, result = 0;
Scanner sc = new
Scanner(System.in);
n=sc.nextInt();
Main obj = new Main();
result =
obj.Graycode(n,0);
System.out.println(result);
}
int Graycode(int x,int i)
{
int a,b,
result=0; if(x!=
0)
{
a=x % 10;
x=x / 10;
b=x % 10;
4

if((a & ~b)==1||(~a & b)==1)


{
result= (int)(result + pow(10,i));
}
return Graycode(x,++i)+result;
}
return 0;
}
}

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


5

EX.NO: 2(a) NAME :


DATE: REG.NO:

PROGRAM TO CHECK FOR EQUILATERAL TRIANGLE

AIM:
To check for Equilateral Triangle

ALGORITHM:

Step 1: Start the program


Step 2: Declare the variable,b,c.
Step 3:Read the input for a,b,c using Scanner object sc.
Step 4: Check the condition if(a=b)&&(b==c)
Step 5: if true then the it forms a equilateral triangle else print not a equilateral triangle.
Step 6: End the program

PROGRAM:
import java.io.*;
import
java.util.Scanner; public
class Main
{
public static void main(String[] args)
{ int a,b,c;
Scanner sc= new Scanner (System.in);
System.out.println("Enter the angles of
Triangle"); a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
sc.close();
if((a==b)&&(b==c)
)
System.out.println("Yes, equilateral Triangle");
else
System.out.println("Not a equilateral Traingle");
}
}
OUTPUT:

RESULT: Thus the required output has been acquired successfully.


6

EX.NO: 03 NAME :
DATE: REG.NO:

PROGRAM TO FIND LARGEST AND SMALLEST NUMBERS

AIM:

To find the largest and smallest among the given list

ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the values
Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program

PROGRAM:

public class Main


{
public static void main(String[] args)
{
//array of 10 numbers
int numbers[] = new int[]{10,43,53,54,32,65,63,180,43,23};
//assign first element of an array to largest and
smallest int smallest = numbers[0];
int largest = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] >
largest) largest =
numbers[i];
else if (numbers[i] <
smallest) smallest =
numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
}
}

OUTPUT:

RESULT:

Thus the required output has been obtained successfully.


7
8

EX NO: 4 NAME :
DATE: REG.NO:
PROGRAM TO CALCULATE SIMPLE
AIM: INTEREST USING CLASS

To calculate simple interest using class


ALGORITHM:
Step 1: Start the program
Step 2: Create a class and methods
Step 3:Declare the variable
Step 4: Assign value to the variable
Step 5: Start looping
Step 6: Check the condition
Step 7: print the prime
number Step 8: End the
program

PROGRAM:
import
java.util.Scanner; class
interest
{
public void cal(float p,float r,float t)
{
float si;
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);
} }
public class Main
{
public static void main(String[] args)
{
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal :
"); p = s.nextFloat();
System.out.print("Enter the Rate of interest :
"); r = s.nextFloat();
System.out.print("Enter the Time period :
"); t = s.nextFloat();
interest i = new interest(); i.cal(p,r,t);
}}

OUTPUT:

Result : Hence Simple Interest have been successfully calculated using Class.
9
8

EX NO: 5 NAME :
DATE: REG.NO:

PROGRAM TO PERFORM CONSTRUCTOR OVERLOADING

AIM:
To display students using constructor overloading concept

PROGRAM:

public class Student


{
//instance variables of the
class int id,passoutYear;
String name,contactNo,collegeName;
Student(String contactNo, String collegeName, int passoutYear)
{ this.contactNo = contactNo;
this.collegeName =
collegeName; this.passoutYear =
passoutYear;
}
Student(int id, String name)
{
this("9840234455", "IIT Kanpur", 2021);
this.id = id;
this.name = name;
}

public static void main(String[] args)


{
//object creation
Student s = new Student(101, "Arvind");
System.out.println("Printing Student Information: \n");
System.out.println("Name: "+s.name+"\nId: "+s.id+"\nContact No.: "+s.contactNo+"\
nColle ge Name: "+ s.contactNo+"\nPassing Year: "+s.passoutYear);
}
}

OUTPUT:
java -cp /tmp/RIfvVJfARx Student

Printing Student Information:


Name:
Sara Id:
101
Contact No.: 9840236655
College Name: 9840234488
Passing Year: 2022

RESULT: Thus the required output has been acquired successfully


8
9

EX NO: 6 NAME :
DATE: REG.NO:

PROGRAM TO ILLUSTRATE THE USE OF STRING CLASS AND METHODS

AIM:

To create a program for string class methods in java.

ALGORITHM:

Step 1. create main class.


Step 2. Declaring the string as static and initializing string.
Step 3. Printing original string.
Step 4. By using length() method finding the length of the stirng.
Step 5. Performing concatenation operation by using ‘+’ sign.
Step 6. By using toUpperCase() method converting the string in uppercase.
Step 7. For extracting a single character from the string using charAt()
method. Step 8. By using the byte array converting the ASCII value in string.
Step 9. Replace() method is used to replace the string ‘s character.
Step 10. Equals() method is used to check whether the strings are same or not..(including
case)
Step 11.EqualsIgnoreCase() is used to check whether the strings are same or not..
Step 12. StartsWith() and EndsWith() method are used to check the string starts and ends
with the given word or not.

PROGRAM:

class strMethod

static String str="object";

public static void main(String arg[])

System.out.println("original string: "+str);

int str1=str.length();

System.out.println("length of string:"+str1);

String str2=str+" Oriented";

System.out.println("Modified string:

"+str2); String str3=str2.toUpperCase();


10

System.out.println("String :"+str3)

; char ch; ch="abc".charAt(2);

System.out.println("CharAt :"+ch);

byte x[]={66,67,68,69,70};

String s4=new

String(x);

System.out.println(s4);

String str4="Hello".replace('l','w');

System.out.println(str4);

String

s1="HELLO";

String s2="hello";

System.out.println(s1.equals(s2));

System.out.println(s1.equalsIgnoreCase(s2))

; String s5="Foot ball";

System.out.println(s5.startsWith("foot"));

System.out.println(s5.endsWith("ball"));

}}

OUTPUT:

java -cp /tmp/RIfvVJfARx


strMethod original string: object
length of string:6
Modified string: object
Oriented String :OBJECT
ORIENTED
CharAt :cBCDE
F Hewwo
fals
e
true
fals
e
true

RESULT: Thus the required output has been acquired successfully.


11

EX NO: 7 (a) NAME :


DATE: REG.NO:

PROGRAM TO ILLUSTRATE THE USE OF SINGLE INHERITANCE


AIM:

To create a program to calculate the area and perimeter of the circle using the single
inheritance concept .

PROGRAM:

import java.io.*;
import
java.util.Scanner; class
Base
{
double r;
public void getInput(){
Scanner in=new Scanner(System.in);
System.out.println("\nEnter the radius of
Circle :"); r=in.nextDouble();
}
public void area()
{
double area=3.14*r*r;
System.out.println("Area of Circle :"+String.format("%.2f",area));
}
}
class Derived extends Base
{
public void perimeter()
{ double
perimeter=2*3.14*r;
System.out.println("Perimeter of Circle :"+String.format("%.2f",perimeter));
}
}
public class Main {
public static void main(String[] args)
{
Derived obj = new
Derived(); obj.getInput();
obj.area();
obj.perimeter()
;
}
12

}
13

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


14

EX NO: 7 (b) NAME :


DATE: REG.NO:

PROGRAM TO ILLUSTRATE THE USE OF MULTILEVEL INHERITANCE

AIM:

To create a program to calculate employee gross pay and net pay using the multilevel
inheritance concept .

PROGRAM:
import java.io.*;
import
java.util.Scanner; class
EmpDetails
{
Scanner in = new
Scanner(System.in); private String
name,id;
public void details()
{

System.out.println("\nEnter Employee
ID :"); id=in.next();
System.out.println("Enter Employee
Name :"); name=in.next();
}
public void printdetails()
{
System.out.println("\nEmployee Details :-\
n"); System.out.println("ID : "+id);
System.out.println("Name : "+name);
}
}
class Salary extends EmpDetails
{
protected double
bp,DA,HRA,PF; public void
Sal()
{
System.out.println("Enter Basic
Pay :"); bp=in.nextDouble();

DA=25*bp/100;
HRA=10*bp/100;
PF=12*bp/100;

}
public void printSal()
{
System.out.println("Basic Pay : $"+bp);
System.out.println("DA :
$"+DA);
15

System.out.println("HRA :
$"+HRA); System.out.println("PF
: $"+PF);

}
}
class Pay extends Salary
{
private double gp,np;
void pay()
{
gp=bp+DA+HRA+PF;
np=bp-PF;
System.out.println("Gross Pay :
$"+gp);
System.out.println("Net Pay : $"+np);
}
}
public class Emp
{
public static void main(String[] args)
{
Pay obj = new
Pay(); obj.details();
obj.Sal();
obj.printdetails();
obj.printSal();
obj.pay();
}
}
OUTPUT

RESULT: Thus the required output has been acquired successfully.


16

EX NO: 8 NAME :
DATE: REG.NO:

PROGRAM TO ILLUSTRATE THE USE OF MULTIPLE INTERFACE

AIM:

To calculate area of the rectangle and square using default method and to implement the
concept of multiple interface in java.
PROGRAM:

interface Polygon
{
void getArea();
// default method
default void getSides()
{
System.out.println("To get sides of a polygon.");
}
}
// implements the interface
class Rectangle implements Polygon
{ public void getArea() {
int length = 6;
int breadth = 5;
int area = length * breadth;
System.out.println("The area of the rectangle is " + area);
}
// overrides the
getSides() public void
getSides() {
System.out.println("I have 4 sides.");
}
}
// implements the interface
class Square implements Polygon
{ public void getArea() {
int length = 5;
int area = length * length;
System.out.println("The area of the square is " + area);
}
}
class Main {
public static void main(String[] args) {
// create an object of Rectangle
Rectangle r1 = new
Rectangle(); r1.getArea();
r1.getSides();
// create an object of
Square Square s1 = new
Square(); s1.getArea();
17

s1.getSides();
}
}

OUTPUT:

The area of the rectangle is


30 I have 4 sides.
The area of the square is
25 To get sides of a
polygon.

RESULT: Thus the required output has been acquired successfully.


18

EX NO: 9 NAME :
DATE: REG.NO:

EXCEPTION HANDLING MECHANISM IN JAVA USING NESTED TRY

BLOCK AIM:

To implement the exception handling mechanism in java using nested try block.
PROGRAM:

public class Main


{
public static void main(String args[])
{
//outer try block
try{
//inner try block 1
try{
System.out.println("going to divide by
0"); int b =39/0;
}
//catch block of inner try block
1 catch(ArithmeticException e)
{
System.out.println(e);
}

//inner try block


2 try{
int a[]=new int[5];

//assigning the value out of array


bounds a[5]=4;
}

//catch block of inner try block 2


catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}

System.out.println("other statement");
}
//catch block of outer try
block catch(Exception e)
{
System.out.println("handled the exception (outer catch)");
}
19

System.out.println("normal flow..");
}
}

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


20

EX NO: 10 (a) NAME :


DATE: REG.NO:

IMPLEMENTING THE JAVA


MULTITHREADING CONCEPT
AIM:

To set priority of the threads and to implement the concept of java multithreading.
PROGRAM:

class TestMultiPriority1 extends Thread


{
public void run()
{

System.out.println("running thread name is:"+Thread.currentThread().getName());


System.out.println("running thread priority is:"+Thread.currentThread().getPriority());

}
public static void main(String args[])
{
TestMultiPriority1 m1=new
TestMultiPriority1(); TestMultiPriority1
m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();

}
}

OUTPUT

running thread name is:Thread-


0 running thread name
is:Thread-1 running thread
priority is:10 running thread
priority is:1

RESULT: Thus the required output has been acquired successfully.


21

EX NO: 10 (b) NAME :


DATE: REG.NO:
CREATING THREADS USING
RUNNABLE INTERFACE
AIM:

To create threads using runnable interface and to implement the concept of


java multithreading.

PROGRAM:
class MultithreadingDemo implements Runnable
{ public void run()
{
try {
// Displaying the thread that is
running System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
class Main {
public static void main(String[] args)
{
int n = 8; // Number of
threads for (int i = 0; i < n; i+
+) {
Thread object
= new Thread(new
MultithreadingDemo()); object.start();
}
}
}
22

OUTPUT

RESULT: Thus the required output has been acquired successfully.


23

EX NO: 11(a) NAME :


DATE: REG.NO:

PROGRAM USING VECTOR CLASS


Aim:

To create a dynamic array of strings and implement various vectors methods using vector
class in java.

PROGRAM

import java.util.*;
public class VectorExample1 {
public static void main(String args[]) {
//Create an empty vector with initial capacity 4
Vector<String> vec = new Vector<String>(4);
//Adding elements to a
vector vec.add("RED");
vec.add("BLUE");
vec.add("GREEN");
vec.add("ORANGE");
//Check size and capacity
System.out.println("Size is:
"+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
//Display Vector elements
System.out.println("Vector element is:
"+vec); vec.addElement("YELLOW");
vec.addElement("WHITE");
vec.addElement("BLACK");
//Again check size and capacity after two insertions
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());
//Display Vector elements again
System.out.println("Elements are:
"+vec);
//Checking if Tiger is present or not in this
vector if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
//Get the first element
System.out.println("The first animal of the vector is = "+vec.firstElement());
//Get the last element
System.out.println("The last animal of the vector is = "+vec.lastElement());
}
}
24

OUTPUT:

Size is: 4
Default capacity is: 4
Vector element is: [RED, BLUE, GREEN, ORANGE]
Size after addition: 7
Capacity after addition is: 8
Elements are: [RED, BLUE, GREEN, ORANGE, YELLOW, WHITE, BLACK]
Tiger is not present in the list.
The first animal of the vector is = RED The
last animal of the vector is = BLACK

RESULT: Thus the required output has been acquired successfully.


25

EX NO: 11(b) NAME :


DATE: REG.NO:

ADDING INTEGER ELEMENTS IN VECTOR


AIM:

To create a dynamic array of integers and to implement various vectors methods using
vector class in java.

PROGRAM:

import
java.util.*; public
class Main
{
public static void main(String args[])
{
//Create an empty Vector
Vector < Integer > in = new Vector < > ();
//Add elements in the
vector in.add(100);
in.add(200);
in.add(300);
in.add(200);
in.add(400);
in.add(500);
in.add(600);
in.add(700);
//Display the vector elements
System.out.println("Values in vector: "
+in);
//use remove() method to delete the first occurence of an element
System.out.println("Remove first occourence of element 200: "+in.remove((Integer)200));
//Display the vector elements afre remove()
method System.out.println("Values in vector: "
+in);
//Remove the element at index 4
System.out.println("Remove element at index 4: "
+in.remove(4)); System.out.println("New Value list in vector: "
+in);
//Remove an element
in.removeElementAt(5)
;
//Checking vector and displays the element
System.out.println("Vector element after removal: " +in);
//Get the hashcode for this vector
System.out.println("Hash code of this vector = "+in.hashCode());
//Get the element at specified index
System.out.println("Element at index 1 is =
"+in.get(1));
}
}
26

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


27

EX NO: 11(c) NAME :


DATE: REG.NO:

IMPLEMENTING STACK IN JAVA USING STACK CLASS

AIM:

To perform various stack operations in java using stack class in java.

PROGRAM:
import

java.util.*; public

class Main

{
public static void main(String[] args)
{
//creating an instance of Stack
class Stack stk= new Stack();
// checking stack is empty or
not boolean result =
stk.empty();
System.out.println("Is the stack empty? " + result);
// pushing elements into
stack stk.push(78);
stk.push(113)
;
stk.push(90);
stk.push(120)
;
//prints elements of the stack
System.out.println("Elements in Stack: " + stk);
result = stk.empty();
System.out.println("Is the stack empty? " + result);
}
}

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


28

EX NO: 12 NAME :
DATE: REG.NO:

DISPLAYING DATE AND TIME USING JAVA CALENDAR


CLASS

AIM:
To convert date between a specific instant in time and a set of calendar fields such as MONTH,
YEAR, HOUR, etc using Java Calendar class.

PROGRAM:
import
java.util.Calendar; public
class Main {
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance(); System.out.println("The
current date is : " + cal.getTime()); cal.add(Calendar.DATE, -
15); System.out.println("15 days ago: " + cal.getTime());
cal.add(Calendar.MONTH, 4);
System.out.println("4 months later: " +
cal.getTime()); cal.add(Calendar.YEAR, 2);
System.out.println("2 years later: " + cal.getTime());
}
}

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


29

EX NO: 13 (a) NAME:


DATE: REG.NO:

EVENT HANDLING USING ACTIONLISTENER INTERFACE

AIM:
To perform event handling in java using ActionListener interface.

PROGRAM:
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener
{
TextField
tf; AEvent()
{
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click
me");
b.setBounds(100,120,80,30);
b.addActionListener(this);
add(b);add(tf);
setSize(300,300)
;
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}}

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


30

EX NO: 13 (b) NAME :


DATE: REG.NO:

EVENT HANDLING USING MOUSELISTENER INTERFACE

AIM:
To perform event handling in java using MouseListener interface.

PROGRAM:

import java.awt.*;
import
java.awt.event.*;
public class MouseListenerExample extends Frame implements
MouseListener{ Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20)
; add(l);
setSize(300,300)
; setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{ l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{ l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{ l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{ l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{ l.setText("Mouse Released");
}
public static void main(String[] args)
{ new MouseListenerExample();
}

OUTPUT:
31

RESULT: Thus the required output has been acquired successfully.


32

EX NO: 14(a) NAME :


DATE: REG.NO:

AWT LABEL CONTROL

AIM:

To create awt label control using applet.

PROGRAM:
import java.awt.*;
import
java.applet.*;
/*
<applet code = "LabelDemo" width=300 height=200>
</applet>
*/
public class LabelDemo extends Applet
{
public void init ()
{
Label one = new Label ("One");
Label two = new Label ("Two");
Label three = new Label
("Three"); add (one);
add (two);
add (three);
}
}

OUTPUT:

RESULT: Thus the required output has been acquired successfully.


33

EX NO: 14(b) NAME :


DATE: REG.NO:

AWT BUTTON CONTROL


AIM:

To create awt button control using applet.

PROGRAM:

/*
<applet code="CreateAWTButton" width=200 height=200>
</applet>
*/

import
java.applet.Applet;
import java.awt.Button;
public class CreateAWTButton extends Applet
{
public void init()
{
Button b1 = new
Button();
b1.setLabel("Submit");
Button b2 = new Button("Cancel");
add(b1);
add(b2);
}
}
OUTPUT:

RESULT: Thus the required output has been acquired successfully.


34

EX NO: 15 (a) NAME :


DATE: REG.NO:

BORDER LAYOUT
AIM:

To arrange the components in five regions. The five regions can be north, south, east, west
and the centre using BorderLayout.

PROGRAM:
import java.awt.*;
import
javax.swing.*;
public class BorderDemo1
{
JFrame frame;
BorderDemo1(
)
{
frame=new JFrame();
JButton box1=new JButton("**NORTH**");;
JButton box2=new JButton("**SOUTH**");;
JButton box3=new JButton("**EAST**");;
JButton box4=new JButton("**WEST**");;
JButton box5=new
JButton("**CENTER**");;
frame.add(box1,BorderLayout.NORTH);
frame.add(box2,BorderLayout.SOUTH);
frame.add(box3,BorderLayout.EAST);
frame.add(box4,BorderLayout.WEST);
frame.add(box5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}

public static void main(String[] args)


35

{
36

new BorderDemo1();
}
}

OUTPUT:
RESULT: Thus the required output has been acquired successfully.
37

EX NO: 15 (b) NAME :


DATE: REG.NO:

FLOW LAYOUT
AIM:

To arrange the components in a sequence one after another using FlowLayout.

PROGRAM:
import java.awt.*;
import
javax.swing.*;
public class
FlowDemo1{ JFrame
frame1; FlowDemo1()
{ frame1=new JFrame();
JButton box1=new JButton("1");
JButton box2=new JButton("2");
JButton box3=new JButton("3");
JButton box4=new JButton("4");
JButton box5=new JButton("5");
JButton box6=new JButton("6");
JButton box7=new JButton("7");
JButton box8=new JButton("8");
JButton box9=new JButton("9");
JButton box10=new
JButton("10"); frame1.add(box1);
frame1.add(box2);
frame1.add(box3);
frame1.add(box4);
frame1.add(box5);
frame1.add(box6);
frame1.add(box7);
38

frame1.add(box8);
frame1.add(box9);
frame1.add(box10);
frame1.setLayout(new
FlowLayout(FlowLayout.LEFT))
; frame1.setSize(400,400);
frame1.setVisible(true);
}

public static void main(String[] args)


{
new FlowDemo1();
}
}

OUTPUT

RESULT: Thus the required output has been acquired successfully.

You might also like