0% found this document useful (0 votes)
12 views16 pages

Java 5-8

Uploaded by

rajkirannaidu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Java 5-8

Uploaded by

rajkirannaidu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Lab programs week-2

5. Design a “farm animals” java application with the details of animals


like cow, pig, horse. Consider the following details like where they stay,
what they eat, the sound they make by using classes and objects

class Animal {
String name;

public Animal(String name) {


this.name = name;
}

public void makeSound() {


System.out.println("The " + name + " makes a sound.");
}

public void eat() {


System.out.println("The " + name + " eats something.");
}

public void whereTheyStay() {


System.out.println("The " + name + " stays in a place.");
}
}

class Cow extends Animal {


public Cow() {
super("Cow");
}

public void makeSound() {


System.out.println("The Cow says: Moo!");
}

public void eat() {


System.out.println("The Cow eats grass.");
}

public void whereTheyStay() {


System.out.println("The Cow stays in a barn.");
}
}

class Pig extends Animal {


public Pig() {
super("Pig");
}

public void makeSound() {


System.out.println("The Pig says: Oink!");
}

public void eat() {


System.out.println("The Pig eats slop.");
}

public void whereTheyStay() {


System.out.println("The Pig stays in a pigsty.");
}
}

class Horse extends Animal {


public Horse() {
super("Horse");
}

public void makeSound() {


System.out.println("The Horse says: Neigh!");
}

public void eat() {


System.out.println("The Horse eats hay.");
}

public void whereTheyStay() {


System.out.println("The Horse stays in a stable.");
}
}

public class FarmAnimals {


public static void main(String[] args) {
Animal cow = new Cow();
Animal pig = new Pig();
Animal horse = new Horse();

cow.makeSound();
cow.eat();
cow.whereTheyStay();
System.out.println();

pig.makeSound();
pig.eat();
pig.whereTheyStay();

System.out.println();

horse.makeSound();
horse.eat();
horse.whereTheyStay();
}
}

Output:

The Cow says: Moo!

The Cow eats grass.

The Cow stays in a barn.

The Pig says: Oink!

The Pig eats slop.

The Pig stays in a pigsty.

The Horse says: Neigh!

The Horse eats hay.

The Horse stays in a stable.

6.An organization is maintaining the data of employee according to cadre


of employee with following parameters name, id, designation, salary,
promotion status. Apply the constructor overloading to implement it.

public class Employee {


private String name;
privateint id;
private String designation;
private double salary;
private Boolean promotionStatus;

// Constructor for regular employees


public Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.designation = "Employee";
this.salary = salary;
this.promotionStatus = false;
}

// Constructor for managers


public Employee(String name, int id, double salary, booleanpromotionStatus) {
this.name = name;
this.id = id;
this.designation = "Manager";
this.salary = salary;
this.promotionStatus = promotionStatus;
}

// Constructor for executives


public Employee(String name, int id, double salary, Boolean promotionStatus, String
designation) {
this.name = name;
this.id = id;
this.designation = designation;
this.salary = salary;
this.promotionStatus = promotionStatus;
}
// Getter methods (not included in the original question)
public String getName() {
return name;
}

Public int getId() {


return id;
}

public String getDesignation() {


return designation;
}

public double getSalary() {


return salary;
}

Public Boolean getPromotionStatus() {


Return promotionStatus;
}

public static void main(String[] args) {


// Example usage of the Employee class with different constructors
Employee regularEmployee = new Employee("John Doe", 1001, 50000.0);
Employee manager = new Employee("Jane Smith", 2001, 80000.0, true);
Employee executive = new Employee("Robert Johnson", 3001, 120000.0, true,
"Executive");

// Displaying employee information


displayEmployeeInformation(regularEmployee);
displayEmployeeInformation(manager);
displayEmployeeInformation(executive);
}

// Method to display employee information


