Java Programming Lab
Java Programming Lab
LAB MANUAL
JAVA PROGRAMMING LAB
BACHELOR OF COMPUTER TECHNOLOGY
ACADEMIC YEAR: 2025-2026
ODD SEMESTER
Prepared by:
Dr. D. Maheshwari
Java Programming Lab (33P)
HARDWARE:
SOFTWARE:
Program 6
Write a Java Program to create a frame with four text fields name, street, city
and pin code with suitable tables. Also add a button called my details. When
the button is clicked its corresponding values are to be appeared in the text
fields.
Program 7
Write a Java Program to demonstrate the Multiple Selection List-box.
Program 8
Write a Java Program to create a frame with three text fields for name, age
and qualification and a text field for multiple line for address
Program 9
Write a Java Program to create Menu Bars and pull down menus.
Program 10
Write a Java Program to create frames which respond to the mouse clicks. For
each events with mouse
Program 11
Write a Java Program to draw circle, square, ellipse and rectangle at the
mouse click positions.
Program 12
Write a Java Program which open an existing file and append text to that file.
Text Book (s)
1 Programming with Java – A Primer - E. Balagurusamy, 3rd Edition, TMH
Java Programming Lab (33P)
INDEX
S. No. Date Name of the Program Page No. Signature
1 String Extraction
2 Multiple Inheritance
3 Own Exception
4 Multithread
5 Shapes
9 Menu Creation
Exercise No: 1
String Extraction
Date :
Aim
To Write a Java Applications to extract a portion of a character string and print the
extracted string.
Procedure
Step 1: Start the program
Step 2: Create a class stringextract
Step 3: Initialize and Assign string object s1
Step 4: Use charAt() function to extract the single character for the given
index value
Step 5: Use getChars() function to extract the one or more character for the
given started index value, ended index value and store in to the array
object buf[]
Step 6: Use ToCharArray() fuction to convert String object into Character
Array.
Step 7: Print the output
Step 9: Stop the process
Coding
/* Java Applications to extract a portion of a character string and print the
extracted string. */
import java.io.*;
import java.lang.*;
public class stringextract
{
public static void main(String args[])
{
String s1= "Computer Science with Data Analytics";
System.out.println("Single Character Extraction");
char ch;
ch="Computer".charAt(3);
System.out.println ("The Index 3rd character=: "+ch);
Java Programming Lab (33P)
OUTPUT
Exercise No: 2
Multiple Inheritance
Date :
Aim
To Write a Java Program to implement the concept of multiple inheritance
using Interfaces
Procedure
Step 1: Start the process
Step 2: Create an interface area with a member variable pi=3.14
Step 3: Create an interface area1 with a method float compute(float x, float
y)
Step 4: Define a class rectangle that implements the interfaces area and
area1
Step 5: Call the method compute to calculate the area of rectangle using
rectarea=x*y
Step 6: Return the value of rectarea
Step 7: Define a class circle that implements the interfaces area and area1
Step 8: Call the method compute to calculate the area of circle using
cirarea=pi*x*x
Step 9: Return the value of cirarea
Step 10: Create a class interdemo and declare objects for the two classes
Step 11: Display the results
Step 12: Stop the Process
Coding
/* Java Program to implement the concept of multiple inheritance using
Interfaces*/
import java.io.*;
import java.lang.*;
public interface area
{
static final float pi=3.14f;
}
Java Programming Lab (33P)
OUTPUT
Java Programming Lab (33P)
Exercise No: 3
Own Exception
Date :
Aim
To Write a Java Program to create an Exception called pay- out-of-bounds
and throw the exception
Procedure
Step 1: Start the program
Step 2: Create a class myexp that extends the in built class Exception
Step 3: Define the constructors for the class
Step 4: Create a class mainexp
Step 5: Declare and initialize the variables inside the main function
Step 6: Get the input values using readLine() method within the try block
Step 7: If salpermonth>10000.1 then throw an exception “pay out of bounds”
Step 8 : Otherwise display “correct salary paid for each month”
Step 9: Stop the process.
Coding
/* Java Program to create an Exception called pay-out-of-bounds and throw the
exception*/
import java.io.*;
import java.lang.*;
import java.io.DataInputStream;
public class myexp extends Exception
{
public myexp() { }
public myexp(String msg)
{
super(msg);
}
}
class mainexp1
{
public static void main(String args[])
Java Programming Lab (33P)
{
float salary=0.0f;
int month=0;
float salpermon=0.0f;
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("Enter the string:");
salary=Float.valueOf(in.readLine());
System.out.println("Enter the month");
month=Integer.parseInt(in.readLine());
}
catch(Exception e) {}
try
{
salpermon=salary/month;
if(salpermon>10000.1)
{
throw new myexp("Pay out bound");
}
else
{
System.out.println("Correct salary paid for each month");
System.out.println("_______");
}
}
catch(myexp e)
{
System.out.println("Caught my exception");
Java Programming Lab (33P)
System.out.println(e.getMessage());
e.getMessage();
}
}
}
OUTPUT
Java Programming Lab (33P)
Exercise No: 4
Date :
Multithreading
Aim
To Write a Java Program to implement the concept of multithreading with the
use of any three multiplication tables and assign three different priorities to them
Procedure
Step1 : Start the process
Step 2: Create a class aThread that extends the inbuilt class Thread
Step 3: Define a method run() to display the multiplication table 7
Step 4: Create a class bThread that extends the inbuilt class Thread
Step 5: Define a method run() to display the multiplication table 13
Step6 : Create a class cThread that extends the inbuilt class Thread
Step 7: Define a method run() to display the multiplication table 13
Step8 : Create a class mThread in which main method is defined
Step9 : Declare objects for the three classes
Step10: Set Priority values for the three threads as follows:
c.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.MIN_PRIORITY);
a.setPriority(Thread.MIN_PRIORITY);
Step11: Call the start() method to execute different Threads
Step12: Stop the process.
Coding
/*Java Program to implement the concept of multithreading with the use of any three
multiplication tables and assign three different priorities to them */
import java.io.*;
import java.lang.*;
public class aThread extends Thread {
public void run()
{
System.out.println("Thread A started");
Java Programming Lab (33P)
for(int i=1;i<5;i++)
System.out.println(i+"*"+"7="+(i*7));
System.out.println("Thread A exit");
}
}
public class bThread extends Thread {
public void run()
{
System.out.println("Thread B started");
for(int i=1;i<=5;i++)
System.out.println(i+"*"+"13="+(i*13));
System.out.println("Thread B exit");
}
}
public class cThread extends Thread {
public void run()
{
System.out.println("Thread C started");
for(int i=1;i<=5;i++)
System.out.println(i+"*"+"13="+(i+13));
System.out.println("Thread C exit");
}
}
public class mThread {
public static void main(String args[])
{
aThread a=new aThread();
bThread b=new bThread();
cThread c=new cThread();
c.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.MIN_PRIORITY);
a.setPriority(Thread.MIN_PRIORITY);
c.start();
b.start();
a.start();
}
}
Java Programming Lab (33P)
OUTPUT
Java Programming Lab (33P)
Exercise No: 5
Shapes
Date :
Aim
To Write a Java Program to draw several shapes in the created windows
Procedure
Step 1: Start the process
Step 2: import the packages required
Step 3: Define a class Shapes that extends the inbuilt class Applet
Step 4: Define a method void Paint()
Step 5: Name the applet window as “My shapes”
Step 6: Set the color of square as black using setColor() method.
Step 7: Draw a Square using the function fillRect(80,80,80,80)
Step 8: Set the color of Circle as blue
Step 9: Draw a Circle using the function fillOval(80,150,80,150)
Step 10: Set the Color of Oval as green
Step 11: Draw an Oval using the function fillRect(160,150,50,50)
Step 12: Set the Color of rectangle as orange
Step 13: Draw a Rectangle using the function fillRect(420,200,220,230)
Step 14: Display the result in applet window
Step15: Stop the Process
Coding:
// Java Program to draw several shapes in the created windows
import java.applet.*;
import java.awt.*;
public class shapes extends Applet
{
public static void Paint(Graphics g)
{
String str1 = "My shapes";
g.setColor(Color.red);
g.drawString(str1,20,20);
g.setColor(Color.black);
g.fillRect(80,80,80,80);
g.setColor(Color.blue);
g.fillOval(80,150,80,150);
g.setColor(Color.green);
Java Programming Lab (33P)
g.fillOval(160,150,50,50);
g.setColor(Color.orange);
g.fillRect(420,200,220,230);
}
}
OUTPUT
Java Programming Lab (33P)
Exercise No: 6
My Details using Frames
Date :
Aim
To Write a Java Program to create a frame with four text fields name, street,
city and pin code with suitable tables. Also add a button called my details. When the
button is clicked its corresponding values are to be appeared in the text fields.
Procedure
Step 1: Start the Process
Step 2: Create a class framedemo that extends the inbuilt class Frame
Step 3: Declare and initialize the controls to design the frame.
Step 4: Create object for the class Frame goto step 4
Using constructor mframe()
Create an object for 4 labels
Using add() method all the following to applet window
Labels,Textarea,Textfield,Button,Layout
Use actionlistner to perform the event
Step 5: Check the condition get source equal to button object set the following
text to the
Textfield and text area,object name,age,qualification and address will
display in
multiple line using the method append()
Step 6: Use paint() method to display the details again and again
Step 7: Stop the program
Coding
/*Java Program to create a frame with four text fields name, street, city and pin code
with suitable tables. Also add a button called my details. When the button is clicked
its corresponding values are to be appeared in the text fields */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class framedemo extends Frame
{
Button b1;
Label L1=new Label("Name");
Label L2=new Label("Street");
Label L3=new Label("City");
Java Programming Lab (33P)
g.drawString("My Details",50,20);
}
public static void main(String[] args)
{
framedemo fd=new framedemo();
fd.setBounds(1,1,300,300);
fd.setVisible(true);
}
}
OUTPUT
Java Programming Lab (33P)
Exercise No: 7
Date :
Multiple Selection List-box
Aim
To Write a Java Program to demonstrate the Multiple Selection List-box.
Procedure
Step 1: Start the process
Step 2: Create a classname such as MS list
Step 3: Using unit() method to initialize all components
Create object for list,textfield,button
Add textbox in the applet window with to items
Add textfield in the applet using add() method
Add button in the applet window add actionlistner event,goto
step 4
Step 4: Create string object outstr
Check the condition current event equal to button onject
Initialize list box select items to 3
Using loop increment I value one by one upto length of 8
Concatenation outstr[i]=concatenation outstr[i]+s[i];
Step 5: Display the selected items in the textfield
Step 6: Stop the process
Coding
// Java Program to demonstrate the Multiple Selection List-box
import java.applet.Applet;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MSList extends Applet implements ActionListener
{
List ls;
TextField tf;
Button b1;
public void init()
{
tf=new TextField(60);
Java Programming Lab (33P)
add(tf);
ls=new List(10,true);
ls.add("coimbatore");
ls.add("ooty");
ls.add("chennai");
ls.add("salem");
ls.add("tirupur");
ls.add("erode");
ls.add("tirchy");
ls.add("sakthi");
ls.add("madras");
ls.add("bombay");
add(ls);
b1=new Button("show selected");
b1.addActionListener(this);
add(b1);
}
String[]s;
public void actionPerformed(ActionEvent ae)
{
String outstr=new String("your selected");
if(ae.getSource()==b1)
{
s=ls.getSelectedItems();
for(int i=0;i<s.length;i++)
{
outstr+=" "+s[i];
}
tf.setText(outstr);
}
}
}
OUTPUT
Java Programming Lab (33P)
Exercise No: 8
Date :
Text Field For Multiple Line
Aim
To Write a Java Program to create a frame with three text fields for name, age and
qualification and a text field for multiple line for address
Procedure
Step 1: Start the Process
Step 2: Create a class framedemo that extends the inbuilt class Frame
Step 3: Declare and initialize the controls to design the frame.
Step 4: Create object for the class Frame goto step 4
I. Using constructor mframe()
II. Create an object for 4 labels
III. Using add() method all the following to applet window
IV. Labels, Textarea, Textfield, Button, Layout
V. Use actionlistner to perform the event
Step 5: Check the condition get source equal to button object set the following text
to the Textfield and text area,object name,age,qualification and address will
display in multiple line using the method append()
Step 6: Use paint() method to display the details again and again
Step 7: Stop the program
Coding
import java.awt.*;
import java.awt.event.*;
public class framedemo extends Frame
{
Button b1;
Label l1=new Label("Name :");
Label l2=new Label("Age :");
Label l3=new Label("Qualification :");
Label l4=new Label ("Address :");
TextField Name= new TextField(15);
TextField Age=new TextField(15);
TextField Qualification=new TextField(15);
TextField Address=new TextField(50);
Java Programming Lab (33P)
framedemo()
{
Label l= new Label("Output of My Details",Label.CENTER);
add(l);
add(l1);add(Name);
add(l2);add(Age);
add(l3);add(Qualification);
add(l4);add(Address);
setLayout(f);
b1=new Button("My Details");
b1.addActionListener(new AL());
add(b1);
addWindowListener(new WH());
}
class WH extends WindowAdapter
{
public void windowClosing(ActionEvent e)
{ System.exit(0);
} }
public class AL implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==b1)
{
Name.setText("Alex");
Age.setText("25");
Qualification.setText("BE - CSE");
Address.setText("11 A, Kumaran Nagar , Pincode- 641148");
} } }
public void paint(Graphics g)
{
g.drawString("Mydetails",50,20);
}
Java Programming Lab (33P)
OUTPUT
Java Programming Lab (33P)
Exercise No: 9
Menu Creation
Date :
Aim
To Write a Java Program to create Menu Bars and pull down menus.
Procedure
Step 1: Start the program
Step 2: Create a Class MenuCreate
Step 3: Create a Frame object f , MenuBar Object mb, Menu object M1 with
the name File
Step 4: Create Menu object submenu with the name Sub Menu
Step 5: Create MenuItem object i1, i2, i3,i4 and i5 with name as New, Open,
Save, Print and Exit
Step 6: Add the MenuItem to Menu
Step 7: Add Submenu as i4 and i5
Step 8 : Add all Menu details to MenuBar
Step 9: Set MenuBar, Size. Layout , visibility to the Frame
Step 10: Using main method to call the constructor MenuCreate()
Step 11 : Stop the program
Coding
// Create Menu Bars and pull down menus
import java.awt.*;
class MenuCreate
{
MenuCreate()
{
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu m1=new Menu("File");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("New");
MenuItem i2=new MenuItem("Open");
MenuItem i3=new MenuItem("Save");
MenuItem i4=new MenuItem("Print");
MenuItem i5=new MenuItem("Exit");
m1.add(i1);
Java Programming Lab (33P)
m1.add(i2);
m1.add(i3);
submenu.add(i4);
submenu.add(i5);
m1.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuCreate();
}
}
OUTPUT
Java Programming Lab (33P)
Exercise No: 10
Mouse Click Event
Date :
Aim
To Write a Java Program to create frames which respond to the mouse clicks.
For each events with mouse.
Procedure
Step 1: Start the process
Step 2: Create a main class such as MLExample
Step 3: Create a object for class event example goto Step 5 MLExample to
receive the arguments value and display the value using window adapter
event
Step 4: Use Actionlistener event to the following event and display the
message
Mouse clicked
Mouse entered
Mouse pressed
Mouse exit
Mouse released
Step 5: Use the paint() method to display the mouse event again and again
Step6: Use paint() method to display the mouse event
Step 7: Display the result
Step 8: Stop the process
Coding
/*Java Program to create frames which respond to the mouse clicks. For each
events with mouse */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MLExample extends Frame implements MouseListener{
String strEvent=" ";
MLExample(String title)
{
super(title);
addWindowListener(new MyWindowAdapter(this));
addMouseListener(this);
Java Programming Lab (33P)
setSize(300,300);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
strEvent="Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent e)
{
strEvent="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent e)
{
strEvent="Mouse Released";
repaint();
}
public void mouseEntered(MouseEvent e)
{
strEvent="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e)
{
strEvent="Mouse Exited";
repaint();
}
public void paint(Graphics g)
{
g.drawString(strEvent,50,50);
}
public static void main(String[] args) {
MLExample mw=new MLExample ("window with mouse event example");
}
}
class MyWindowAdapter extends WindowAdapter
{
Java Programming Lab (33P)
MLExample mw=null;
MyWindowAdapter(MLExample mw)
{
this.mw=mw;
}
public void windowClosing(WindowEvent we)
{
mw.setVisible(false);
}
}
OUTPUT
Java Programming Lab (33P)
Exercise No: 11
Shapes in Mouse Click Position
Date :
Aim
To Write a Java Program to draw circle, square, ellipse and rectangle at the
mouse click positions
Procedure
Step 1: Start the process
Step 2: Create a class bclick
Step 3: initialize the variable x,y,z as using unit() method
Step 4: Use mouselistner,increment the value of a depending on the number
of time mouse is clicked
Step 5: Get the x and y value from the method get x() and get y()
Step 6: Use paint() method click, it remainder of c divide by 4 is equal to
Draw circle
Draw square
Draw ellipse
Draw rectangle
Step 7: Display all shapes using mouse click position
Step 8: Stop the process
Coding
/* Java Program to draw circle, square, ellipse and rectangle at the mouse click
positions */
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
c++;
x= me.getX();
y= me.getY();
repaint();
}
});
}
public void paint(Graphics g)
{
if(c%4==1)
g.drawOval(x,y,100,100);
else if(c%4==2)
g.drawRect(x,y,50,50);
else if(c%4==3)
g.drawRect(x,y,25,50);
else if(c%4==0)
g.drawOval(x,y,50,80);
}
}
Java Programming Lab (33P)
Exercise No: 12
Append Text to the File
Date:
Aim
To Write a Java Program which open an existing file and append text to that file.
Procedure
Step 1: Start the process
Step 2: Create a class raf
Step 3: Create an object for the RandomAccessFile in read/write mode with
the text file (city.txt)
Step 4: Find the length of the file using rfile seek=(rfile.length)
Step 5: Append the text in rfile using mode rfile writeBytes(“Mumbai”)
Step 6: Close the file
Step 7: open the text file city.txt to check the appended data.
Step 8: if data is appended at the end of the file then close the file.
Step 9: Stop the process.
Coding
// Java Program which open an existing file and append text to that file //
import java.io.*;
public class raf
{
public static void main(String args[])
{
RandomAccessFile rFile;
try
{
rFile=new RandomAccessFile("City.txt","rw");
rFile.seek(rFile.length());
rFile.writeBytes("Mumbai\n");
rFile.close();
Java Programming Lab (33P)
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
OUTPUT