Batch 1 Set 2 OOPS
Batch 1 Set 2 OOPS
Here is the implementation of the first five questions from the provided document. Each
program imports the necessary packages and files independently, and the output is shown
below each program.
// File: MyPackage/MyClass.java
package MyPackage;
public class MyClass {
public void displayMessage() {
System.out.println("This is a message from MyPackage.");
}
}
// File: Main.java
import MyPackage.MyClass;
Output:
csharp
1/26
Question 2: Binary Search Tree Operations
java
class Node {
int key;
Node left, right;
class BinarySearchTree {
Node root;
BinarySearchTree() {
root = null;
}
return root;
}
void inorder() {
inorderRec(root);
}
2/26
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
tree.inorder();
}
}
Output:
20 30 40 50 60 70 80
java
import java.util.ArrayList;
public SequentialSearchTree() {
list = new ArrayList<>();
3/26
}
System.out.println(tree.search(10));
tree.display();
}
}
Output:
csharp
true
[10, 30]
4/26
java
import java.util.Arrays;
Output:
5/26
c
Bubble Sorted array: [11, 12, 22, 25, 34, 64, 90]
Selection Sorted array: [11, 12, 22, 25, 34, 64, 90]
java
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
class Stack {
Node top;
void pop() {
if (top != null) {
top = top.next;
}
}
void display() {
Node temp = top;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
6/26
}
System.out.println();
}
}
class Queue {
Node front, rear;
void dequeue() {
if (front != null) {
front = front.next;
if (front == null) {
rear = null;
}
}
}
void display() {
Node temp = front;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}
7/26
stack.push(20);
stack.display();
stack.pop();
stack.display();
Output:
20 10
10
30 40
40
Here are the implementations for the next set of questions. Each program imports the
necessary packages and files independently, and the output is shown below each program.
class Account {
protected double balance;
8/26
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
Output:
yaml
Balance: 1000.0
Deposited: 500.0
Withdrawn: 300.0
9/26
Insufficient funds!
Balance: 1200.0
10/26
private final int number;
Output:
makefile
Generated: 45
Cube: 91125
Generated: 28
Square: 784
Generated: 63
Cube: 250047
Generated: 16
Square: 256
Generated: 89
Cube: 704969
java
import java.io.*;
11/26
public static void main(String[] args) {
try {
// Create a new file
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
Output:
vbnet
12/26
Writable: true
Readable: true
Written to file.
Hello, this is a file operations example.
class GenericClass<T> {
private T value;
genericMethod(456);
genericMethod("Hello, Generics!");
}
}
Output:
13/26
mathematica
Value: 123
Value: Generics in Java
Generic Method: 456
Generic Method: Hello, Generics!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
stage.setTitle("JavaFX Example");
stage.setScene(scene);
stage.show();
}
Output:
14/26
css
Here are the implementations for the next set of questions. Each program includes
independent imports, and the output is shown below each program.
java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
15/26
}
Output:
sql
A JavaFX window opens with two radio buttons and two checkboxes.
Users can select one radio button and toggle the checkboxes.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
16/26
stage.setTitle("Text and Password Fields");
stage.setScene(scene);
stage.show();
}
Output:
css
A JavaFX window opens with a label, a text field, and a password field.
The text field shows the entered text, while the password field masks it.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
17/26
MenuBar menuBar = new MenuBar(fileMenu);
Output:
css
A JavaFX window opens with a menu bar containing a "File" menu and a clickable
hyperlink.
Clicking the hyperlink prints "Hyperlink clicked" in the console.
java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
18/26
Button top = new Button("Top");
Button bottom = new Button("Bottom");
Button left = new Button("Left");
Button right = new Button("Right");
Button center = new Button("Center");
borderPane.setTop(top);
borderPane.setBottom(bottom);
borderPane.setLeft(left);
borderPane.setRight(right);
borderPane.setCenter(center);
borderPane.setBottom(hBox);
Output:
css
19/26
java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
Output:
less
20/26
2. A StackPane layout with a single button stacked at the center.
Here are the implementations for the next set of questions, continuing from where we left
off.
java
class ElectricityBill {
private int consumerNo;
private String consumerName;
private int previousReading;
private int currentReading;
21/26
bill = (100 * 4.50) + (300 * 6.50) + (100 * 8.00) + (300 * 9.00) +
(units - 800) * 11.00;
}
return bill;
}
Output:
yaml
java
22/26
}
}
Output:
python
java
23/26
cube.printVolume(150.0);
}
}
Output:
mathematica
java
import java.util.Scanner;
class ReservationSystem {
private String[] seats;
24/26
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ReservationSystem system = new ReservationSystem(5);
system.displaySeats();
system.bookSeat(2, "John");
system.bookSeat(2, "Alice");
system.displaySeats();
}
}
Output:
yaml
Seat Reservations:
Seat 0: Available
Seat 1: Available
...
Seat 4: Available
Seat 2 booked for John
Seat 2 is already booked.
Seat Reservations:
Seat 0: Available
Seat 1: Available
Seat 2: John
...
Seat 4: Available
java
class Dog {
String color;
String size;
int age;
25/26
public Dog(String color, String size, int age) {
this.color = color;
this.size = size;
this.age = age;
}
Output:
csharp
26/26