private static void displayEmployeeInformation(Employee employee) {
System.out.println("Employee Information:");
System.out.println("Name: " + employee.getName());
System.out.println("ID: " + employee.getId());
System.out.println("Designation: " + employee.getDesignation());
System.out.println("Salary: " + employee.getSalary());
System.out.println("Promotion Status: " + employee.getPromotionStatus());
System.out.println();
}
}

Output:
Employee Information:
Name: John Doe
ID: 1001
Designation: Employee
Salary: 50000.0
Promotion Status: false

Employee Information:
Name: Jane Smith
ID: 2001
Designation: Manager
Salary: 80000.0
Promotion Status: true

Employee Information:
Name: Robert Johnson
ID: 3001
Designation: Executive
Salary: 120000.0
Promotion Status: true

7. All the banks operating in India are controlled by RBI. (e.g. minimum
interest rate, minimum balance allowed, maximum withdrawal limit etc)
which all banks must follow. Suppose RBI has set minimum interest rate
applicable to a saving bank account to be 4% annually. however, banks
are free to use 4% interest rate or to set any rates above it.
Write a JAVA program to implement bank functionality in the above
scenario and demonstrate the dynamic polymorphism concept. Note:
Create few classes namely Customer, Account, RBI (Base Class) and
few derived classes (SBI, ICICI, PNB etc). Assume and implement
required member variables and functions in each class.
Testcase1:
Enter the Bank name to find the rate of Interest : RBI
RBI rate of interest is : 4%

importjava.util.Scanner;

// Base class RBI


class RBI {
// Default interest rate set by RBI
protected double interestRate = 4.0;

public double getInterestRate() {


return interestRate;
}
}

// Derived class SBI


class SBI extends RBI {
// Override interestRate for SBI
// SBI can set its own interest rate, but it must be above the RBI minimum

public double getInterestRate() {


// Assume SBI sets an interest rate of 5%
return 5.0;
}
}

// Derived class ICICI


class ICICI extends RBI {
// Override interestRate for ICICI
// ICICI can set its own interest rate, but it must be above the RBI minimum

public double getInterestRate() {


// Assume ICICI sets an interest rate of 6%
return 6.0;
}
}

// Derived class PNB


class PNB extends RBI {
// Override interestRate for PNB
// PNB can set its own interest rate, but it must be above the RBI minimum

public double getInterestRate() {


// Assume PNB sets an interest rate of 4.5%
return 4.5;
}
}

// Class to represent a customer


class Customer {
private String name;

public Customer(String name) {


this.name = name;
}

public String getName() {


return name;
}
}

// Class to represent an account


class Account {
private Customer customer;
private RBI bank;

public Account(Customer customer, RBI bank) {


this.customer = customer;
this.bank = bank;
}

public void displayInterestRate() {


System.out.println(customer.getName() + "'s Bank: " + bank.getClass().getSimpleName());
System.out.println("Interest Rate: " + bank.getInterestRate() + "%\n");
}
}

public class BankDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Create instances of customers


Customer customer1 = new Customer("John");
Customer customer2 = new Customer("Alice");

// Create instances of different banks


RBI rbi = new RBI();
SBI sbi = new SBI();
ICICI icici = new ICICI();
PNB pnb = new PNB();

// Create accounts for customers in different banks


Account account1 = new Account(customer1, rbi);
Account account2 = new Account(customer2, sbi);

// Display interest rates for each account


account1.displayInterestRate();
account2.displayInterestRate();

System.out.print("Enter the Bank name to find the rate of Interest: ");


String bankName = scanner.nextLine().toUpperCase();

// Demonstrate dynamic polymorphism


switch (bankName) {
case "RBI":
Account accountRBI = new Account(customer1, rbi);
accountRBI.displayInterestRate();
break;
case "SBI":
Account accountSBI = new Account(customer1, sbi);
accountSBI.displayInterestRate();
break;
case "ICICI":
Account accountICICI = new Account(customer1, icici);
accountICICI.displayInterestRate();
break;
case "PNB":
Account accountPNB = new Account(customer1, pnb);
accountPNB.displayInterestRate();
break;
default:
System.out.println("Invalid bank name.");
}

scanner.close();
}
}

Output:1

John's Bank: RBI


Interest Rate: 4.0%
Alice's Bank: SBI
Interest Rate: 5.0%

Enter the Bank name to find the rate of Interest: PNB


John's Bank: PNB
Interest Rate: 4.5%

Output:2
csmb java lab>java BankDemo
John's Bank: RBI
Interest Rate: 4.0%

Alice's Bank: SBI


Interest Rate: 5.0%

Enter the Bank name to find the rate of Interest: SBI


John's Bank: SBI
Interest Rate: 5.0%

output:3
csmb java lab>java BankDemo
John's Bank: RBI
Interest Rate: 4.0%

Alice's Bank: SBI


Interest Rate: 5.0%

Enter the Bank name to find the rate of Interest: RBI


John's Bank: RBI
Interest Rate: 4.0%

Output:4
csmb java lab>java BankDemo
John's Bank: RBI
Interest Rate: 4.0%

Alice's Bank: SBI


Interest Rate: 5.0%

Enter the Bank name to find the rate of Interest: ICICI


John's Bank: ICICI
Interest Rate: 6.0%

8. Different categories of employees are working in a software company


like Regular Employees, Contract Employees and Vendors. And their pay
roll is different for regular and contract employees. For the regular
employees Basic pay is 25000, HRA is 15000rs and TA is 5000. For the
Contract employees Basic pay is 12000 TA is 3000rs and there is no HRA.
Find the monthly salary details of Employee. If input is Regular Employee
display the Regular employee salary details. If input is Contract based
display the Contract salary details.
TestCase1: Input: Enter Employee Id: R101
Output: Salary Details:
Basic Pay: 25000 HRA: 15000 T.A: 5000 Total Amount: 45000

importjava.util.Scanner;

// Base class Employee


class Employee {
protected String employeeId;
public Employee(String employeeId) {
this.employeeId = employeeId;
}

public void displaySalaryDetails() {


System.out.println("Salary Details for Employee " + employeeId + ":");
}
}

// Derived class RegularEmployee


classRegularEmployee extends Employee {
private double basicPay = 25000.0;
private double hra = 15000.0;
private double ta = 5000.0;

public RegularEmployee(String employeeId) {


super(employeeId);
}

public void displaySalaryDetails() {


super.displaySalaryDetails();
System.out.println("Basic Pay: " + basicPay + "\tHRA: " + hra + "\tT.A: " + ta +
"\tTotal Amount: " + (basicPay + hra + ta));
}
}

// Derived class ContractEmployee


Class ContractEmployee extends Employee {
private double basicPay = 12000.0;
private double ta = 3000.0;

public ContractEmployee(String employeeId) {


super(employeeId);
}

public void displaySalaryDetails() {


super.displaySalaryDetails();
System.out.println("Basic Pay: " + basicPay + "\tT.A: " + ta +
"\tTotal Amount: " + (basicPay + ta));
}
}

public class SalaryDetailsDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter Employee Id: ");


String employeeId = scanner.nextLine();

System.out.print("Enter Employee Type (Regular or Contract): ");


String employeeType = scanner.nextLine().toLowerCase();

Employee employee;

if (employeeType.equals("regular")) {
employee = new RegularEmployee(employeeId);
} else if (employeeType.equals("contract")) {
employee = new ContractEmployee(employeeId);
} else {
System.out.println("Invalid Employee Type.");
scanner.close();
return;
}

employee.displaySalaryDetails();

scanner.close();
}
}

Output:1
Enter Employee Id: r1
Enter Employee Type (Regular or Contract): regular
Salary Details for Employee r1:
Basic Pay: 25000.0 HRA: 15000.0 T.A: 5000.0 Total Amount: 45000.0

Output :2
csmb java lab>java SalaryDetails
Enter Employee Id: r2
Enter Employee Type (Regular or Contract): contract
Salary Details for Employee r2:
Basic Pay: 12000.0 T.A: 3000.0 Total Amount: 15000.0

You might also like