E3_merged
E3_merged
/*
3.Inheritance: Design and develop inheritance for a given case study, identify
objects and relationships and implement inheritance wherever applicable. Employee
class hasEmp_name, Emp_id, Address, Curriculum for Second Year of Information
Technology (2019 Course), Savitribai Phule Pune University SE (Information
Technology) Syllabus (2019 Course) 37 Mail_id, and Mobile_noas members. Inherit the
classes: Programmer, Team Lead, Assistant Project Manager and Project Manager from
employee class. Add Basic Pay (BP) as the member of all the inherited classes with
97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club
fund. Generate pay slips for the employees with their gross and net salary.
*/
import java.util.*;
class Employee{
String empName;
int empId;
String empAddress;
String empMailId;
String empMobileNo;
while (!exit) {
System.out.println("Menu:");
System.out.println("1. Generate Pay Slip for Programmer");
System.out.println("2. Generate Pay Slip for Team Lead");
System.out.println("3. Generate Pay Slip for Assistant Project
Manager");
System.out.println("4. Generate Pay Slip for Project Manager");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Name: ");
String pname = sc.next();
System.out.print("Enter ID: ");
int pid = sc.nextInt();
System.out.print("Enter Address: ");
String paddress = sc.next();
System.out.print("Enter Email: ");
String pmail = sc.next();
System.out.print("Enter Mobile No: ");
String pmobile = sc.next();
System.out.print("Enter Basic Pay: ");
double ppay = sc.nextDouble();
Programmer prog = new Programmer(pname, pid, paddress, pmail,
pmobile, ppay);
prog.showPaySlip();
break;
case 2:
System.out.print("Enter Name: ");
String tlname = sc.next();
System.out.print("Enter ID: ");
int tlid = sc.nextInt();
System.out.print("Enter Address: ");
String tladdress = sc.next();
System.out.print("Enter Email: ");
String tlmail = sc.next();
System.out.print("Enter Mobile No: ");
String tlmobile = sc.next();
System.out.print("Enter Basic Pay: ");
double tlpay = sc.nextDouble();
TeamLead lead = new TeamLead(tlname, tlid, tladdress, tlmail,
tlmobile, tlpay);
lead.showPaySlip();
break;
case 3:
System.out.print("Enter Name: ");
String apname = sc.next();
System.out.print("Enter ID: ");
int apid = sc.nextInt();
System.out.print("Enter Address: ");
String apaddress = sc.next();
System.out.print("Enter Email: ");
String apmail = sc.next();
System.out.print("Enter Mobile No: ");
String apmobile = sc.next();
System.out.print("Enter Basic Pay: ");
double appay = sc.nextDouble();
AssistantProjectManager apm = new
AssistantProjectManager(apname, apid, apaddress, apmail, apmobile, appay);
apm.showPaySlip();
break;
case 4:
System.out.print("Enter Name: ");
String pmname = sc.next();
System.out.print("Enter ID: ");
int pmid = sc.nextInt();
System.out.print("Enter Address: ");
String pmaddress = sc.next();
System.out.print("Enter Email: ");
String pmmail = sc.next();
System.out.print("Enter Mobile No: ");
String pmmobile = sc.next();
System.out.print("Enter Basic Pay: ");
double pmpay = sc.nextDouble();
ProjectManager pm = new ProjectManager(pmname, pmid, pmaddress,
pmmail, pmmobile, pmpay);
pm.showPaySlip();
break;
case 5:
exit = true;
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
}
sc.close();
}
}
Output:
1. Generate Pay Slip for Programmer
2. Generate Pay Slip for Team Lead
3. Generate Pay Slip for Assistant Project Manager
4. Generate Pay Slip for Project Manager
5. Exit
Enter your choice: 3
Enter Name: Ali
Enter ID: 1227
Enter Address: Pune
Enter Email: [email protected]
Enter Mobile No: 1234567890
Enter Basic Pay: 79666
Menu:
1. Generate Pay Slip for Programmer
2. Generate Pay Slip for Team Lead
3. Generate Pay Slip for Assistant Project Manager
4. Generate Pay Slip for Project Manager
5. Exit
Enter your choice: 5
Exiting...
Experiment 08:
/*
8.File Handling: Implement a program for maintaining a database of student
records using Files. Students have Student_id,name, Roll_no, Class, marks
and address. Display the data for a few students.
1. Create Database
2. Display Database
3. Delete Records
4. Update Record
5. Search Record
*/
import java.io.*;
import java.util.*;
class Student {
String studentId;
String name;
String rollNo;
String className;
int marks;
String address;
@Override
public String toString() {
return studentId + "," + name + "," + rollNo + "," + className +
"," + marks + "," + address;
}
if (!inputFile.delete()) {
System.out.println("Could not delete original file");
}
if (!tempFile.renameTo(inputFile)) {
System.out.println("Could not rename temp file");
}
}
switch (choice) {
case 1:
System.out.print("Enter Student ID: ");
String id = scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Roll No: ");
String rollNo = scanner.nextLine();
System.out.print("Enter Class: ");
String className = scanner.nextLine();
System.out.print("Enter Marks: ");
int marks = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Address: ");
String address = scanner.nextLine();
Student student = new Student(id, name, rollNo,
className, marks, address);
createStudent(student);
System.out.println("Student record created.");
break;
case 2:
displayDatabase();
break;
case 3:
System.out.print("Enter Student ID to delete: ");
String deleteId = scanner.nextLine();
deleteRecord(deleteId);
System.out.println("Student record deleted if it
existed.");
break;
case 4:
System.out.print("Enter Student ID to update: ");
String updateId = scanner.nextLine();
System.out.print("Enter new Name: ");
String newName = scanner.nextLine();
System.out.print("Enter new Roll No: ");
String newRollNo = scanner.nextLine();
System.out.print("Enter new Class: ");
String newClassName = scanner.nextLine();
System.out.print("Enter new Marks: ");
int newMarks = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter new Address: ");
String newAddress = scanner.nextLine();
Student updatedStudent = new Student(updateId, newName,
newRollNo, newClassName, newMarks, newAddress);
updateRecord(updateId, updatedStudent);
System.out.println("Student record updated if it
existed.");
break;
case 5:
System.out.print("Enter Student ID to search: ");
String searchId = scanner.nextLine();
searchRecord(searchId);
break;
case 6:
System.out.println("Exiting the system.");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid option. Please try
again.");
}
}
}
}
Output:
@Override
protected void paint() {
System.out.println("Painting Hatchback with basic color.");
}
@Override
protected void finalMakeup() {
System.out.println("Final makeup for Hatchback.");
}
}
@Override
protected void paint() {
System.out.println("Painting Sedan with luxury color.");
}
@Override
protected void finalMakeup() {
System.out.println("Final makeup for Sedan.");
}
}
@Override
protected void paint() {
System.out.println("Painting SUV with durable color.");
}
@Override
protected void finalMakeup() {
System.out.println("Final makeup for SUV.");
}
}
class CarFactory {
public static Car createCar(String type) {
switch (type.toLowerCase()) {
case "hatchback":
return new Hatchback();
case "sedan":
return new Sedan();
case "suv":
return new SUV();
default:
throw new IllegalArgumentException("Unknown car type: " +
type);
}
}
}
System.out.println("\nCreating a Sedan:");
Car sedan = CarFactory.createCar("sedan");
System.out.println("\nCreating an SUV:");
Car suv = CarFactory.createCar("suv");
}
}
Output:
Creating a Hatchback:
Chassis allocated.
Engine allocated.
Wheels allocated.
Adding basic accessories for Hatchback.
Painting Hatchback with basic color.
Final makeup for Hatchback.
Creating a Sedan:
Chassis allocated.
Engine allocated.
Wheels allocated.
Adding premium accessories for Sedan.
Painting Sedan with luxury color.
Final makeup for Sedan.
Creating an SUV:
Chassis allocated.
Engine allocated.
Wheels allocated.
Adding rugged accessories for SUV.
Painting SUV with durable color.
Final makeup for SUV.
Experiment 02:
/*
2. Polymorphism. Identify commonalities and differences between
Publication, Book and Magazine classes. Title, Price, Copies are common
instance variables and saleCopy is a common method. The differences are,
Bookclass has author and orderCopies(). Magazine Class has methods
orderQty, Current issue, receiveissue().Write a program to find how many
copies of the given books are ordered and display total sale of publication
*/
class Publication {
protected String title;
protected double price;
protected int copies;
@Override
public void saleCopy(int numCopies) {
super.saleCopy(numCopies);
}
}
@Override
public void saleCopy(int numCopies) {
super.saleCopy(numCopies);
}
}
book1.saleCopy(10);
magazine1.saleCopy(30);
magazine1.receiveIssue("February 2024");
}
}
Output:
Ordered 20 more copies of the book "Java Programming" by John Doe.
Ordered 100 copies of the magazine "Tech Monthly".
10 copies of "Java Programming" sold at $29.99 each.
Total sale: $299.9
30 copies of "Tech Monthly" sold at $5.99 each.
Total sale: $179.7
New issue of "Tech Monthly" received: February 2024
Experiment 09:
/*
9.Case Study: Using concepts of Object-Oriented programming develop
solution for any one application
1) Banking system having following operations :
1. Create an account
2. Deposit money
3. Withdraw money
4. Honour daily withdrawal limit
5. Check the balance
6. Display Account information.
2) Inventory management system having following operations :
1. List of all products
2. Display individual product
*/
import java.util.Scanner;
class BankAccount {
private String accountNumber;
private String accountHolderName;
private double balance;
private double dailyWithdrawalLimit;
private double dailyWithdrawnAmount;
switch (choice) {
case 1:
System.out.println("Account creation not implemented in
this example.");
break;
case 2:
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 3:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 4:
System.out.println("Current Balance: $" +
account.checkBalance());
break;
case 5:
account.displayAccountInfo();
break;
case 6:
System.out.println("Exiting the system. Have a nice
day!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid option. Please choose
again.");
}
}
}
}
Output:
Banking System Menu:
1. Create Account (Account already created in this example)
2. Deposit Money
3. Withdraw Money
4. Check Balance
5. Display Account Information
6. Exit
Choose an option: 2
Enter amount to deposit: 200
Deposited: $200.0
Banking Syste
Menu: 1. Create Account (Account already created in this example)
2. Deposit Money
3. Withdraw Money
4. Check Balance
5. Display Account Information
6. Exit
Choose an option: 3
Enter amount to withdraw: 150
Withdrawn: $150.0
Experiment 07:
/*
7.Template: Implement a generic program using any collection class to count
the number of elements in a collection that have a specific property such
as even numbers, odd number, prime number and palindromes.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
Output:
Total even numbers: 3
Total odd numbers: 5
Total prime numbers: 3
Total palindrome numbers: 3
Experiment 01:
/*
1.Classes and objects: Design a class ‘Complex ‘with data members for real and
imaginary parts. Provide default and Parameterized constructors. Write a program to
perform arithmetic operations of two complex numbers.
*/
import java.text.DecimalFormat;
import java.util.*;
class Complex {
double real;
double imaginary;
Complex(){
this(0,0);
}
Output:
Enter Num1 Real part: 12
Enter Num1 Imaginary part: 7
Enter A: Addition, S: Subtraction, M: Multiplication, D: Division, E:
Exit: S
Enter Num2 Real part: 22
Enter Num2 Imaginary part: 13
import java.util.Scanner;
// Perform division
int result = num1 / num2;
System.out.println("Result of division (Num1 / Num2): " +
result);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception: Division by zero is
not allowed.");
} catch (NumberFormatException e) {
System.out.println("Number Format Exception: Please enter valid
integers.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Out of Bounds Exception: Tried
to access an invalid array index.");
} finally {
scanner.close();
System.out.println("Program execution completed.");
}
}
}
Output:
//Test 1
Enter the first number (Num1): 10
Enter the second number (Num2): 0
Arithmetic Exception: Division by zero is not allowed. Program
execution completed.
//Test 2
Enter the first number (Num1): ten
Enter the second number (Num2): five
Number Format Exception: Please enter valid integers. Program
execution completed.
//Test 3
Enter the first number (Num1): 10
Enter the second number (Num2): 2
Result of division (Num1 / Num2): 5
Array Index Out of Bounds Exception: Tried to access an invalid array
index. Program execution completed.
Experiment 04:
/*
4.Dynamic Binding: Design a base class shape with two double type values and member
functions to input the data and compute_area() for calculating area of shape.
Derive two classes: triangle and rectangle. Make compute_area() as an abstract
function and redefine this function in the derived class to suit their
requirements. Write a program that accepts dimensions of triangle/rectangle and
displays calculated area. Implement dynamic binding for given case study
*/
import java.util.*;
void calc_area() {
System.out.println("The area of rectangle is: " + length * breath);
}
}
void calc_area() {
System.out.println("The area of triangle is: " + (0.5 * length * breath));
}
}
public class Assignment_04 {
do {
switch(res) {
case "R" -> {
System.out.print("Enter lenght: ");
double length = sc.nextDouble();
System.out.print("Enter breath: ");
double breath = sc.nextDouble();
Rectangle R1 = new Rectangle(length, breath);
R1.calc_area();
}
case "T" -> {
System.out.print("Enter lenght: ");
double length = sc.nextDouble();
System.out.print("Enter breath: ");
double breath = sc.nextDouble();
Triangle T1 = new Triangle(length, breath);
T1.calc_area();
}
default -> {
System.out.println("Invalid input!");
}
}
System.out.print("Do you want continue: Y/N?: ");
String input = sc.next();
if("N".equals(input)) {
break;
}else {
System.out.print("Enter R: Rectagle, T: Triangle, E: Exit : ");
res = sc.next();
}
}while(!"E".equals(res));
}
}
Output:
Enter R: Rectangle, T: Triangle, E: Exit : T
Enter length: 44
Enter breath: 17
The area of triangle is: 374.0
Do you want continue: Y/N?: y
Enter R: Rectangle, T: Triangle, E: Exit : R
Enter length: 19
Enter breath: 11
The area of rectangle is: 209.0
Do you want continue: Y/N?: N
Experiment 05:
/*
5.Interface: Design and develop a context for given case study and
implement an interface for Vehicles Consider the example of vehicles like
bicycle, car and bike. All Vehicles have common functionalities such as
Gear Change, Speed up and apply brakes. Make an interface and put all these
common functionalities. Bicycle, Bike, Car classes should be implemented
for all these functionalities in their own class in their own way
*/
interface Vehicle {
void changeGear(int gear);
void speedUp(int increment);
void applyBrakes(int decrement);
}
public Bicycle() {
this.speed = 0;
this.gear = 1;
}
@Override
public void changeGear(int gear) {
this.gear = gear;
System.out.println("Bicycle gear changed to: " + this.gear);
}
@Override
public void speedUp(int increment) {
speed += increment;
System.out.println("Bicycle speed increased to: " + speed + "
km/h");
}
@Override
public void applyBrakes(int decrement) {
speed -= decrement;
if (speed < 0) speed = 0;
System.out.println("Bicycle speed after applying brakes: " + speed
+ " km/h");
}
}
public Bike() {
this.speed = 0;
this.gear = 1;
}
@Override
public void changeGear(int gear) {
this.gear = gear;
System.out.println("Bike gear changed to: " + this.gear);
}
@Override
public void speedUp(int increment) {
speed += increment;
System.out.println("Bike speed increased to: " + speed + " km/h");
}
@Override
public void applyBrakes(int decrement) {
speed -= decrement;
if (speed < 0) speed = 0;
System.out.println("Bike speed after applying brakes: " + speed + "
km/h");
}
}
public Car() {
this.speed = 0;
this.gear = 1;
}
@Override
public void changeGear(int gear) {
this.gear = gear;
System.out.println("Car gear changed to: " + this.gear);
}
@Override
public void speedUp(int increment) {
speed += increment;
System.out.println("Car speed increased to: " + speed + " km/h");
}
@Override
public void applyBrakes(int decrement) {
speed -= decrement;
if (speed < 0) speed = 0;
System.out.println("Car speed after applying brakes: " + speed + "
km/h");
}
}
System.out.println("Testing Bicycle:");
bicycle.changeGear(2);
bicycle.speedUp(10);
bicycle.applyBrakes(3);
System.out.println("\nTesting Bike:");
bike.changeGear(3);
bike.speedUp(20);
bike.applyBrakes(5);
System.out.println("\nTesting Car:");
car.changeGear(4);
car.speedUp(30);
car.applyBrakes(10);
}
}
Output
Testing Bicycle:
Bicycle gear changed to: 2
Bicycle speed increased to: 10 km/h
Bicycle speed after applying brakes: 7 km/h
Testing Bike:
Bike gear changed to: 3
Bike speed increased to: 20 km/h
Bike speed after applying brakes: 15 km/h
Testing Car:
Car gear changed to: 4
Car speed increased to: 30 km/h
Car speed after applying brakes: 20 km/h
Experiment 10:
/*
11. Strategy Design Pattern: Implement and apply Strategy Design pattern
for simple Shopping Cart where three payment strategies are used such as
Credit Card, PayPal, Bitcoin. Create an interface for strategy patterns and
give concrete implementation for payment.
*/
// PaymentStrategy.java
public interface PaymentStrategy {
void pay(double amount);
}
// CreditCardPayment.java
public class CreditCardPayment implements PaymentStrategy {
private String cardNumber;
private String name;
private String cvv;
@Override
public void pay(double amount) {
System.out.println("Paid $" + amount + " using Credit Card.");
}
}
// PayPalPayment.java
public class PayPalPayment implements PaymentStrategy {
private String email;
// BitcoinPayment.java
public class BitcoinPayment implements PaymentStrategy {
private String walletAddress;
@Override
public void pay(double amount) {
System.out.println("Paid $" + amount + " using Bitcoin.");
}
}
// ShoppingCart.java
import java.util.ArrayList;
import java.util.List;
public ShoppingCart() {
this.items = new ArrayList<>();
}
// Main.java
public class Main {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
cart.addItem(100.00);
cart.addItem(200.50);
Output: