Assignment 3
Encapsulation TOPIC Questions
1.Write a Java program that encapsulates the Employee class with the following private attributes:
id (integer)
name (String)
salary (double)
Provide getter and setter methods for each attribute. Include the following constraints:
The id must be positive.
The salary must be greater than 3000. If not, set it to a default value of 3000.
Write a TestEmployee class to:
Create an Employee object.
Set the values for the attributes using setter methods.
Display the values using getter methods.
2. Bank Account Management
Problem:
Create a BankAccount class with the following private attributes:
accountNumber (String)
accountHolderName (String)
balance (double)
Implement:
Getter and setter methods for each attribute.
Validation:The accountNumber must be a 10-digit number.
The balance cannot be negative.
A method deposit(double amount) to add money to the account. Ensure the amount is positive.
A method withdraw(double amount) to deduct money from the account. Ensure the balance is sufficient.
Hint:Use String.length() to validate the account number.
3. Product Management System
Problem: Create a Product class to represent a product in a store. The class should have the following private attributes:
productId (int)
productName (String)
price (double)
quantity (int)
Implement:Getter and setter methods for each attribute.
Validation:The price must be greater than 0.
The quantity cannot be negative.
A method calculateTotalCost() to return the total cost as price * quantity.
Test Class:
Create a product object.
Set attributes using setter methods.
Display product details using getter methods.
Print the total cost of the product.
4. Library Book System
Problem:Design a Book class with these attributes:
bookId (String)
title (String)
author (String)
price (double)
Implement:Getter and setter methods with validation:
The bookId must start with "B" followed by digits (e.g., "B123").
The price must be positive.
Add a method displayBookDetails() to print all the details of a book.
5. Student Grade Management
Problem:Create a Student class with private attributes:
rollNumber (int)
name (String)
marks (array of doubles)
Implement: Getter and setter methods for the attributes.
A method calculateAverage() to return the average marks.
A method findGrade() that assigns a grade based on the average:
A: Average >= 90
B: 80 <= Average < 90
C: 70 <= Average < 80
F: Average < 70
Test Class:
Create a student object.
Set marks using a setter method.
Display the average and grade.
6. Car Rental System
Problem: Design a Car class with the following private attributes:
carId (String)
carName (String)
rentalPricePerDay (double)
Implement:
Getter and setter methods with validation:
The rentalPricePerDay must be a positive value.
A method calculateRentalCost(int days) that calculates the total rental cost for a given number of days.
Test Class:
Create a Car object.
Input rental price and number of days.
Display the total rental cost.
Inheritance TOPIC Questions
1. Animal Hierarchy
Problem:Create a base class Animal with the following attributes and methods:
name (String)
age (int)
A method speak() that prints a generic message like "This is an animal."
Create subclasses:
Dog: Override the speak() method to print "The dog barks."
Cat: Override the speak() method to print "The cat meows."
Test Class:
Create objects of Animal, Dog, and Cat.
Call the speak() method on each object.
2. Employee-Manager Hierarchy
Problem: Create a base class Employee with the following attributes and methods:
name (String)
salary (double)
A method displayDetails() to print the employee's name and salary.
Create a subclass Manager:
Add an additional attribute bonus (double).
Override the displayDetails() method to include the bonus.
Test Class:
Create objects of Employee and Manager.
Call displayDetails() for both objects.
3. Shape Hierarchy
Problem: Create a base class Shape with:
A method calculateArea() that prints "Area calculation not defined."
Create subclasses:
Circle:
Add an attribute radius (double).
Override calculateArea() to calculate the area of a circle:
๐ × radius 2
ฯ×radius 2
Rectangle:
Add attributes length and breadth (double).
Override calculateArea() to calculate the area of a rectangle:
length × breadth
length×breadth.
Test Class:
Create objects of Shape, Circle, and Rectangle.
Call the calculateArea() method on each.
4. Vehicle Hierarchy
Problem: Create a base class Vehicle with:
brand (String)
model (String)
A method displayInfo() to print the brand and model.
Create subclasses:
Car: Add an attribute numberOfDoors (int). Override displayInfo() to include the number of doors.
Bike: Add an attribute type (String, e.g., "Sports" or "Cruiser"). Override displayInfo() to include the bike type.
Test Class:
Create objects of Vehicle, Car, and Bike.
Call displayInfo() for each object.
5. University System
Problem: Create a base class Person with:
name (String)
age (int)
A method showDetails() to print the name and age.
Create subclasses: Student:
Add an attribute course (String).
Override showDetails() to include the course.
Professor:
Add an attribute department (String).
Override showDetails() to include the department.
Test Class:
Create objects of Person, Student, and Professor.
Call showDetails() for each.
6. Bank Account Inheritance
Problem: Create a base class BankAccount with:
accountNumber (String)
balance (double)
Methods deposit(double amount) and withdraw(double amount).
Create subclasses:
SavingsAccount: Add an attribute interestRate (double). Add a method calculateInterest() that calculates interest based on the balance.
CheckingAccount: Add an attribute overdraftLimit (double). Override withdraw() to allow overdrafts up to the limit.
Test Class:
Create objects of SavingsAccount and CheckingAccount.
Test deposits, withdrawals, and interest calculation.
7. Appliance Inheritance
Problem: Create a base class Appliance with:
brand (String)
power (int, in watts)
A method showDetails() to print the brand and power.
Create subclasses:
WashingMachine: Add an attribute capacity (double, in kg). Override showDetails() to include the capacity.
Refrigerator: Add an attribute volume (double, in liters). Override showDetails() to include the volume.
Test Class:
Create objects of Appliance, WashingMachine, and Refrigerator.
Call showDetails() for each.
8. Payment System
Problem: Create a base class Payment with:
amount (double)
A method makePayment() to print "Payment of [amount] made."
Create subclasses:
CreditCardPayment: Add an attribute cardNumber (String). Override makePayment() to include the card number.
CashPayment: Add an attribute cashGiven (double). Override makePayment() to include the cash given.
Test Class:
Create objects of Payment, CreditCardPayment, and CashPayment.
Call makePayment() for each.
Comments
Post a Comment