oops batch 1
oops batch 1
Write a program to create a room class, the attributes of this class is room no, room type, room area and
AC machine. In this class the member functions are set data and display data.
Program:
class Room
{
private int roomNo = 0;
private String roomType = "";
private double roomArea = 0.0;
private boolean hasAC = false;
public void setData(int roomNo, String roomType, double roomArea, boolean hasAC) {
this.roomNo = roomNo;
this.roomType = roomType;
this.roomArea = roomArea;
this.hasAC = hasAC;
}
{
Room myRoom = new Room();
myRoom.setData(101, "Deluxe", 25.5, true);
myRoom.displayData();
}
}
Output:
D:\java>javac RoomTest.java
D:\java>java RoomTest
Room No: 101
Room Type: Deluxe
Room Area: 25.5 sq. meters
AC Machine: Yes
2. Write a program to create a class named shape. In this class we have three sub classes circle, triangle and
square each class has two member function named draw () and erase (). Create these using polymorphism
concepts.
Program:
class Shape
{
public void draw()
{
System.out.println("Drawing a shape");
}
Output:
C:\Users\hp>cd\
C:\>d:
D:\>cd java
D:\java>javac shapes.java
D:\java>
D:\java>java shapes
Drawing a circle
Erasing a circle
Drawing a triangle
Erasing a triangle
Drawing a square
Erasing a square
3. Write a program for example of multiple catch statements occurring in a program.
Program:
try
{
c = a / b;
}
catch (ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch (NullPointerException e)
{
System.out.println("Null Pointer Exception occurs");
}
catch (Exception e)
{
System.out.println("General Exception occurs");
}
System.out.println("divison by zero");
}
}
Output:
D:\java>javac Multi.java
D:\java>java Multi
Arithmetic Exception occurs
divison by zero
4. Write a program to illustrate usage of try/catch with finally clause
Program:
{
public static void main(String args[])
{
int a = 10, b = 0, c;
try
{
c = a / b;
}
catch (ArithmeticException e)
{
System.out.println("Divided by zero /0 ");
}
finally
{
System.out.println(" value of a is =" +a);
System.out.println(" value of b is =" +b);
}
}
Output:
D:\java>javac Trycatch.java
D:\java>java Trycatch
Divided by zero /0
value of a is =10
value of b is =0
5. Write a program for creation of user defined exception.
Program:
class InvalidAgeException extends Exception
{
public InvalidAgeException(String str)
{
super(str);
}
}
}
}
Output:
D:\java>javac UserDefinedException.java
D:\java>java UserDefinedException
Access granted
6. Write a program to get the input from the user and store it into file.
Using Reader and Writer file
Program:
import java.io.*;
public class SimpleWriteToFile
{
public static void main(String[] args)
{
String valueToWrite = "Hello, this is a simple test.";
String fileName = "output.txt";
try {
FileWriter writer = new FileWriter(fileName);
writer.write(valueToWrite);
writer.close();
System.out.println("Text has been written to the file successfully.");
}
catch (IOException e)
{
Output:
D:\java>javac SimpleWriteToFile.java
D:\java>java SimpleWriteToFile
Text has been written to the file successfully.
7. Develop a java application with Employee class with Emp_name, Emp_id, Address, Mail_id, Mobile_no as
members. Inherit the classes, Programmer, Assistant Professor, Associate Professor and Professor from
employee class. Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP as DA, 10 % of
BP as HRA, 12% of BP as PF, 0.1% of BP for staff club fund. Generate pay slips for the employees with their
gross and net salary
Program:
class Employee
{
String empName;
String empId;
String address;
String mailId;
String mobileNo;
double basicPay;
public Employee(String empName, String empId, String address, String mailId, String mobileNo, double
basicPay)
{
this.empName = empName;
this.empId = empId;
this.address = address;
this.mailId = mailId;
this.mobileNo = mobileNo;
this.basicPay = basicPay;
}
public double calculateDA()
{
return 0.97 * basicPay;
}
public double calculateHRA()
{
return 0.10 * basicPay;
}
public double calculatePF()
{
return 0.12 * basicPay;
}
public double calculateStaffClubFund()
{
return 0.001 * basicPay;
}
public double calculateGrossSalary()
{
return basicPay + calculateDA() + calculateHRA();
}
public double calculateNetSalary()
{
return calculateGrossSalary() - calculatePF() - calculateStaffClubFund();
}
public void displayPaySlip()
{
System.out.println("Pay Slip for " + empName);
System.out.println("Employee ID: " + empId);
System.out.println("Basic Pay: " + basicPay);
System.out.println("DA: " + calculateDA());
System.out.println("HRA: " + calculateHRA());
System.out.println("PF: " + calculatePF());
System.out.println("Staff Club Fund: " + calculateStaffClubFund());
System.out.println("Gross Salary: " + calculateGrossSalary());
System.out.println("Net Salary: " + calculateNetSalary());
System.out.println("-------------------------------");
}
}
Output:
D:\java>javac Empdetail.java
D:\java>java Empdetail
Pay Slip for John Doe
Employee ID: P001
Basic Pay: 50000.0
DA: 48500.0
HRA: 5000.0
PF: 6000.0
Staff Club Fund: 50.0
Gross Salary: 103500.0
Net Salary: 97450.0
-------------------------------
8. Write a program to Check Prime Number using Interface.
Program:
interface PrimeChecker
{
boolean isPrime(int number);
}
class PrimeNumber implements PrimeChecker
{
public boolean isPrime(int number)
{
if (number <= 1)
{
return false;
}cd\
{
PrimeChecker primeChecker = new PrimeNumber() ;
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers)
{
if (primeChecker.isPrime(num))
{
System.out.println(num + " is a prime number.");
}
else
{
System.out.println(num + " is not a prime number.");
}
}
}
}
Output:
D:\java>javac PrimeCheck.java
D:\java>java PrimeCheck
1 is not a prime number.
2 is a prime number.
3 is a prime number.
4 is not a prime number.
5 is a prime number.
D:\java>
9. Write a program to exhibit simple inheritance, multilevel inheritance and hybrid inheritance concepts.
Program:
class Vehicle
{
void start()
{
System.out.println("The vehicle starts.");
}
}
class Animal
{
void sound()
{
System.out.println("Animal makes sound.");
}
}
class Machine
{
void run()
{
System.out.println("The machine runs.");
}
}
System.out.println("Multilevel Inheritance:");
Puppy puppy = new Puppy();
puppy.sound();
puppy.bark();
puppy.cuteSound();
System.out.println();
System.out.println("Hybrid Inheritance:");
HumanoidRobot robot = new HumanoidRobot();
robot.run();
robot.speak();
robot.walk();
}
}
Output:
D:\java>javac InheritanceExample.java
D:\java>java InheritanceExample
Simple Inheritance:
The vehicle starts.
The car drives.
Multilevel Inheritance:
Animal makes sound.
Dog barks.
Puppy makes cute sound.
Hybrid Inheritance:
The machine runs.
The robot speaks.
Humanoid robot walks.
10 (a) Write a program to create a dialogbox and menu.
Program:
import javax.swing.*;
import java.awt.event.*;
menu.add(showDialogItem);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
Output:
D:\java>javac DialogBoxAndMenuExample.java
D:\java>java DialogBoxAndMenuExample
10(B) Write a program to create a grid layout control
PROGRAM:
import javax.swing.*;
import java.awt.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Output:
D:\>cd java
D:\java>javac GridLayoutExample.java
D:\java>java GridLayoutExample