oops batch2
oops batch2
Program:
Output:
Program:
Output:
Program:
class Employee {
String name;
int employeeId;
public Employee(String name, int employeeId) {
this.name = name;
this.employeeId = employeeId;
}
public void generatePaySlip() {
System.out.println("Employee Name: " + name);
System.out.println("Employee ID: " + employeeId);
System.out.println("Base Salary: Not Defined");
System.out.println("Bonus: Not Defined");
}
}
class FullTimeEmployee extends Employee {
double baseSalary;
double bonus;
public FullTimeEmployee(String name, int employeeId, double baseSalary, double bonus) {
super(name, employeeId); // Call parent constructor
this.baseSalary = baseSalary;
this.bonus = bonus;
}
public void generatePaySlip() {
super.generatePaySlip();
System.out.println("Base Salary: $" + baseSalary);
System.out.println("Bonus: $" + bonus);
double totalSalary = baseSalary + bonus;
System.out.println("Total Salary: $" + totalSalary);
}
}
Program:
Output:
Program:
interface Shape {
double calculateArea();
}
class Circle implements Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle implements Shape {
double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double calculateArea() {
return length * width;
}
}
class Triangle implements Shape {
double base, height;
super(message);
} else {
try {
validateAge(16);
} catch (AgeNotValidException e) {
try {
validateAge(25);
} catch (AgeNotValidException e) {
}
Output:
Exception caught: Age is less than 18, access denied.
Program:
class MyThread1 extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread 1: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class MyThread2 implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread 2: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MultiThreadExample {
public static void main(String[] args) {
MyThread1 thread1 = new MyThread1();
thread1.start();
MyThread2 myRunnable = new MyThread2();
Thread thread2 = new Thread(myRunnable);
thread2.start(); // Starting Thread 2
}
}
Output:
Thread 1: 1
Thread 2: 1
Thread 1: 2
Thread 2: 2
Thread 1: 3
Thread 2: 3
Thread 1: 4
Thread 2: 4
Thread 1: 5
Thread 2: 5
8. Write a java program for implementing generic class.
Program:
class Box<T> {
private T value;
this.value = value;
public T getValue() {
return value;
this.value = value;
Output:
Integer value in box: 10
maxSize = size;
front = 0;
rear = -1;
if (isFull()) {
} else {
queueArray[++rear] = value;
if (isEmpty()) {
return -1;
} else {
return value;
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
} else {
return queueArray[front];
if (isEmpty()) {
System.out.println("Queue is empty");
} else {
System.out.println();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);
queue.displayQueue();
queue.enqueue(60);
queue.dequeue();
queue.dequeue();
queue.displayQueue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.displayQueue();
Output:
Enqueued: 10
Enqueued: 20
Enqueued: 30
Enqueued: 40
Enqueued: 50
Queue elements: 10 20 30 40 50
Dequeued: 10
Dequeued: 20
Queue elements: 30 40 50
Dequeued: 30
Dequeued: 40
Dequeued: 50
Queue is empty
10. Write a program to Check Prime Number using Interface.
Program:
interface PrimeCheck {
@Override
if (number <= 1) {
return false;
if (number % i == 0) {
return false;
return true;
if (primeChecker.isPrime(num1)) {
} else {
}
if (primeChecker.isPrime(num2)) {
} else {
Output:
29 is a prime number.
Output:
Sum of Integers: 30.0
Program:
this.subjectName = subjectName;
this.subjectCode = subjectCode;
this.marks = marks;
this.credits = credits;
}
public double weightedMarks() {
return marks * credits;
}
}
public class GPACalculator {
public static void main(String[] args) {
double totalMarks = 0;
int totalCredits = 0;
Subject[] subjects = new Subject[5];
try {
subjects[0] = new Subject("Mathematics", "MA101", 85, 4);
subjects[1] = new Subject("Physics", "PH102", 90, 3);
subjects[2] = new Subject("Chemistry", "CH103", 75, 4);
subjects[3] = new Subject("Biology", "BI104", 80, 3);
subjects[4] = new Subject("Computer Science", "CS105", 88, 5);
for (Subject subject : subjects) {
totalMarks += subject.weightedMarks();
totalCredits += subject.credits;
}
double GPA = totalMarks / totalCredits;
System.out.println("Your GPA is: " + GPA);
} catch(InvalidSubjectNameException| InvalidSubjectCodeException |
InvalidMarksException | InvalidCreditsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Your GPA is: 83.68421052631578
Program: