OOPS Lab Manual
OOPS Lab Manual
OOPS Lab Manual
KCA-251
Session: 2021-22
INDEX
UNIT 1: Java Basic Programs:
1. Write a Java program to insert 3 numbers from keyboard and find out greater number
among 3 numbers.
2. Write a Java program to find out the sum of command line arguments.
3. Write a Java program to create a Room class, the attributes of this class is roomno,
roomtype, roomarea and ACmachine. In this class the member functions are setData and
displayData. Use member function to set data and display that data using displayData()
method.
4. Write a Java program to create a class “SimpleObject” and display message by using
constructor of this class.
5. Write a program to demonstrate static variables, methods, and blocks.
When any of these validations fail, then raise a user defined exception
InvalidInputException
Create a class TestCustomer having main method. Ask user to enter customer details.
Create an object of Customer and perform validations. Print details of customer.
5. Write a Java program to create a text file in the path c:\Java\abc.txt and check whether
that file exists or not. Using the command exists(), isDirectory(), isFile(), getName() and
getAbsolutePath().
Create a class ThreadDemo having main method. Create 2 objects of MyThread class and
observe the behavior of threads
2. Modify the above to create MyThread class by implementing Runnable interface and
observe the behavior of threads.
Write a program that creates an instance of the Storage class and set up a Counter and
Printer object to operate on it.
UNIT 5: Java GUI Applications:
1. Design and implement a GUI for the Temperature class. One challenge of this design
is to find a good way for the user to indicate whether a Fahrenheit or Celsius value is
being input. This should also determine the order of the conversion: F to C or C to F.
[C/5 = (F -32)/9]
2. Design and implement a GUI application for Calculator same as Windows OS
calculator.
3. Design and create small notepad type application using Swing/AWT.
Unit -1
Experiment-1
Objective: JAVA basic programs
Scheduled Date Compiled Date Submission Date
10-April-2021 12-April-2021 19-April-2021
import java.util.Scanner;
class Greatest
{
int num1,num2,num3;
void getdata()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter number1=");
num1=input.nextInt();
System.out.println("Enter number2=");
num2=input.nextInt();
System.out.println("Enter number3=");
num3=input.nextInt();
if(num1>=num2&&num1>=num3)
{
System.out.println("greatest"+num1);
}
else if(num2>=num1&&num2>=num3)
{
System.out.println("greatest"+num2);
}
else if(num3>=num1&&num3>=num2)
{
System.out.println("greatest"+num3);
}
else
{
System.out.println("all are equal");
}
}
}
public class GreatestMain {
Output:-
Experiment-2
Objective: JAVA basic programs
Scheduled Date Compiled Date Submission Date
10-April-2021 12-April-2021 19-April-2021
sum=sum+Integer.parseInt(data);
}
/*for(String data:args)
{
sum=sum+Integer.parseInt(data);
}*/
System.out.println(sum);
}
else
{
System.out.println("you did not pass
any command line argument");
}
}
}
Output:-
Experiment-3
Objective: JAVA basic programs
Scheduled Date Compiled Date Submission Date
10-April-2021 12-April-2021 19-April-2021
import java.util.Scanner;
class Room
{
int roomno;
String roomtype,roomarea,acmachine;
void setData()
{
Scanner input = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
System.out.println("Enter room no=");
roomno=input.nextInt();
System.out.println("Enter room type=");
roomtype=sc.nextLine();
System.out.println("Enter room area=");
roomarea=sc.nextLine();
System.out.println("Enter AC machine=");
acmachine=sc.nextLine();
}
void displayData()
{
System.out.println("room no:"+roomno);
System.out.println("room type:"+roomtype);
System.out.println("room area:"+roomarea);
System.out.println("AC machine:"+acmachine);
}
}
public class MainRoom {
Output:-
Experiment-4
Objective: JAVA basic programs
Scheduled Date Compiled Date Submission Date
10-April-2021 12-April-2021 19-April-2021
class SimpleObject
{
SimpleObject()
{
System.out.println("I am in class simple
object default constructor");
}
SimpleObject(int a)
{
System.out.println("I am in super class
parameterized constructor");
}
}
public class MainSimple {
Output:-
Experiment-5
Objective: JAVA basic programs
Scheduled Date Compiled Date Submission Date
10-April-2021 12-April-2021 19-April-2021
}
public static void m1()
{
System.out.println(j);
}
static
{
System.out.println("second static block");
}
static int j=20;
}
Output:-
Unit -2
Experiment-1
Objective: JAVA object oriented programs
Scheduled Date Compiled Date Submission Date
20-April-2021 25-April-2021 04-May-2021
}
class Circle extends Shape
{
void draw() //overriding
{
System.out.println("circle is draw");
}
void erase() //overriding
{
System.out.println("circle is erase");
}
}
class Triangle extends Shape
{
void draw(int x) //overloading
{
System.out.println("Triangle is draw");
}
void erase() //overriding
{
System.out.println("Triangle is erase");
}
}
class Square extends Shape
{
void draw() //overriding
{
System.out.println("square is draw");
}
void erase() //overriding
{
System.out.println("square is erase");
}
}
public class ShapeMain {
Experiment-2
Objective: JAVA object oriented programs
Scheduled Date Compiled Date Submission Date
20-April-2021 25-April-2021 04-May-2021
Program:- Write a Java program to give a simple example for
abstract class.
Output:-
Experiment-3
Objective: JAVA object oriented programs
Scheduled Date Compiled Date Submission Date
20-April-2021 25-April-2021 04-May-2021
class A
{
String msg="hello i am in class";
void getmsg()
{
System.out.println(msg);
}
}
class B extends A //single inheritance
{
void getBmsg()
{
System.out.println("this is new msg by class
B");
}
}
class C extends B //multilevel inheritance supported
{
C()
{
System.out.println("this is class C");
}
}
/*class c extends A,B //multiple inheritance not
supported
{
}*/
public class InheritanceEx {
Output:-
Experiment-4
Objective: JAVA object oriented programs
Scheduled Date Compiled Date Submission Date
20-April-2021 25-April-2021 04-May-2021
class Number
{
private double n;
Number(int n)
{
this.n=n;
}
public boolean isZero()
{
if(n==0)
return true;
else
return false;
}
public boolean isPositive()
{
if(n>0)
return true;
else
return false;
}
public boolean isNegative()
{
if(n<0)
return true;
else
return false;
}
public boolean isOdd()
{
if(n%2!=0)
return true;
else
return false;
}
public boolean isEven()
{
if(n%2==0)
return true;
else
return false;
}
public boolean isPrime()
{
double a=2,f=0;
while(a<n)
{
if(n%a==0)
{
f=1;
break;
}
a++;
}
if(f==0)
return true;
else
return false;
}
public boolean isArmstrong()
{
double m;
double temp,sum=0;
temp=n;
while(n>0)
{
m=n%10;
sum=sum+m*m*m;
n=n/10;
}
if(sum==temp)
return true;
else
return false;
}
}
public class NumberMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Number n1= new Number(10);
System.out.println("zero="+n1.isZero());
System.out.println("positive="+n1.isPositive());
System.out.println("nagative="+n1.isNegative());
System.out.println("odd="+n1.isOdd());
System.out.println("even="+n1.isEven());
System.out.println("prime="+n1.isPrime());
System.out.println("armstrong="+n1.isArmstrong())
;
}
}
Output:-
Unit -3
Experiment-1
Objective: JAVA Exception Handling
Scheduled Date Compiled Date Submission Date
25-May-2021 01-June-2021 03-June-2021
import java.io.File;
import java.io.IOException;
Output:-
Experiment-2
Objective: JAVA Exception Handling
Scheduled Date Compiled Date Submission Date
25-May-2021 01-June-2021 03-June-2021
Output:-
Experiment-3
Objective: JAVA Exception Handling
Scheduled Date Compiled Date Submission Date
25-May-2021 01-June-2021 03-June-2021
}
class CanVote
{
int age;
void checkAge(int age) throws CheckAgeException
{
if(age<18)
{
throw new CheckAgeException("your age should
be greater then 18");
}
else
{
System.out.println("you can vote");
}
}
}
public class UserDefineException {
}
Output:-
Experiment-4
Objective: JAVA Exception Handling
Scheduled Date Compiled Date Submission Date
25-May-2021 01-June-2021 03-June-2021
package exceptionhandling;
import java.util.Scanner;
@SuppressWarnings("serial")
class InvalidInputException extends Exception{
String msg;
InvalidInputException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
class Customer
{
String custNo,custName,category;
Customer(String custNo,String custName,String category)
{
this.custNo=custNo;
this.custName=custName;
this.category=category;
}
public void customerNo()
{
try
{
if(custNo.charAt(0)=='c'||custNo.charAt(0)=='C')
{
System.out.println(custNo);
}
else
{
throw new InvalidInputException("customer no
is not valid");
}
}catch(InvalidInputException e)
{
System.out.println(e.toString());
}
}
public void customerName()
{
try {
if(custName.length()>=4)
{
System.out.println(custName);
}
else
{
throw new InvalidInputException("customer
name is not valid");
}
}catch(InvalidInputException e)
{
System.out.println(e.toString());
}
}
public void custCategory()
{
try {
if(category.equals("Platinum")||category.equals("Gold")|
|category.equals("Silver"))
{
System.out.println(category);
}
else
{
throw new InvalidInputException("customer
category is not valid");
}
}catch(InvalidInputException e)
{
System.out.println(e.toString());
}
}
}
Output:-
Experiment-5
Objective: JAVA Exception Handling
Scheduled Date Compiled Date Submission Date
25-May-2021 01-June-2021 03-June-2021
package filehandling;
import java.io.File;
System.out.println("extension:"+path.substring(path.lastInde
xOf('.')));
System.out.println("size :"+file.getTotalSpace());
System.out.println(" ");
}
}
Output:-
Experiment-6
Objective: JAVA Exception Handling
Scheduled Date Compiled Date Submission Date
25-May-2021 01-June-2021 03-June-2021
Read employee details from the file and display those details.
package filehandling;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
Output:-
Unit -4
Experiment-1
Objective: JAVA Multi Threading programs
Scheduled Date Compiled Date Submission Date
05-June-2021 05-June-2021 05-June-2021
package Threading;
}
}
class MyThread extends Thread
{
MyThread(String name)
{
super(name);
}
public void run()
{
System.out.println("Thread
name="+Thread.currentThread().getName());
}
}
Output:-
Experiment-2
Objective: JAVA Multi Threading programs
Scheduled Date Compiled Date Submission Date
10-June-2021 12-June-2021 12-June-2021
package Threading;
}
class MyRThread implements Runnable
{
public void run() {
System.out.println("Thread
name="+Thread.currentThread().getName());
}
}
Output:-
Experiment-3
Objective: JAVA Multi Threading programs
Scheduled Date Compiled Date Submission Date
13-June-2021 14-June-2021 14-June-2021
package Threading;
class MyPriorityThread extends Thread
{
MyPriorityThread(String s)
{
super(s);
start();
}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Thread
Name:"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Output:-
Experiment-4
Objective: JAVA Multi Threading programs
Scheduled Date Compiled Date Submission Date
15-June-2021 16-June-2021 16-June-2021
package Threading;
Storage st;
public Counter(Storage store){
st = store;
}
@Override
public void run() {
synchronized(st) {
for(int i = 0 ; i < 10; i++){
while(!st.isPrinted()) {
//loop to take care of spontaneous wake-ups
try {
st.wait();
} catch(Exception e) { }
}
st.setValue(i);
st.setPrinted(false);
st.notify();
}
}
}
}
class Printer implements Runnable{
Storage st;
public Printer(Storage st){
this.st = st;
}
@Override
public void run() {
synchronized(st) {
for(int i = 0; i < 10; i++) {
while(st.isPrinted()) {
//loop to take care of spontaneous wake-ups
try {
st.wait();
} catch(Exception e) { }
}
}
class Storage{
int i;
boolean printed = true;
public void setValue(int i){
this.i = i;
}
public int getValue(){
return this.i;
}
public boolean isPrinted() {
return printed;
}
public void setPrinted(boolean p) {
printed = p;
}
}
class MainClass {
public static void main(String[] args) {
Storage st = new Storage();
Counter c = new Counter(st);
Printer p = new Printer(st);
new Thread(c,"Counter").start(); //start the
counter
new Thread(p,"Printer").start(); //start the
printer
}
}
Output:-
Unit -5
Experiment-1
Objective: JAVA GUI Applications
Scheduled Date Compiled Date Submission Date
15-June-2021 16-June-2021 16-June-2021
package SwingPrograms;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public temp() {
setTitle("Temperature Conversion");
Container c = getContentPane();
c.setLayout(new GridLayout(1, 4));
c.add(celsiusLabel);
c.add(celsiusTF);
c.add(fahrenheitLabel);
c.add(fahrenheitTF);
celsiusTF.addActionListener(celsiusHandler);
fahrenheitTF.addActionListener(fahrenheitHandler);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Output:-
Experiment-2
Objective: JAVA GUI Applications
Scheduled Date Compiled Date Submission Date
20-June-2021 21-June-2021 21-June-2021
package SwingPrograms;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class Calculator extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
// default constructor
Calculator()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClass
Name());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
// create a textfield
l = new JTextField(16);
// set the textfield to non editable
l.setEditable(false);
// equals button
beq1 = new JButton("=");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
// convert it to string
s0 = Double.toString(te);
s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
// convert it to string
s0 = Double.toString(te);
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
class editor extends JFrame implements ActionListener {
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
editor()
{
// Create a frame
f = new JFrame("editor");
try {
// Set metal look and feel
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAn
dFeel");
// Text component
t = new JTextArea();
// Create a menubar
JMenuBar mb = new JMenuBar();
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi9);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
mc.addActionListener(this);
mb.add(m1);
mb.add(m2);
mb.add(mc);
f.setJMenuBar(mb);
f.add(t);
f.setSize(500, 500);
f.show();
}
// If a button is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("cut")) {
t.cut();
}
else if (s.equals("copy")) {
t.copy();
}
else if (s.equals("paste")) {
t.paste();
}
else if (s.equals("Save")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
if (r == JFileChooser.APPROVE_OPTION) {
try {
// Create a file writer
FileWriter wr = new FileWriter(fi,
false);
// Write
w.write(t.getText());
w.flush();
w.close();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f,
evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "the user
cancelled the operation");
}
else if (s.equals("Print")) {
try {
// print the file
t.print();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f,
evt.getMessage());
}
}
else if (s.equals("Open")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsOpenDialog function to show
the save dialog
int r = j.showOpenDialog(null);
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new
BufferedReader(fr);
// Initialize sl
sl = br.readLine();
// Main class
public static void main(String args[])
{
editor e = new editor();
}
}
Output:-