DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING(CYBERSECURITY)
II [Link] I Semester
Subject Name:OOPS Through JAVA LAB
Lab Manual
Academic Year: 2025-26
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING(CYBERSECURITY)
ELLENKI COLLEGE OF ENGINEERING AND TECHNOLOGY
Patelguda, Sangareddy Dist. Hyderabad.
ER24MC8108: OBJECT ORIENTED PROGRAMMING THROUGH
JAVA LAB
L T P C
0 0 3 1.5
Course Objectives:
● To write programs using abstract classes.
● To write programs for solving real world problems using the java collection framework.
● To write multithreaded programs.
● To write GUI programs using swing controls in Java.
● To introduce java compiler and eclipse platform.
● To impart hands-on experience with java programming.
Course Outcomes:
● Able to write programs for solving real world problems using the java collection framework.
● Able to write programs using abstract classes.
● Able to write multithreaded programs.
● Able to write GUI programs using swing controls in Java.
Note:
1. Use LINUX and MySQL for the Lab Experiments. Though not mandatory, encourage the use of the
Eclipse platform.
2. The list suggests the minimum program set. Hence, the concerned staff is requested to add more
problems to the list as needed.
List of Experiments:
1. Use Eclipse or Net bean platform and acquaint yourself with the various menus. Create a test project, add a
test
2. class, and run it. See how you can use auto suggestions, auto fill. Try code formatter and code refactoring
like renaming variables, methods, and classes. Try debug step by step with a small program of about 10 to 15
lines which contains at least one if else condition and a for loop.
3. Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits
and for the +, -,*, % operations. Add a text field to display the result. Handle any possible exceptions like
divided by zero.
4. A) Develop an applet in Java that displays a simple message.
B) Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and
returns it in another text field, when the button named “Compute” is clicked.
5. Write a Java program that creates a user interface to perform integer divisions. The user enters two numbers
in the text fields, Num1 and Num2. The division of Num1 and Num 2 is displayed in the Result field when the
Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a Number Format
Exception. If Num2 were Zero, the program would throw an Arithmetic Exception. Display the exception in a
message dialog box.
6. Write a Java program that implements a multi-thread application that has three threads. First thread generates
a random integer every 1 second and if the value is even, the second thread computes the square of the number
and prints. If the value is odd, the third thread will print the value of the cube of the number.
Write a Java program for the following: Create a doubly linked list of element
Delete a given element from the above list. Display the
contents of the list after deletion.
7. Write a Java program that simulates a traffic light. The program lets the user select one of three lights: red,
yellow, or green with radio buttons. On selecting a button, an appropriate message with “Stop” or “Ready” or
“Go” should appear above the buttons in the selected color. Initially, there is no message shown.
8. Write a Java program to create an abstract class named Shape that contains two integers and an empty
method named print Area (). Provide three classes named Rectangle, Triangle, and Circle such that each one of
the classes extends the class Shape. Each one of the classes contains only the method print Area () that prints the
area of the given shape.
9. Suppose that a table named [Link] is stored in a text file. The first line in the file is the header, and the
remaining lines correspond to rows in the table. The elements are separated by commas.
Write a java program to display the table using Labels in Grid Layout.
10. Write a Java program that handles all mouse events and shows the event name at the center of the window
when a mouse event is fired (Use Adapter classes).
REFERENCE BOOKS:
1. Java for Programmers, P. J. Deitel and H. M. Deitel, 10th Edition Pearson education.
2. Thinking in Java, Bruce Eckel, Pearson Education.
3. Java Programming, D. S. Malik and P. S. Nair, Cengage Learning.
4. Core Java, Volume 1, 9th edition, Cay S. Horstmann and G Cornell, Pearson.
OBJECTIVES:
To teach the students basics of JAVA programs and its execution.
To teach the students the differences between C++ and Java programming.
To make the students learn concepts like packages and interfaces.
To make the students understand life cycle of the applets and its functionality.
To make the students understand the usage util package.
To teach the student, to develop java programs using interfaces.
Recommended System/Software Requirements:
Intel based desktop PC with minimum of 2.6GHZ or faster processor with at least 256
MB RAM and 40GB free disk space.
Operating system: Flavor of any WINDOWS.
Software:j2sdk1.7.
Linux and MvSQL.
Eclipse or Net beam.
INTRODUCTION TO OOP
Object-oriented programming (OOP) is a computer science term used to characterize a
programming language that began development in the 1960’s. The term ‘object-oriented
programming’ was originally coined by Xerox PARC to designate a computer application that
describes the methodology of using objects as the foundation for computation. By the 1980’s,
OOP rose to prominence as the programming language of choice, exemplified by the success of C+
+. Currently, OOPs such as Java, J2EE, C++,C=, Visual [Link], Python and java Script
are popular OOP programming languages that any career-oriented Software Engineer or
developer should be familiar with.
OOP is widely accepted as being far more flexible than other computer programming
languages. OOPs use three basic concepts as the fundamentals for the Abstraction,
Polymorphism, Event Handling and Encapsulation are also significant concepts within object-
oriented programming languages that are explained in online tutorial describing the functionality
of each concept in detail.
The java platform is undoubtedly fast moving and comprehensive. Its many application
programming interfaces (APIs) provide a wealth of functionality for all aspects of application
and system-level programming. Real-world developers never use one or two APIs to solve a
problem, but bring together key functionality spanning a number of APIs, Knowing which APIs
you need,
which parts of which APIs you need, and how the APIs work together to create the best solution
can be a daunting task.
4
1. GUIDELINES TO STUDENTS
1. Equipment in the lab for the use of student community. Students need to maintain a proper
decorum in the computer lab. Students must use the equipment with care. Any damage is caused
is punishable.
2. Students are instructed to come to lab in formal dresses only.
3. Students are supposed to occupy the systems allotted to them and are not supposed to talk or
make noise in the lab.
4. Students are required to carry their observation book and lab records with completed exercises
while entering the lab.
5. Lab records need to be submitted every week.
6. Students are not supposed to use pen drives in the lab.
5
2. LAB OBJECTIVE
To introduce Java compiler and eclipse platform.
To make the student learn an object oriented way of solving problems using java.
To make the students to write programs using multithreading concepts and handle
exceptions.
To make the students to write programs that connects to a database and be able to
perform various operations.
To make the students to create the Graphical User Interface using Applets, AWT
Components & Swing Components.
3. LAB OUTCOME
Able to use Java compiler and eclipse platform to write and execute java program.
Understand and Apply Object oriented features and Java concepts.
Able to apply the concept of multithreading and implement exception handling.
Able to access data from a Database with java program.
Develop applications using Console I/O and File I/O,GUI applications
6
4. List of experiments as per the university curriculum
[Link]. Name of the Program Page No.
1 Week1 :
Use eclipse or Netbean platform and acquaint with the various menus,
create a test project, add a test class and run it see how you can use auto
8-9
suggestions, auto fill. Try code formatter and code refactoring like
renaming variables, methods and classes. Try debug step by step with a
small program of about 10 to 15 lines which contains at least one if else
condition and a for loop.
2 Week 2 :
Write a Java program that works as a simple calculator. Use a grid layout
to arrange buttons for the digits and for the +, -,*, % operations. Add a text 10-13
field to display the result. Handle any possible exceptions like divide by zero.
3 Week 3 :
a) Develop an applet that displays a simple message.
14-16
b) Develop an Applet that receives an integer in one text field & compute its
factorial value & returns it in another text filed when the button “Compute” is
clicked
4. Week 4 :
Write a program that creates a user interface to perform integer divisions. The
user enters two numbers in the text fields, Num1 and Num2. The division of
Num1 and Num2 is displayed in the Result field when the Divide button is 17-19
clicked. If Num1 or Num2 were not an integer, the program would throw a
NumberFormatException. If Num2 were Zero, the program would throw an
Arithmetic Exception Display the exception in a message dialog box
5 Week 5 :
Write a java program that implements a multi-thread application that has
three threads. First thread generates random integer every 1 second and if the 20-22
value is even, second thread computes the square of the number and prints. If
the value is odd, the third thread will print the value of cube of the
number.
6 Week 6 :
23-29
Write a java program that connects to a database using JDBC and does
7
add, deletes, modify and retrieve operations
7 Week 7 :
Write a java program that simulates a traffic light. The program lets the
user select one of three lights: red, yellow, or green with radio buttons. On 30-34
selecting a button, an appropriate message with “stop” or “ready” or “go”
should appear above the buttons in a selected color. Initially there is no
message shown.
8 Week 8 :
Write a java program to create an abstract class named Shape that contains
two integers and an empty method named printArea(). Provide three
classes named Rectangle, Triangle and Circle such that each one of the 35-36
classes extends the class Shape. Each one of the classes contain only the
method printArea( ) that prints the area of the given shape.
9 Week 9 :
Suppose that a table named [Link] is stored in a text file. The first line
in the file header and the remaining lines correspond to row in the table. The 37-39
elements are separated by commas. Write a Java program to display the
table using labels in grid layout.
8
5. List of Additional Experiments
1 Write a Java program that prints all real solutions to the quadratic equation
ax2+bx+c = 0. Read in a, b, c and use the quadratic formula. If the
discriminate b2-4ac is negative, display a message stating that there are no 58-59
real solutions?
2 The Fibonacci sequence is defined by the following rule. The first 2 values
in the sequence are 1, 1. Every subsequent value is the sum of the 2 values
preceding it. Write a Java program that uses both recursive and non- 60-62
recursive functions to print the nth value of the Fibonacci sequence?
3 Write a Java program that prompts the user for an integer and then prints 63
out all the prime numbers up to that Integer?
4 Write a Java program that checks whether a given string is a palindrome or 64
not. Ex: MADAM is a palindrome?
5 Write a Java program for sorting a given list of names in ascending order? 65-66
6 Write a Java program to multiply two given matrices? 67-68
7 Write a Java program that reads a line of integers and then displays each 69-70
integer and the sum of all integers. (use StringTokenizer class)?
8 Write a Java program that reads on file name from the user, then displays 71
information about whether the file exists, whether the file is readable,
whether the file is writable, the type of file and the length of the file in
bytes?
9 Write a Java program that reads a file and displays the file on the screen, 72
with a line number before each line?
9
Solutions:-
[Link] eclipse or Netbean platform and acquaint with
the various menus, create a test project, add a test
class and run it see how you can use auto suggestions,
auto fill. Try code formatter and code refactoring
like renaming variables, methods and classes. Try
debug step by step with a small program of about 10
to 15 lines which contains at least one if else
condition and a for loop.
Program:-
public class Prog1
{
public static void main(String[] args)
{
[Link]("\n Prog. is showing even no");
for(int i=2;i<=20;i++)
{
if(i%2==0)
{
[Link]("\n "+i);
}
}
}
}
Compile:-
D:>javac [Link]
Run:-
D:>java Prog1
10
Output:-
In Netbeans IDE:-
In Command Prompt:-
11
[Link] a Java program that works as a simple calculator. Use
a grid layout to arrange buttons for the digits and for the +, -,*,
% operations. Add a text field to display the result. Handle any
possible exceptions like divide by zero.
Program:-
import [Link].*;
import [Link].*;
import [Link].*;
//<applet code=Calculator height=300 width=200></applet>
public class Calculator extends JApplet
{
public void init()
{
CalculatorPanel calc=new CalculatorPanel();
getContentPane().add(calc);
}
}
class CalculatorPanel extends JPanel implements ActionListener
{
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
static JTextField result=new JTextField("0",45);
static String lastCommand=null;
JOptionPane p=new JOptionPane();
double preRes=0,secVal=0,res;
private static void assign(String no)
{
if(([Link]()).equals("0"))
[Link](no);
else if(lastCommand=="=")
{
[Link](no);
lastCommand=null;
else
[Link]([Link]()+no);
public CalculatorPanel()
{
setLayout(new BorderLayout());
[Link](false);
[Link](300,200);
add(result,[Link]);
JPanel panel=new JPanel();
12
[Link](new GridLayout(4,4));
n7=new JButton("7");
[Link](n7);
[Link](this);
n8=new JButton("8");
[Link](n8);
[Link](this);
n9=new JButton("9");
[Link](n9);
[Link](this);
div=new JButton("/");
[Link](div);
[Link](this);
n4=new JButton("4");
[Link](n4);
[Link](this);
n5=new JButton("5");
[Link](n5);
[Link](this);
n6=new JButton("6");
[Link](n6);
[Link](this);
mul=new JButton("*");
[Link](mul);
[Link](this);
n1=new JButton("1");
[Link](n1);
[Link](this);
n2=new JButton("2");
[Link](n2);
[Link](this);
n3=new JButton("3");
[Link](n3);
[Link](this);
minus=new JButton("-");
[Link](minus);
[Link](this);
dot=new JButton(".");
[Link](dot);
[Link](this);
n0=new JButton("0");
[Link](n0);
[Link](this);
equal=new JButton("=");
[Link](equal);
[Link](this);
plus=new JButton("+");
[Link](plus);
[Link](this);
add(panel,[Link]);
}
13
public void actionPerformed(ActionEvent ae)
{
if([Link]()==n1) assign("1");
else if([Link]()==n2) assign("2");
else if([Link]()==n3) assign("3");
else if([Link]()==n4) assign("4");
else if([Link]()==n5) assign("5");
else if([Link]()==n6) assign("6");
else if([Link]()==n7) assign("7");
else if([Link]()==n8) assign("8");
else if([Link]()==n9) assign("9");
else if([Link]()==n0) assign("0");
else if([Link]()==dot)
{
if((([Link]()).indexOf("."))==-1)
[Link]([Link]()+".");
}
else if([Link]()==minus)
{
preRes=[Link]([Link]());
lastCommand="-";
[Link]("0");
}
else if([Link]()==div)
{
preRes=[Link]([Link]());
lastCommand="/";
[Link]("0");
}
else if([Link]()==equal)
{
secVal=[Link]([Link]());
if([Link]("/"))
res=preRes/secVal;
else if([Link]("*"))
res=preRes*secVal;
else if([Link]("-"))
res=preRes-secVal;
else if([Link]("+"))
res=preRes+secVal;
[Link](" "+res);
lastCommand="=";
}
else if([Link]()==mul)
{
preRes=[Link]([Link]());
lastCommand="*";
[Link]("0");
}
else if([Link]()==plus)
{
preRes=[Link]([Link]());
lastCommand="+";
14
[Link]("0");
}
}
}
Output:-
15
3. a) Develop an applet that displays a simple message.
Program:-
import [Link].*;
import [Link].*;
/*<applet code = “HelloJava” width = 200 height = 60 > </applet>*/
public class HelloJava extends Applet {
public void paint(Graphics g) {
[Link](“Hello Java”, 10, 100);
Output:-
16
3.b) Develop an Applet that receives an integer in one text field
& compute its factorial value & returns it in another text filed
when the button “Compute” is clicked.
Program:-
import [Link].*;
import [Link];
import [Link].*;
import [Link];
public class Fact extends Applet implements ActionListener
String str;
Button b0;
TextField t1,t2;
Label l1;
public void init()
{ Panel p=new Panel();
[Link](new GridLayout());
add(new Label("Enter any Integer value"));
add(t1=new TextField(20));
add(new Label("Factorial value is: "));
add(t2=new TextField(20));
add(b0=new Button("compute"));
[Link](this);
public void actionPerformed(ActionEvent e)
int i,n,f=1;
17
n=[Link]([Link]());
for(i=1;i<=n;i++)
f=f*i;
[Link]([Link](f));
repaint();
Output:-
18
[Link] a program that creates a user interface to perform
integer divisions. The user enters two numbers in the text
fields, Num1 and Num2. The division of Num1 and Num2 is
displayed in the Result field when the Divide button is clicked.
If Num1 or Num2 were not an integer, the program would
throw a NumberFormatException. If Num2 were Zero, the
program would throw an Arithmetic Exception Display the
exception in a message dialog box.
Program:-
import [Link].*;
import [Link].*;
import [Link].*;
public class Add1 extends Applet implements ActionListener
{
String msg;
TextField num1, num2, res;
Label l1, l2, l3;
Button div;
public void init()
{
l1 = new Label("Number 1");
l2 = new Label("Number 2");
l3 = new Label("result");
num1 = new TextField(10);
num2 = new TextField(10);
res = new TextField(30);
div = new Button("DIV");
[Link](this);
add(l1);
add(num1);
add(l2);
add(num2);
add(l3);
add(res);
add(div);
}
public void actionPerformed(ActionEvent ae)
{
String arg = [Link]();
19
if ([Link]("DIV"))
{
String s1 = [Link]();
String s2 = [Link]();
int num1 = [Link](s1);
int num2 = [Link](s2);
if (num2 == 0)
{
msg = "Arithemetic Exception ";
repaint();
}
else if ((num1 < 0) || (num2 < 0))
{
msg = "NumberFormat Exception";
repaint();
}
else
{
int num3 = num1 / num2;
msg = [Link](num3);
}
[Link](msg);
}
}
public void paint(Graphics g)
{
//[Link](msg, 30, 70);
}
}
[Link]
<html>
<head>
</head>
<body>
/*<applet code="[Link]"width=350 height=300>
</applet>*/
</body>
</html>
20
Output:-
21
5.) Write a java program that implements a multi-thread
application that has three threads. First thread generates
random integer every 1 second and if the value is even, second
thread computes the square of the number and prints. If the
value is odd, the third thread will print the value of cube of the
number.
Program:-
class RandomGenThread implements Runnable
{
double num;
public void run()
{
try {
SquareThread sqt = new SquareThread();
Thread squareThread = new Thread(sqt);
CubeThread cbt = new CubeThread();
Threadcube Thread = new Thread(cbt);
[Link]();
[Link]();
for(int i=0;i<10;i++)
{
22
S tNum(new Double(i));
y }
s else
t {
e [Link](new Double(i));
m }
. [Link](1000);
o }
u } catch (InterruptedException e)
t {
. [Link]();
p }
r }
i
n }
t class SquareThread implements Runnable
l {
n D
( o
" u
t b
1 l
- e
"
+ n
i u
) m
; ;
i
f p
( u
i b
% l
2 i
c
=
= v
o
0 i
) d
{
s r
q u
t
n
.
s (
e )
23
{
try { i++;
int i=0; if(num != null&&num %2 ==0)
do{ {
[Link]("t2--->square of "+num+"="+(num*num));
num = null;
}
[Link](1000);
}while(i<=5);
}
catch (Exception e)
{
[Link]();
}
}
public Double getNum()
{
return num;
}
public void setNum(Double num)
{
[Link] = num;
}
}
class CubeThread implements Runnable
{
Double num;
public void run()
{
try {
int i=0;
do{
i++;
if(num != null&&num%2 !=0)
{
[Link]("t3-->Cube of "+num+"="+(num*num*num));
num=null;
}
[Link](1000);
}
while(i<=5);
24
}
catch (Exception e)
{
[Link]();
}
}
public Double getNum()
{
return num;
}
public void setNum(Double num)
{
[Link] = num;
}
}
public class MultiThreaded
{
public static void main(String[] args) throws InterruptedException
{
Thread randomThread = new Thread(new RandomGenThread());
[Link]();
}
}
Output:-
25
6).Write a java program that connects to a database using JDBC
and does add, deletes, modify and retrieve operations?
Program:-
[Link]
import [Link];
import [Link];
import [Link];
public class ConnectionUtil
public static Connection getConnection() throws SQLException
Connection connection = null;
try
[Link]("[Link]");
connection = [Link]("jdbc:mysql://[Link]:3306/vijju","root",
"root");
catch (ClassNotFoundException e)
[Link]();
catch (SQLException e)
[Link]();
[Link]("message for connection open"+connection);
return connection;
26
}
[Link]
public class StudentDetails
private long st_id;
private String st_name;
private long st_mobile;
public StudentDetails(long st_id,String st_name,long st_mobile)
this.st_id = st_id;
this.st_name = st_name;
this.st_mobile = st_mobile;
public long getSt_id()
return st_id;
public void setSt_id(long st_id)
this.st_id = st_id;
public String getSt_name()
return st_name;
public void setSt_name(String st_name)
27
this.st_name = st_name;
public long getSt_mobile()
return st_mobile;
public void setSt_mobile(long st_mobile)
this.st_mobile = st_mobile;
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
public class Prog6
String createTableQuery = "create table test.student_details (st_namevarchar(50), st_mobile
numeric(10), st_id numeric(10))";
public static void main(String[] args)
{ Connection conn = null;
try
conn = [Link]();
Prog6 prog6 = new Prog6();
[Link]("Student_details table data before inserting:");
[Link](conn);
28
StudentDetails st1 = new StudentDetails(1, "GNIT", 23232323);
StudentDetails st2 = new StudentDetails(2, "GNEC", 24242424);
StudentDetails st3 = new StudentDetails(3, "GNITC", 25252525);
[Link](conn, st1);
[Link](conn, st2);
[Link](conn, st3);
[Link]("Student_details table data after inserting:");
[Link](conn);
[Link](conn,2);
[Link]("Student_details table data after deleting:");
[Link](conn);
[Link](conn, 26262626, 3);
[Link]("Student_details table data after modifying:");
[Link](conn);
} catch (SQLException e)
[Link]();
} finally
try {
[Link]();
} catch (SQLException e)
[Link]();
private void modifyData(Connection conn, long st_mobile, long st_id)
29
{
String query = "update student_details set st_mobile=? wherest_id=?";
try {
PreparedStatement pstmt = [Link](query);
[Link](1, st_mobile);
[Link](2, st_id);
int row = [Link]();
//[Link](row);
} catch (SQLException e)
{ [Link]();
private void deleteARow(Connection conn, long st_id)
String query = "delete from student_details where st_id=?" ;
try
PreparedStatement pstmt = [Link](query);
[Link](1, st_id);
int row = [Link]();
//[Link](row);
catch (SQLException e)
[Link]();
private void insertData(Connection conn, StudentDetails details)
30
{
String query = "insert into test.student_details values (?,?,?)";
try
PreparedStatement pstmt = [Link](query);
[Link](1,details.getSt_name());
[Link](2, details.getSt_mobile());
[Link](3, details.getSt_id());
int row = [Link]();
//[Link](row);
catch (SQLException e)
[Link]();
private void retrieveData(Connection conn)
try {
String query = "select * from test.student_details";
PreparedStatement pstmt = [Link](query);
ResultSet rs = [Link]();
[Link]("ST_ID\tST_NAME\t\tST_MOBILE");
while([Link]())
[Link]([Link]("st_id")+"\t"+[Link]("st_name")+"\t\t"+[Link](
"st_mobile"));
31
}
catch (SQLException e)
[Link]();
Output:-
32
7) Write a java program that simulates a traffic light.
The program lets the user select one of three lights:
red, yellow, or green with radio buttons. On selecting a
button, an appropriate message with “stop” or “ready”
or “go” should appear above the buttons in a selected
color. Initially there is no message shown.
Program:-
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class TrafficSignal extends Applet implements Runnable
Thread t;
Font f, f1;
int i = 0, a = 0, j = 0;
public void init()
setBackground([Link]);
f = new Font("TimesNewRoman", [Link], 28);
f1 = new Font("TimesNewRoman", [Link] + [Link], 28);
public void start()
t = new Thread(this);
[Link]();
public void run()
{
33
for (i = 10; i >= 0; i--)//countdown
{
try
[Link](1000);
catch (Exception e)
[Link](e);
if (i <= 10 && i > 3)//red
a = 1;
repaint();
else if (i <= 3 && i > 0)//yellow
a = 2;
repaint();
else if (i == 0)//green
for (j = 0; j < 10; j++)
a = 3;
try
[Link](1000);
}
34
catch (Exception e)
{
[Link](e);
repaint();
if (j == 10)//end of green(return to red)
run();
repaint();
public void paint(Graphics g)
setBackground([Link]);//ROAD
[Link]([Link]);//POLE UP
[Link](150, 150, 50, 150);
[Link](150, 150, 50, 150);
[Link]([Link]);//POLE DOWN
[Link](165, 300, 20, 155);
[Link](165, 300, 20, 155);
[Link](150, 150, 50, 50);//RED
[Link](150, 200, 50, 50);//YELLOW
[Link](150, 250, 50, 50);//GREEN
[Link]([Link]);//COUNTDOWN STOP
[Link](f);
[Link]("" + i, 50, 50);
35
if (a == 1)//REDSIGNAL
{
[Link]([Link]);
[Link](150, 150, 50, 50);
[Link](150, 150, 50, 50);
[Link]("STOP", 50, 150);
if (a == 2)//YELLOWSIGNAL
[Link]([Link]);
[Link](150, 200, 50, 50);
[Link](150, 200, 50, 50);
[Link]("READY", 50, 200);
if (a == 3)//GREENSIGNAL
{
[Link]([Link]);//countdown
[Link](f);
[Link]("" + j, 150, 50);
[Link]([Link]);
[Link](150, 250, 50, 50);
[Link](150, 250, 50, 50);
[Link]("GO", 50, 250);
}
int x1[] = {220, 300, 300, 280};
int y1[] = {250, 150, 250, 150};
int n1 = 4;
int n2 = 3;
int x2[] = {340, 380, 380};
int y2[] = {150, 100, 150};
int x3[] = {460, 460, 500};
36
int y3[] = {150, 100, 150};
[Link]
<html>
<head>
</head>
<body>
/*<applet code="[Link]" height=500 width=300></applet>*/
</body>
</html>
Output:-
37
8) Write a java program to create an abstract class
named Shape that contains two integers and an empty
method named printArea(). Provide three classes named
Rectangle, Triangle and Circle such that each one of the
classes extends the class Shape. Each one of the classes
contain only the method printArea( ) that prints the area
of the given shape.
Program:-
abstract class Shape
abstract void numberOfSides();
class Trapezoid extends Shape
void numberOfSides()
[Link](" Trapezoidal has four sides");
class Triangle extends Shape
void numberOfSides()
[Link]("Triangle has three sides");
class Hexagon extends Shape
void numberOfSides()
38
{
[Link]("Hexagon has six sides");
}
}
class ShapeDemo
{
public static void main(String args[ ])
{
Trapezoid t=new Trapezoid();
Triangle r=new Triangle();
Hexagon h=new Hexagon();
Shape s;
s=t;
[Link]();
s=r;
[Link]();
s=h;
[Link]();
}
}
Output:-
39
9) Suppose that a table named [Link] is stored in a text file. The first
line in the file header and the remaining lines correspond to row in the table.
The elements are separated by commas. Write a Java program to display the
table using labels in grid layout.
Program:-
import [Link].*; import
[Link].*; import [Link].*;
import [Link].*; import [Link].*;
public class Table1 extends JFrame
int i=0;
int j=0,k=0;
Object data[][]=new Object[5][4]; Object list[][]=new
Object[5][4]; JButton save;
JTable table1; FileInputStream fis;
DataInputStream dis; public Table1()
String d= " ";
Container con=getContentPane(); [Link](new
BorderLayout());
final String[] colHeads={"Name","Roll Number","Department","Percentage"}; try
String s=[Link]("Enter the File name present in the current directory"); FileInputStream fis=new
FileInputStream(s);
DataInputStream dis = new DataInputStream(fis);
while ((d=[Link]())!=null)
{
StringTokenizer st1=new StringTokenizer(d,","); while
([Link]())
{
40
for (j=0;j<4;j++)
{
data[i][j]=[Link](); [Link](data[i][j]);
} i++;
}
[Link] (" ");
}
} catch (Exception e)
{
[Link] ("Exception raised" +[Link]());
}
table1=new JTable(data,colHeads);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane scroll=new
JScrollPane(table1,v,h); [Link](scroll,[Link]);
}
public static void main(String args[])
{
Table1 t=new Table1();
[Link]([Link]); [Link]("Display
Data"); [Link](500,300); [Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
} }
[Link]:-
a,123,der,23 b,456,frg,45
Output:-
41
42