Batch 2 Set 8 OOPS
Batch 2 Set 8 OOPS
Here are individual programs based on the questions from your file. Each program includes
its required imports. I'll provide one example at a time, including the output.
import java.util.Scanner;
System.out.println("Sorted array:");
for (int value : arr) {
System.out.print(value + " ");
}
1/31
}
}
Output:
yaml
Let me know if you'd like me to proceed with the next program or provide them all at once.
Here are more programs corresponding to the questions from your file:
java
import java.util.Scanner;
2/31
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
System.out.println("Sorted array:");
for (int value : arr) {
System.out.print(value + " ");
}
}
}
Output:
yaml
java
import java.util.Scanner;
3/31
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Sorted array:");
for (int value : arr) {
System.out.print(value + " ");
}
}
}
Output:
yaml
java
import java.util.Scanner;
4/31
class Stack {
private int[] stack;
private int top;
private int size;
5/31
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the stack:");
int size = sc.nextInt();
Stack stack = new Stack(size);
while (true) {
System.out.println("1. Push\n2. Pop\n3. Display\n4. Exit");
int choice = sc.nextInt();
switch (choice) {
case 1 -> {
System.out.println("Enter element to push:");
int element = sc.nextInt();
stack.push(element);
}
case 2 -> {
int popped = stack.pop();
if (popped != -1) {
System.out.println("Popped element: " + popped);
}
}
case 3 -> stack.display();
case 4 -> System.exit(0);
default -> System.out.println("Invalid choice!");
}
}
}
}
Output:
markdown
6/31
1. Push
2. Pop
3. Display
4. Exit
3
Stack elements: 10
1. Push
2. Pop
3. Display
4. Exit
2
Popped element: 10
1. Push
2. Pop
3. Display
4. Exit
4
5. Java Application with Employee Class and Pay Slip (Using Inheritance)
java
import java.util.Scanner;
class Employee {
int empId;
String name;
double basicPay;
7/31
double da = 0.05 * basicPay;
return basicPay + hra + da;
}
Output:
yaml
8/31
Basic Pay: 50000.0
Gross Pay: 57500.0
import java.util.Scanner;
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
double calculateArea() {
return length * width;
}
}
9/31
public class AreaCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose Shape (1: Circle, 2: Rectangle):");
int choice = sc.nextInt();
if (choice == 1) {
System.out.println("Enter radius of the circle:");
double radius = sc.nextDouble();
Shape circle = new Circle(radius);
System.out.println("Area of Circle: " + circle.calculateArea());
} else if (choice == 2) {
System.out.println("Enter length and width of the rectangle:");
double length = sc.nextDouble();
double width = sc.nextDouble();
Shape rectangle = new Rectangle(length, width);
System.out.println("Area of Rectangle: " + rectangle.calculateArea());
} else {
System.out.println("Invalid choice!");
}
}
}
Output:
mathematica
java
10/31
import java.util.Scanner;
interface Shape {
double calculateArea();
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
if (choice == 1) {
System.out.println("Enter radius of the circle:");
double radius = sc.nextDouble();
11/31
Shape circle = new Circle(radius);
System.out.println("Area of Circle: " + circle.calculateArea());
} else if (choice == 2) {
System.out.println("Enter length and width of the rectangle:");
double length = sc.nextDouble();
double width = sc.nextDouble();
Shape rectangle = new Rectangle(length, width);
System.out.println("Area of Rectangle: " + rectangle.calculateArea());
} else {
System.out.println("Invalid choice!");
}
}
}
Output:
mathematica
import java.util.Scanner;
12/31
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive number:");
int num = sc.nextInt();
try {
if (num < 0) {
throw new InvalidInputException("Number is negative!");
} else {
System.out.println("You entered: " + num);
}
} catch (InvalidInputException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Output:
mathematica
9. Multithreaded Application
java
import java.util.Random;
13/31
new EvenNumberHandler(num).start();
} else {
new OddNumberHandler(num).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
14/31
Output:
mathematica
Generated Number: 7
Odd Number Cube: 343
Generated Number: 10
Even Number Square: 100
Generated Number: 3
Odd Number Cube: 27
import java.io.File;
import java.util.Scanner;
if (file.exists()) {
System.out.println("File exists.");
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: " + file.canWrite());
System.out.println("File Type: " + (file.isFile() ? "File" :
"Directory"));
System.out.println("File Length: " + file.length() + " bytes");
} else {
System.out.println("File does not exist.");
}
}
}
Output:
15/31
yaml
class GenericClass<T> {
private T value;
public T getValue() {
return value;
}
16/31
}
}
Output:
makefile
Value: 10
Value: Hello
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Calculator() {
setTitle("Calculator");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] buttons = {
"7", "8", "9", "/", "4", "5", "6", "*",
"1", "2", "3", "-", "0", ".", "=", "+"
};
17/31
JButton button = new JButton(text);
button.setBounds(x, y, 70, 50);
add(button);
button.addActionListener(this);
x += 80;
if (x > 300) {
x = 30;
y += 70;
}
}
setLayout(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9' ||
command.equals(".")) {
textField.setText(textField.getText() + command);
} else if (command.equals("=")) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+' -> result = num1 + num2;
case '-' -> result = num1 - num2;
case '*' -> result = num1 * num2;
case '/' -> result = num1 / num2;
}
textField.setText(String.valueOf(result));
} else {
operator = command.charAt(0);
num1 = Double.parseDouble(textField.getText());
textField.setText("");
}
}
18/31
Output: A fully functional GUI calculator that supports basic arithmetic operations.
java
import java.util.Scanner;
class Queue {
private int[] queue;
private int front, rear, size;
19/31
System.out.println("Queue Underflow");
return -1;
} else {
return queue[front++];
}
}
while (true) {
System.out.println("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit");
int choice = sc.nextInt();
switch (choice) {
case 1 -> {
System.out.println("Enter element to enqueue:");
int element = sc.nextInt();
queue.enqueue(element);
}
case 2 -> {
int dequeued = queue.dequeue();
if (dequeued != -1) {
System.out.println("Dequeued element: " + dequeued);
}
20/31
}
case 3 -> queue.display();
case 4 -> System.exit(0);
default -> System.out.println("Invalid choice!");
}
}
}
}
Output:
markdown
21/31
14. Abstract Class for Addition (Sum of Two and Three Numbers)
java
@Override
void calculate() {
System.out.println("Sum of Two Numbers: " + (a + b));
}
}
@Override
void calculate() {
System.out.println("Sum of Three Numbers: " + (a + b + c));
}
}
22/31
Sum sumThree = new SumOfThree(10, 20, 30);
sumThree.calculate();
}
}
Output:
mathematica
java
import java.util.Scanner;
interface PrimeChecker {
boolean isPrime(int number);
}
23/31
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if it's prime:");
int number = sc.nextInt();
Output:
mathematica
java
24/31
public double getSum() {
return num1.doubleValue() + num2.doubleValue();
}
}
Output:
mathematica
java
import java.util.Random;
25/31
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
@Override
public void run() {
if (number % 2 == 0) {
System.out.println("Even Number: " + number + ", Square: " + (number *
number));
}
}
}
@Override
public void run() {
if (number % 2 != 0) {
System.out.println("Odd Number: " + number + ", Cube: " + (number *
number * number));
}
}
}
26/31
public class MultiThreadRandom {
public static void main(String[] args) {
new RandomNumberGenerator().start();
}
}
Output:
mathematica
Generated Number: 5
Odd Number: 5, Cube: 125
Generated Number: 8
Even Number: 8, Square: 64
Generated Number: 3
Odd Number: 3, Cube: 27
java
import java.util.Scanner;
27/31
for (int i = 1; i <= 5; i++) {
System.out.println("Enter marks for subject " + i + ":");
double marks = sc.nextDouble();
System.out.println("Enter credits for subject " + i + ":");
double credits = sc.nextDouble();
try {
if (marks < 0 || marks > 100) {
throw new InvalidMarksException("Invalid Marks! Should be
between 0 and 100.");
}
if (credits <= 0) {
throw new InvalidCreditsException("Invalid Credits! Should be
greater than zero.");
}
totalMarks += marks * credits;
totalCredits += credits;
} catch (InvalidMarksException | InvalidCreditsException e) {
System.out.println(e.getMessage());
i--;
}
}
Output:
yaml
28/31
Invalid Marks! Should be between 0 and 100.
Enter marks for subject 3:
88
Enter credits for subject 3:
3
Enter marks for subject 4:
75
Enter credits for subject 4:
4
Enter marks for subject 5:
80
Enter credits for subject 5:
3
Your GPA is: 83.83
java
Output:
29/31
javascript
java
import java.util.Scanner;
switch (monthNumber) {
case 1 -> System.out.println("January");
case 2 -> System.out.println("February");
case 3 -> System.out.println("March");
case 4 -> System.out.println("April");
case 5 -> System.out.println("May");
case 6 -> System.out.println("June");
case 7 -> System.out.println("July");
case 8 -> System.out.println("August");
case 9 -> System.out.println("September");
case 10 -> System.out.println("October");
case 11 -> System.out.println("November");
case 12 -> System.out.println("December");
default -> System.out.println("Invalid month number!");
}
}
}
Output:
css
30/31
Enter a number between 1 and 12:
4
April
These programs cover the entire set of questions provided in your file. Let me know if you
need further clarification or any more assistance!
31/31