0% found this document useful (0 votes)
3 views

oops batch 1

Uploaded by

ece apce
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

oops batch 1

Uploaded by

ece apce
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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;
}

public void displayData()


{
System.out.println("Room No: " + roomNo);
System.out.println("Room Type: " + roomType);
System.out.println("Room Area: " + roomArea + " sq. meters");
System.out.println("AC Machine: " + (hasAC ? "Yes" : "No"));
}
}

public class RoomTest


{
public static void main(String[] args)

{
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");
}

public void erase()


{
System.out.println("Erasing a shape");
}
}

class Circle extends Shape


{
public void draw()
{
System.out.println("Drawinsg a circle");
}
public void erase()
{
System.out.println("Erasing a circle");
}
}

class Triangle extends Shape


{
public void draw()
{
System.out.println("Drawing a triangle");
}
public void erase()
{
System.out.println("Erasing a triangle");
}
}

class Square extends Shape


{

public void draw()


{
System.out.println("Drawing a square");
}
public void erase()
{
System.out.println("Erasing a square");
}
}
public class shapes
{
public static void main(String[] args)
{
Shape circle = new Circle();
Shape triangle = new Triangle();
Shape square = new Square();

Shape[] shapes = {circle, triangle, square};

for (Shape shape : shapes)


{
shape.draw();
shape.erase();
}
}
}

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:

public class Multi


{
public static void main(String args[])
{
int a = 10, b = 0, c;

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 class Trycatch

{
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);
}
}

public class UserDefinedException


{
public static void checkAge(int age) throws InvalidAgeException
{
if (age < 18)
{
throw new InvalidAgeException("Age must be 18 or older.");
}
else
{
System.out.println("Access granted");
}
}

public static void main(String[] args)


{
try
{
checkAge(23);
}
catch (InvalidAgeException e)
{
System.out.println("Exception: " + e.getMessage());
}

}
}
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)
{

System.out.println("An error occurred: " + e.getMessage());


}
}
}

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("-------------------------------");
}
}

public class Empdetail


{
public static void main(String[] args)
{
Employee programmer = new Employee("John Doe", "P001", "123 Street, City", "[email protected]",
"1234567890", 50000);
programmer.displayPaySlip();
}
}

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\

for (int i = 2; i <= Math.sqrt(number); i++)


{
if (number % i == 0)
{
return false;
}
}
return true;
}
}
public class PrimeCheck
{
public static void main(String[] args)

{
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 Car extends Vehicle


{
void drive()
{
System.out.println("The car drives.");
}
}

class Animal
{
void sound()
{
System.out.println("Animal makes sound.");
}
}

class Dog extends Animal


{
void bark()
{
System.out.println("Dog barks.");
}
}

class Puppy extends Dog


{
void cuteSound()
{
System.out.println("Puppy makes cute sound.");
}
}

class Machine
{
void run()
{
System.out.println("The machine runs.");
}
}

class Robot extends Machine


{
void speak()
{
System.out.println("The robot speaks.");
}
}

class HumanoidRobot extends Robot


{
void walk()
{
System.out.println("Humanoid robot walks.");
}
}

public class InheritanceExample


{
public static void main(String[] args)
{
System.out.println("Simple Inheritance:");
Car car = new Car();
car.start();
car.drive();
System.out.println();

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.*;

public class DialogBoxAndMenuExample


{

public static void main(String[] args)


{

JFrame frame = new JFrame("Dialog Box and Menu Example");


frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Options");
JMenuItem showDialogItem = new JMenuItem("Show Dialog");
showDialogItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "This is a dialog box!", "Dialog",
JOptionPane.INFORMATION_MESSAGE);
}
});

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.*;

public class GridLayoutExample


{
public static void main(String[] args)
{

JFrame frame = new JFrame("GridLayout Example");


frame.setLayout(new GridLayout(3, 3));
for (int i = 1; i <= 9; i++)
{
JButton button = new JButton("Button " + i);
frame.add(button);
}

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

You might also like