OOPS Through Java LabManual-R22
OOPS Through Java LabManual-R22
com
PROGRAMS
Week 1.
Aim: Use Eclipse or Net bean 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.
Solution:
www.btechsmartclass.com
Page | 3
OOPS Through Java Programming
www.btechsmartclass.com
Then, the
installation
process begins.
Page | 4
OOPS Through Java Programming
www.btechsmartclass.com
Page | 5
OOPS Through Java Programming
www.btechsmartclass.com
Click on Accept
Now.
Page | 6
OOPS Through Java Programming
smartclass.com
Click on Accept
www.btechsmartclass.com
OOPS Through
Java
Programming
Click on Select
All and Accept Selected.
After completing,
click on Launch to
start the Eclipse
IDE.
www.btechsmartclass.com
OOPS Through
Java
Programming
Page | 7
Page | 8
OOPS Through Java Programming
www.btechsmartclass.com
Browse the Workspace for storing the java project and click on Launch.
Select "Create
a new Java
project".
Page | 9
OOPS Through Java Programming
www.btechsmartclass.com
Page | 10
OOPS Through Java Programming
www.btechsmartclass.com
Page | 11
OOPS Through Java Programming
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
Page | 15
www.btechsmartclass.com
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 field to display the result. Handle any possible exceptions like divided by zero.
Source Code:
import
java.awt.*; import
java.awt.event.*;
import java.applet.*;
/*
* */
{
add(b[i]);
}
add(add); add(sub);
add(mul); add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this); mul.addActionListener(this);
div.addActionListener(this);
Page | 16
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("+"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("-"))
{
v1=Integer.parseInt(t1.getText()); OP='-'; t1.setText("");
}
else if(str.equals("*"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
} else
if(str.equals("/"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("%")){
v1=Integer.parseInt(t1.getText());
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
OP='%';
t1.setText("");
}
if(str.equals("=")){
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2; else
if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("Clear")
)
{
t1.setText(""
);
}
}
}
Page | 17
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
Output:
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
Page | 18
Week 3:
// Import the packages to access the classes and methods in awt and applet
classes. import java.awt.*;
import java.applet.*;
Output:
OOPS Through Java Programming
Page | 19
www.btechsmartclass.com
Source code for question b:
import java.awt.*;
import
java.awt.event.*;
import java.applet.Applet;
www.btechsmartclass www.btechsmartclass.com
Output:
Page | 20
Week 4:
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.
Source code:
import java.awt.*;
import
java.awt.event.*;
import java.applet.*;
res.setText(String.valueOf(num3));
msg = "Operation Succesfull!!!";
repaint();
} catch (NumberFormatException ex) {
System.out.println(ex);
res.setText("");
msg = "NumberFormatException - Non-numeric";
Page | 21
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
repaint();
} catch (ArithmeticException e) {
System.out.println("Can't be divided by Zero" + e);
res.setText("");
msg = "Can't be divided by Zero";
repaint();
}
}
}
}
Page | 22
www.btechsmartclass
Page | 23
www.btechsmartclass.com
www.btechsmartclass
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 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.
Source code:
import java.util.Random;
SquareThread(int randomNumbern) {
number = randomNumbern;
}
CubeThread(int randomNumber) {
number = randomNumber;
}
Page | 24
www.btechsmartclass
Output:
Page | 25
www.btechsmartclass.com www.btechsmartclass
OOPS Through Java Programming
Week6:
Source code:
class Node {
int data;
Node previous;
Node next;
if (head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
} else {
tail.next = newNode;
newNode.previous =
tail;
tail = newNode;
tail.next = null;
}
}
public void display() {
Node current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while (current != null) {
dList.display();
}
}
Page | 26
www.btechsmartclass www.btechsmartclass.com
Output:
Page | 27
OOPS Through Java Programming
www.btechsmartclass.com
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 selecting a button, an appropriate message with “Stop” or “Ready”
or “Go” should appear above the buttons in selected color. Initially, there is no message shown.
Source code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*
* <applet code = "TrafficLightsExample" width = 1000 height = 500>
* </applet>
* */
redLight.addItemListener(this);
yellowLight.addItemListener(this);
greenLight.addItemListener(this);
add(redLight);
add(yellowLight);
add(greenLight);
add(msg);
msg.setFont(new Font("Serif", Font.BOLD, 20));
}
public void itemStateChanged(ItemEvent ie) {
redLight.setForeground(Color.BLACK);
yellowLight.setForeground(Color.BLACK);
greenLight.setForeground(Color.BLACK);
if(redLight.getState() == true) {
redLight.setForeground(Color.RED);
Page | 28
msg.setForeground(Color.RED);
msg.setText("STOP");
}
else if(yellowLight.getState() == true) {
yellowLight.setForeground(Color.YELLOW);
msg.setForeground(Color.YELLOW);
msg.setText("READY");
}
else{
greenLight.setForeground(Color.GREEN);
msg.setForeground(Color.GREEN);
msg.setText("GO");
}
}
}
www.btechsmartclass.com OOPS
Through Java Programming
www.btechsmartclass
Output:
Page | 29
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
Week 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.
Source code:
import java.util.*;
Page | 30
public class AbstractClassExample {
public static void main(String[]
args) {
Rectangle rec = new Rectangle();
rec.printArea();
www.btechsmartclass.com
Output:
Page | 31
www.btechsmartclass.com OOPS
Through Java Programming
www.btechsmartclass
Week 9:
Suppose that a table named Table.txt 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.
Source code:
import java.io.*;
import
java.util.*; import
java.awt.*;
import javax.swing.*;
Page | 32
public static void main(String[]
args) { A a = new A();
}
}
www.btechsmartclass www.btechsmartclass.com
Output:
Page | 33
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.co
Week 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).
Source code:
import
java.awt.*; import
java.applet.*; import
java.awt.event.*;
Page | 35
OOPS Through Java Programming
www.btechsmartclass
public void
mouseExited(MouseEvent me) {
mx = 40;
my = 80;
msg = "Mouse Exited";
repaint();
}
repaint();
}
Page | 36
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
www.btechsm
www.btechsmartclass
OOPS Through Java Programming
Output:
Week 11:
Write a java program that loads names and phone numbers from a text file where the data is organized as
one line per record and each field in a record are separated by a tab (\t).it takes a name or phone number as
input and prints the corresponding other value from the hash table(hint: use hash tables)
Source code:
Page | 37
import
java.io.BufferedReader; import
java.io.File;
import
java.io.FileNotFoundException;
import java.io.FileReader; import
java.io.IOException; import
java.util.Hashtable; import
java.util.Iterator;
import java.util.Set;
Page | 38
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
Page | 39
OOPS Through Java Programming
www.btechsmartclass www.btechsmartclass.com
Output:
Week – 12
Write a Java program that correctly implements the producer – consumer problem using the concept of
interthread communication.
Source Code:
class
ItemQueue { int
item;
boolean valueSet = false;
www.btechsmartclass www.btechsmartclass.com
}
System.out.println("Consummed:" + item);
valueSet =
false; try {
Thread. sleep(1000);
} catch (InterruptedException e) {
System. out.println("InterruptedException caught");
}
notify();
return item;
}
}
}
.com
class Consumer implements Runnable{
Page | 41
OOPS Through Java Programming
ItemQueue itemQueue;
Consumer(ItemQueue itemQueue){
this .itemQueue = itemQueue;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
itemQueue .getItem();
}
}
}
class ProducerConsumer{
public static void main(String
args[])
{
ItemQueue itemQueue = new
ItemQueue(); new
Producer(itemQueue); new
Consumer(itemQueue);
}
}
Output:
Page | 42
www.btechsmartclass.com OOPS
Through Java Programming
www.btechsmartclass
Week – 13
Write a Java program to list all the files in a directory including the files present in all its subdirectories.
Source Code:
import java.util.Scanner;
import java.io.*;
Page | 43
public static void printFiles(String path) {
System. out.println("Current Location: " +
path);
File f_ref = new File(path);
File[] filesList =
f_ref.listFiles(); for (File
file : filesList) {
if (file.isFile())
System. out.println("- " +
file.getName()); else
System. out.println("> " + file.getName());
}
}
public static void printLine() {
System.out.println("----------------------------------------");
}
}
www.btechsmartclass www.btechsmartclass.com
Output:
Page | 44