Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
case '/':
Practical_1:
if (num2 != 0) {
import [Link];
result = num1 / num2;
public class Calculator {
public static void main(String[] args) { [Link]("Result: " +
result);
Scanner sc = new Scanner([Link]);
} else {
[Link]("===== Simple Java
Calculator ====="); [Link]("Error: Division
by zero is not allowed.");
[Link]("Enter first number: ");
}
double num1 = [Link]();
break;
[Link]("Enter operator (+, -, *,
/): "); default:
char operator = [Link]().charAt(0); [Link]("Invalid
operator!");
[Link]("Enter second number:
"); }
double num2 = [Link](); [Link]();
double result; }
switch (operator) { }
case '+':
result = num1 + num2;
[Link]("Result: " +
result);
break;
case '-':
result = num1 - num2;
[Link]("Result: " +
result);
break; case ':
result = num1 * num2;
[Link]("Result: " +
result);
break;
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
[Link](width);
Practical_2
import [Link]; class
double area = [Link]();
Rectangle {
[Link]("Length: " +
private double length; [Link]());
private double width; [Link]("Width: " +
public void setLength(double length) { [Link] [Link]());
= length; [Link]("Area of Rectangle: " +
area);
}
[Link]();
public void setWidth(double width) { [Link] =
}
width;
}
}
public double getLength() { return length;
public double getWidth() { return width;
public double calculateArea() { return length
* width;
public class RectangleArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Rectangle rect = new Rectangle();
[Link]("Enter Length: "); double
length = [Link]();
[Link]("Enter Width: "); double
width = [Link]();
[Link](length);
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Scanner sc = new Scanner([Link]);
Practical_3
[Link]("Enter real part of first
import [Link]; complex number: ");
class Complex { double r1 = [Link]();
double real, imag; Complex() [Link]("Enter imaginary part of
{ first complex number: ");
real = 0; double i1 = [Link]();
imag = 0; [Link]("Enter real part of
second complex number: ");
}
double r2 = [Link]();
Complex(double r, double i) { real = r;
[Link]("Enter imaginary part of
imag = i; second complex number: ");
} double i2 = [Link](); Complex c1
Complex add(Complex c) { = new Complex(r1, i1); Complex c2 = new
return new Complex(real + [Link], imag + Complex(r2, i2); Complex add =
[Link]);
[Link](c2);
}
Complex sub = [Link](c2);
Complex subtract(Complex c) {
Complex mul = [Link](c2);
return new Complex(real - [Link], imag -
[Link]); [Link]("Addition: ");
} [Link]();
Complex multiply(Complex c) { [Link]("Subtraction: "); [Link]();
double r = real * [Link] - imag * [Link]; [Link]("Multiplication: ");
double i = real * [Link] + imag * [Link]; [Link]();
return new Complex(r, i); [Link]();
} }}
void display() {
[Link](real + " + " + imag + "i");
}}
public class ComplexNumber {
public static void main(String[] args) {
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
[Link]("Book returned
Practical_4 successfully.");
import [Link]; } else {
import [Link]; [Link]("Book was not
class Book { issued.");
}
private String title;
}
private String author;
private boolean isIssued; public void displayBook() {
static int totalBooks = 0; [Link]("Title: " + title);
Book(String title, String author) { [Link]("Author: " + author);
[Link] = title; [Link]("Status: " + (isIssued ?
"Issued" : "Available"));
[Link] = author; [Link] =
[Link](" --------------------------- ");
false; totalBooks++;
}
}
public static void showTotalBooks() {
public String getTitle() {
[Link]("Total Books in Library:
return title; " + totalBooks);
} }
public void issueBook() { if }
(!isIssued) { public class LibraryManagement {
isIssued = true; public static void main(String[] args) {
[Link]("Book issued Scanner sc = new Scanner([Link]);
successfully.");
} else { ArrayList<Book> library = new
ArrayList<>();
[Link]("Book already
issued."); int choice; do {
} [Link]("\n===== Library
Management System =====");
}
[Link]("1. Add Book");
public void returnBook() {
[Link]("2. View Books");
if (isIssued) {
[Link]("3. Issue Book");
isIssued = false;
[Link]("4. Return Book");
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
[Link]("5. Show Total }
Books");
}
[Link]("6. Exit");
break;
[Link]("Enter choice: "); choice
case 4:
= [Link]();
[Link]("Enter book title to
[Link](); return: ");
switch (choice) { case 1: String returnTitle = [Link](); for (Book b :
library) {
[Link]("Enter Book Title: if
"); ([Link]().equalsIgnoreCase(returnTitle)) {
String title = [Link](); [Link]();
[Link]("Enter Author }
Name: ");
}
String author = [Link]();
break;
[Link](new Book(title,
case 5:
author));
[Link]();
[Link]("Book added
successfully."); break; case 6:
break; case 2: [Link]("Exiting
system...");
for (Book b : library) { [Link]();
break; default:
}
[Link]("Invalid
break; case 3:
choice!");
[Link]("Enter book title
}
to issue: ");
} while (choice != 6);
String issueTitle = [Link](); for (Book b :
[Link]();
library) {
}
if
([Link]().equalsIgnoreCase(issueTitle)) { }
[Link]();
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
double average = (double) sum / n;
Practical_5
[Link]("\nMaximum Element:
import [Link]; " + max);
public class ArrayOperations { [Link]("Minimum Element: "
public static void main(String[] args) { + min);
Scanner sc = new Scanner([Link]); [Link]("Sum of Elements: " +
sum);
[Link]("Enter number of
[Link]("Average of Elements:
elements: ");
" + average);
int n = [Link](); int
[Link]("Enter element to
arr[] = new int[n]; search: ");
[Link]("Enter array int key = [Link]();
elements:");
boolean found = false; for
for (int i = 0; i < n; i++) { arr[i] =
(int i = 0; i < n; i++) { if (arr[i]
[Link]();
== key) {
}
found = true; break;
[Link]("Array Elements:"); for
}
(int i = 0; i < n; i++) {
}
[Link](arr[i] + " ");
if (found) {
}
[Link]("Element found in
int max = arr[0]; int array.");
min = arr[0]; int sum } else {
= 0; [Link]("Element not found in
for (int i = 0; i < n; i++) { if array.");
}
(arr[i] > max) {
[Link]();
max = arr[i];
}
}
}
if (arr[i] < min) { min = arr[i];
sum = sum + arr[i];
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_6 class Book { String class PrintedBook extends Book { int
title; String
pages;
author; String
String coverType;
isbn;
PrintedBook(String title, String author, String
Book(String title, String author, String isbn) { isbn, int pages, String coverType) {
[Link] = title; super(title, author, isbn); [Link]
[Link] = author; [Link] = = pages;
isbn; [Link] = coverType;
} }
void displayDetails() { void displayDetails() {
[Link]("Title: " + title); [Link]();
[Link]("Author: " + author); [Link]("Number of Pages: " +
[Link]("ISBN: " + isbn); pages);
} [Link]("Cover Type: " +
coverType);
}
}
class EBook extends Book { double
}
fileSize;
public class LibrarySystem {
String format;
public static void main(String[] args) { EBook
EBook(String title, String author, String isbn,
double fileSize, String format) { ebook = new EBook("Java
Programming", "James Gosling", "12345", 5.2,
super(title, author, isbn); "PDF");
[Link] = fileSize; [Link] PrintedBook pbook = new PrintedBook("Data
Structures", "Mark Allen", "67890", 350,
= format;
"Hardcover");
}
[Link]("EBook Details:");
void displayDetails() {
[Link]();
[Link]();
[Link]("\nPrinted Book Details:"); [Link]();
[Link]("File Size: " + fileSize + "
MB"); }
[Link]("Format: " + format); }
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_7 static void main(String[] args) {
import [Link];
class Student {
String name;
int rollNo; int
marks; char
grade;
void calculateGrade() { if
(marks >= 90)
grade = 'A';
else if (marks >= 75) grade
= 'B';
else if (marks >= 60) grade
= 'C';
else if (marks >= 50) grade
= 'D';
else
grade = 'F';
void displayDetails() {
[Link]("Student Name: " +
name);
[Link]("Roll Number: " +
rollNo);
[Link]("Marks: " + marks);
[Link]("Grade: " + grade);
public class StudentManagement { public
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Scanner sc = new
Scanner([Link]);
Student s = new
Student();
");
[Link] = [Link]();
[Link]("Enter
Student Name:
[Link]("Enter
Roll Number: "); [Link] =
[Link]();
[Link]("Enter
Marks: "); [Link] =
[Link]();
[Link]();
[Link]("\nStud
ent Details");
[Link]();
[Link]();
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
public void withdraw(double amount) { if
Practical_8
(amount <= balance) {
import [Link];
balance = balance - amount;
class BankAccount {
private int accountNumber; [Link]("Amount
Withdrawn: " + amount);
private String holderName;
} else {
private double balance;
[Link]("Insufficient
public void setAccountNumber(int
Balance.");
accountNumber) {
}
[Link] = accountNumber;
}
}
}
public int getAccountNumber() { return
public class BankSystem {
accountNumber;
public static void main(String[] args) {
}
Scanner sc = new Scanner([Link]);
public void setHolderName(String
holderName) { BankAccount account = new
BankAccount();
[Link] = holderName;
[Link]("Enter Account Number:
}
");
public String getHolderName() {
[Link]([Link]()); [Link]();
return holderName;
[Link]("Enter Account Holder
}
public void setBalance(double balance) { Name: ");
[Link] = balance; [Link]([Link]());
} [Link]("Enter Initial Balance: ");
public double getBalance() { [Link]([Link]());
return balance; [Link]("Enter Deposit Amount:
");
}
double depositAmount = [Link]();
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
public void deposit(double amount) {
balance = balance + amount;
[Link]("Amount Deposited: "
+ amount);
[Link](depositAmount);
[Link]("Enter Withdrawal
Amount: ");
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_9:
class Person {
String name;
int age;
void displayPerson() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
class Student extends Person {
int rollNo;
void displayStudent() {
[Link]("Roll Number: " + rollNo);
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Student s = new Student();
[Link] = "Rahul";
[Link] = 20;
[Link] = 101;
[Link]();
[Link]();
}
}
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_10
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}}
class ScientificCalculator extends Calculator {
@Override
int add(int a, int b) {
double logA = [Link](a);
double logB = [Link](b);
double result = logA + logB;
return (int) result;
}
}
public class CalculatorTest {
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Addition of two integers: " + [Link](5, 3));
[Link]("Addition of two doubles: " + [Link](5.5, 2.5));
[Link]("Addition of three numbers: " + [Link](2, 3, 4));
ScientificCalculator sc = new ScientificCalculator();
[Link]("Logarithmic addition: " + [Link](10, 20));
}
}
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_11
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
class InvalidAmountException extends Exception {
public InvalidAmountException(String message) {
super(message);
}
}
class BankTransaction {
double balance = 0;
void deposit(double amount) throws
InvalidAmountException {
if (amount < 0) {
throw new InvalidAmountException("Invalid
amount! Deposit cannot be negative.");
}
balance += amount;
[Link]("Deposited: " + amount);
}
void withdraw(double amount) throws
InvalidAmountException, InsufficientFundsException {
if (amount < 0) {
throw new InvalidAmountException("Invalid
amount! Withdrawal cannot be negative.");
}
if (amount > balance) {
throw new
InsufficientFundsException("Insufficient balance.");
}
balance -= amount;
[Link]("Withdrawn: " + amount);
}
void displayBalance() {
[Link]("Current Balance: " +
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
balance);
}
}
public class BankTransactionDemo {
public static void main(String[] args) {
BankTransaction bt = new BankTransaction();
try {
[Link](5000);
[Link](2000);
[Link](4000);
} catch (InvalidAmountException |
InsufficientFundsException e) {
[Link]("Exception: " +
[Link]());
}
[Link]();
}
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_12
import [Link];
import [Link];
class Product {
int id;
String name;
double price;
Product(int id, String name, double price) {
[Link] = id;
[Link] = name;
[Link] = price;
}
void display() {
[Link](id + " - " + name + " - Rs." + price);
}
}
public class OnlineShopping {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ArrayList<Product> products = new ArrayList<>();
ArrayList<Product> cart = new ArrayList<>();
[Link](new Product(1, "Laptop", 50000));
[Link](new Product(2, "Mobile", 20000));
[Link](new Product(3, "Headphones", 2000));
[Link](new Product(4, "Keyboard", 1500));
int choice;
do {
[Link]("\n===== Online Shopping System =====");
[Link]("1. View Products");
[Link]("2. Add to Cart");
[Link]("3. View Cart");
[Link]("4. Checkout");
[Link]("5. Exit");
[Link]("Enter choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Available Products: ");
for (Product p : products) {
[Link]();
}
break;
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
case 2:
[Link]("Enter product ID to add: ");
int id = [Link]();
boolean found = false;
for (Product p : products) {
if ([Link] == id) {
[Link](p);
[Link]("Product added to cart.");
found = true;
break;
}
}
if (!found) {
[Link]("Product not found!");
}
break;
case 3:
[Link]("Cart Items: ");
if ([Link]()) {
[Link]("Cart is empty.");
} else {
for (Product p : cart) {
[Link]();
}
}
break;
case 4:
double total = 0;
if ([Link]()) {
[Link]("Cart is empty. Nothing to checkout.");
} else {
for (Product p : cart) {
total += [Link];
}
[Link]("Total Bill: Rs." + total);
}
break;
case 5:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice!");
}
} while (choice != 5);
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
[Link]();
}
}
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
Practical_13
import [Link];
import [Link];
class Doctor {
String name;
Doctor(String name) {
[Link] = name;
}
double getConsultationFee() {
return 500;
}
}
class SpecialistDoctor extends Doctor {
SpecialistDoctor(String name) {
super(name);
}
double getConsultationFee() {
return 1000;
}
}
class Patient {
private String name;
private int age;
private String disease;
Patient(String name, int age, String disease) {
[Link] = name;
[Link] = age;
[Link] = disease;
}
public String getDetails() {
return "Name: " + name + ", Age: " + age + ", Disease: " + disease;
}
}
class Appointment {
Doctor doctor;
Patient patient;
Appointment(Doctor doctor, Patient patient) {
[Link] = doctor;
[Link] = patient;
}
By Rohit Wakode
Suman Ramesh Tulsiani Technical Campus PAGE NO.
Faculty of Engineering Date:
void displayAppointment() {
[Link]("Doctor: " + [Link]);
[Link]("Patient: " + [Link]());
[Link]("Consultation Fee: " + [Link]());
}
}
public class HospitalManagement {
public static void main(String[] args) {
Patient p1 = new Patient("Amit", 25, "Fever");
Doctor d1 = new Doctor("Dr. Sharma");
SpecialistDoctor d2 = new SpecialistDoctor("Dr. Mehta");
Appointment a1 = new Appointment(d1, p1);
Appointment a2 = new Appointment(d2, p1);
[Link]();
[Link]();
[Link]();
try {
FileWriter fw = new FileWriter("[Link]");
[Link]([Link]());
[Link]();
[Link]("\nPatient data saved to file.");
} catch (IOException e) {
[Link]("Error saving file.");
}
}
}
By Rohit Wakode