Skip to main content

Assignment 3

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

Popular posts from this blog

Step-by-Step: Launch Browser, Context, and Page in Playwright and Run Test and Configuration (JavaScript)

🎥 Setup Browser, Context, Page & Run Config Test Scripts with package.json & playwright.config.js Step-by-Step: Launch Browser, Context, and Page in Playwright and Run Test and Configuration (JavaScript) 1. Install Playwright You can install Playwright using the following command: npm init playwright@latest 2. Create a Basic Test Script Understand the core Playwright architecture: Element Description browser Controls the browser instance (like Chrome, Firefox, etc.) context Acts like a separate browser profile (cookies, localStorage are isolated) page A single browser tab where interaction happens 3. Run the Test npx playwright test example.spec.js Ways to Run TypeScript Tests Way Command Notes 🟢 Via npx npx playwright test Uses built-in TypeScript support 🟢 With s...

Playwright Test Structure in Details -Session-02

🎥 Playwright: test.only, Hooks & Grouping with test.describe Explained Let’s go step-by-step , showing how to build from a single test , to using beforeEach / afterEach , and then organizing things with test.describe . ✅ Step 1: Basic Single Test with test.only import { test, expect } from '@playwright/test' ; test. only ( '🚀 Basic test - check title' , async ({ page }) => { await page. goto ( 'https://example.com' ); await expect (page). toHaveTitle ( /Example Domain/ ); }); test.only ensures only this test runs — great for debugging. ✅ Step 2: Add beforeEach and afterEach import { test, expect } from '@playwright/test' ; test. beforeEach ( async ({ page }) => { console . log ( '🔄 Setting up before each test' ); await page. goto ( 'https://example.com' ); }); test. afterEach ( async ({ page }, testInfo) => { console . log ( `📦 Finished test: ${testInfo.title} `); }); test. only ( ...

How to Maximize a Browser window using Playwright Java

  Playwright itself does not have a direct maximize () method like some other browser automation tools, but you can achieve the effect by setting the viewport to the screen size of your display. Below are ways to handle Maximize a browser in Playwright Java  Ø   Setting the viewport to the screen size  o    setViewportSize(1920, 1080): This sets the browser window to a full HD resolution, effectively maximizing it. o    setHeadless(false): Ensures the browser runs in a visible mode so you can see the window being maximized.  Code Snapshot  Playwright playwright = Playwright. create ()   ;   Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));     // Create a new browser context   BrowserContext context = browser.newContext(new Browser.NewContextOptions()          .setViewportSize(1920, 1080));     ...