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

EXPERIMENT 2 (JAVA)

The document contains Java programs demonstrating the use of classes and objects through examples of a Car class, a Bank class, a Stack data structure, and a Queue data structure. Each section includes code for creating objects, handling user input, and performing operations like deposit, withdrawal, push, pop, enqueue, and dequeue. The document provides sample outputs for user interactions with each program.

Uploaded by

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

EXPERIMENT 2 (JAVA)

The document contains Java programs demonstrating the use of classes and objects through examples of a Car class, a Bank class, a Stack data structure, and a Queue data structure. Each section includes code for creating objects, handling user input, and performing operations like deposit, withdrawal, push, pop, enqueue, and dequeue. The document provides sample outputs for user interactions with each program.

Uploaded by

ARCHANA M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT: 3

Java Programs using class & Objects


1.CAR
import java.util.Scanner;
class Car {
private String modelNumber;
private String brand;
private String color;
private double price;
public Car() {
this.modelNumber = "Unknown";
this.brand = "Unknown";
this.color = "Unknown";
this.price = 0.0;
}
public Car(String modelNumber, double price) {
this.modelNumber = modelNumber;
this.brand = "Unknown";
this.color = "Unknown";
this.price = price;
}
public Car(String modelNumber, String brand, String color, double price) {
this.modelNumber = modelNumber;
this.brand = brand;
this.color = color;
this.price = price;
}
public void getCarDetails() {
System.out.println("Car Model Number: " + modelNumber);
System.out.println("Car Brand: " + brand);
System.out.println("Car Color: " + color);
System.out.println("Car Price: $" + price);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of cars to add: ");
int numCars = scanner.nextInt();
scanner.nextLine();
Car[] cars = new Car[numCars];
for (int i = 0; i < numCars; i++) {
System.out.println("\nEnter details for Car #" + (i + 1) + ":");
System.out.print("Enter Model Number: ");
String modelNumber = scanner.nextLine();
System.out.print("Enter Brand: ");
String brand = scanner.nextLine();
System.out.print("Enter Color: ");
String color = scanner.nextLine();
System.out.print("Enter Price: $");
double price = scanner.nextDouble();
scanner.nextLine();
cars[i] = new Car(modelNumber, brand, color, price);
}
System.out.println("\nCar Details:");
for (int i = 0; i < numCars; i++) {
System.out.println("\nCar #" + (i + 1) + " Details:");
cars[i].getCarDetails();
}
scanner.close();
}
}
Output:
Enter the number of cars to add: 2
Enter details for Car #1:
Enter Model Number: Model28
Enter Brand: Toyota
Enter Color: Pink
Enter Price: $13000

Enter details for Car #2:


Enter Model Number: Model13
Enter Brand: Honda
Enter Color: Black
Enter Price: $28000

Car Details:
Car #1 Details:
Car Model Number: Model28
Car Brand: Toyota
Car Color: Pink
Car Price: $13000
Car #2 Details:
Car Model Number: Model13
Car Brand: Honda
Car Color: Black
Car Price: $28000

2.BANK ACCOUNT:
import java.util.Scanner;
class Bank {
private String ownerName;
private long accountNumber;
private double balance;
public Bank(String ownerName, long accountNumber, double balance) {
this.ownerName = ownerName;
this.accountNumber = accountNumber;
this.balance = balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit of $" + amount + " successful.");
} else {
System.out.println("Invalid deposit amount. Please enter a positive amount.");
}
}
public void withdraw(double amount) {
if (amount > 0) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawal of $" + amount + " successful.");
} else {
System.out.println("Insufficient balance. Cannot withdraw $" + amount);
}
} else {
System.out.println("Invalid withdrawal amount. Please enter a positive amount.");
}
}
public void displayDetails() {
System.out.println("Owner Name: " + ownerName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: $" + balance);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter owner name: ");
String ownerName = scanner.nextLine();

System.out.print("Enter account number: ");


long accountNumber = scanner.nextLong();

System.out.print("Enter initial balance: ");


double initialBalance = scanner.nextDouble();

Bank myAccount = new Bank(ownerName, accountNumber, initialBalance);

System.out.println("Initial Account Details:");


myAccount.displayDetails();

System.out.print("Enter deposit amount: $");


double depositAmount = scanner.nextDouble();
myAccount.deposit(depositAmount);

System.out.print("Enter withdrawal amount: $");


double withdrawalAmount = scanner.nextDouble();
myAccount.withdraw(withdrawalAmount);

// Display final details


System.out.println("Updated Account Details:");
myAccount.displayDetails();

scanner.close();
}
}

Output:
Enter owner name: Abi
Enter account number: 2813
Enter initial balance: 1000.0
Initial Account Details:
Owner Name: Abi
Account Number: 2813
Balance: $1000.0

Enter deposit amount: $500.0


Deposit of $500.0 successful.
Enter withdrawal amount: $300.0
Withdrawal of $300.0 successful.
Updated Account Details:
Owner Name: Abi
Account Number: 123456789
Balance: $1200.0

3. Stack and queue data structures


import java.util.Scanner;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Stack {
private Node top;
public Stack() {
top = null;
}
public void push(int data) {
Node newNode = new Node(data);
if (top == null) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
System.out.println("Pushed " + data);
}
public void pop() {
if (top == null) {
System.out.println("Stack underflow");
} else {
int poppedValue = top.data;
top = top.next;
System.out.println("Popped Element=" + poppedValue);
}
}
public void display() {
Node current = top;
System.out.println("The stack elements are:");
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Stack stack = new Stack();
int choice;
do {
System.out.println("Stack Operations:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter the choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the value to insert: ");
int valueToPush = scanner.nextInt();
stack.push(valueToPush);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
} while (choice != 4);
scanner.close();
}
}
Output:
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Enter the choice: 1
Enter the value to insert: 5
Pushed 5
Enter the choice: 1
Enter the value to insert: 10
Pushed 10
Enter the choice: 1
Enter the value to insert: 20
Pushed 20
Enter the choice: 3
The stack elements are:
20
10
5
Enter the choice: 2
Popped Element=20
Enter the choice: 3
The stack elements are:
10
5
Enter the choice: 4
Exiting...

4. Queue Operation
import java.util.Scanner
class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

class Queue {
private Node front;
private Node rear;
public Queue() {
front = null;
rear = null;
}

public void enqueue(int data) {


Node newNode = new Node(data);
if (front == null) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
System.out.println("Inserting " + data);
}

public void dequeue() {


if (front == null) {
System.out.println("Queue underflow");
} else {
int dequeuedValue = front.data;
front = front.next;
System.out.println("Dequeued Element=" + dequeuedValue);
}
}

public void display() {


Node current = front;
System.out.println("The Queue elements are:");
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
}
public class QueueclassTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Queue queue = new Queue();
int choice;
do {
System.out.println("Queue Operations:");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter the choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the value to insert: ");
int valueToEnqueue = scanner.nextInt();
queue.enqueue(valueToEnqueue);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
} while (choice != 4);
scanner.close();
}
}
Output:
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter the choice: 1
Enter the value to insert: 10
Inserting 10
Enter the choice: 1
Enter the value to insert: 20
Inserting 20
Enter the choice: 1
Enter the value to insert: 30
Inserting 30
Enter the choice: 3
The Queue elements are:
10
20
30
Enter the choice: 2
Dequeued Element=10
Enter the choice: 3
The Queue elements are:
20
30
Enter the choice: 4
Exiting...

You might also like