0% found this document useful (0 votes)
40 views10 pages

Labmidpaper

hello

Uploaded by

zd64811
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)
40 views10 pages

Labmidpaper

hello

Uploaded by

zd64811
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
You are on page 1/ 10

Object oriented

programming

Lab Mid
(oop)

Submitted by :
Muhammad Zohaib (sp23-bcs-071)
Submitted to :
Mam Hasina kainat
Question_No:1:

You work for a car rental company, and your task is to implement a system that models different
cars in the fleet. Each car has an engine, tires, and a fuel tank, all of which contribute to the car's
overall functionality. You need to create classes to represent these components and show how
the car uses them to display relevant information (e.g., horsepower, tire type, fuel level, etc.).

Answer:

public class Car { private


Engine engine; private Tire
tires; private FuelTank
fuelTank;
public Car(Engine engine, Tire tires, FuelTank fuelTank) {
this.engine = engine; this.tires = tires; this.fuelTank
= fuelTank;
}

public void displayCarInfo() {


System.out.println("Engine Horsepower: " + engine.getHorsepower());
System.out.println("Tire Type: " + tires.getType());
System.out.println("Fuel Level: " + fuelTank.getFuelLevel());
}
}

public class Engine {


private int horsepower ;

public Engine(int horsepower){


this.horsepower = horsepower;
}
public int getHorsepower(){
return horsepower;
}

}
public class FuelTank { private
double fuelLevel;

public FuelTank(double fuelLevel) {


this.fuelLevel = fuelLevel;
}
public double getFuelLevel() {
return fuelLevel;
}

public void refuel(double amount) {


this.fuelLevel += amount;
}
}

public class Tire {


private String type;

public Tire(String type){


this.type = type;
}
public String getType(){
return type;
}
}

public class Main {


public static void main(String[] args) {

Engine engine = new Engine(200);


Tire tires = new Tire("All-Season");
FuelTank fuelTank = new FuelTank(50);

Car car = new Car(engine, tires, fuelTank);


car.displayCarInfo();
}
}
Output:

Question_No:02:

Suppose you have to develop a human resources management system for a company. The
company has both full-time and part-time employees, and you need to design a class hierarchy
that represents these employees. Each employee has a name, position, and salary. Full-time
employees have a fixed salary, while part-time employees are paid based on their hourly rate
and the number of hours they work. You also need to calculate their salary and display their
details in the system for both full –time employees and part-time.

Answer:

abstract class Employee {


private String name; private
String position;

public Employee(String name, String position) {


this.name = name;
this.position = position;
}

public abstract double calculateSalary();

public void displayDetails() {


System.out.println("Name: " + name);
System.out.println("Position: " + position);
System.out.println("Salary: " + calculateSalary());
}

}
class FullTimeEmployee extends Employee {
private double salary;

public FullTimeEmployee(String name, String position, double salary) { super(name,


position);
this.salary = salary;
}

@Override
public double calculateSalary() {
return salary;
}
}

class PartTimeEmployee extends Employee {


private double hourlyRate; private int
hoursWorked;
public PartTimeEmployee(String name, String position, double hourlyRate, int
hoursWorked) { super(name, position); this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}

@Override public double


calculateSalary() {
return hourlyRate * hoursWorked;
}
}

Output:

Question_No:03:

Design a system for managing the operations of a Library, which allows users to borrow and
return books, search for books, and manage their memberships. The system should support
multiple libraries, each containing various categories of books (e.g., Fiction, NonFiction, Science,
History). Each book has attributes like title, author, ISBN, genre, and availability status.

System Features:

· Library Management: Libraries can manage their collection of books, update book details, track
the borrowing and return process, and mark books as available or unavailable.

· User Membership: Users (members) must register in the system, providing their personal
details, membership ID, and contact information. They can search for books based on title,
author, genre, or availability, and borrow them.

· Book Borrowing: Once a user finds a suitable book, they can borrow it for a specified duration.
The borrowing process includes the due date and any late fees if the book is not returned on
time. Users can also extend the borrowing period.

The system should ensure that both library staff and users can interact with the system
intuitively, allowing library staff to manage the collection and track borrowing activities
effectively.

Requirements for the Class Diagram:


1. Identify the main classes involved in the system.

2. Define attributes for each class based on the system’s requirements (e.g., books, users, library
staff).

3. Define methods for each class, such as searching for books, borrowing, returning, and
generating receipts.

4. Demonstrate encapsulation where necessary, particularly for sensitive data like user
information and payment details for late fees.

5. Illustrate relationships between classes, such as composition, aggregation, and inheritance.


Use composition where one entity is wholly dependent on the existence of another (e.g., a book
cannot exist without a library), and aggregation where one entity can exist independently of
another (e.g., a user can borrow multiple books, but they can also exist without borrowing).

Answer: import

java.util.*;

public class Book { private


String title; private String
author; private String ISBN;
private String genre; private
boolean isAvailable;

public Book(String title, String author, String ISBN, String genre) {


this.title = title; this.author = author; this.ISBN = ISBN;
this.genre = genre;
this.isAvailable = true;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public String getGenre() {


return genre;
}

public boolean isAvailable() {


return isAvailable;
}

public void markAsBorrowed() {


if (isAvailable) {
isAvailable = false;
System.out.println(title + " has been borrowed.");
} else {
System.out.println(title + " is currently unavailable.");
}
}

public void markAsReturned() {


isAvailable = true;
System.out.println(title + " has been returned.");
}
}
class User { private String
name; private String
membershipID; private String
contactDetails;
private List<Book> borrowedBooks;

public User(String name, String membershipID, String contactDetails) {


this.name = name; this.membershipID = membershipID;
this.contactDetails = contactDetails; this.borrowedBooks = new
ArrayList<>();
}

public void borrowBook(Book book) {


if (book.isAvailable()) {
book.markAsBorrowed();
borrowedBooks.add(book);
} else {
System.out.println("Cannot borrow. The book is already borrowed.");
}
}
public void returnBook(Book book) {
if (borrowedBooks.contains(book)) {
book.markAsReturned();
borrowedBooks.remove(book);
} else {
System.out.println("This book was not borrowed by the user.");
}
}

public void displayBorrowedBooks() {


System.out.println("Borrowed Books:"); for
(Book book : borrowedBooks) {
System.out.println("- " + book.getTitle());
}
}
}
class Library { private
String name; private String
address; private
List<Book> books;

public Library(String name, String address) {


this.name = name; this.address = address;
this.books = new ArrayList<>();
}

public void addBook(Book book) {


books.add(book);
System.out.println(book.getTitle() + " has been added to the library.");
}

public void removeBook(Book book) {


if (books.remove(book)) {
System.out.println(book.getTitle() + " has been removed from the library.");
} else {
System.out.println("Book not found in the library.");
}
}

public void searchBooks(String searchTerm) { System.out.println("Search


Results:");
for (Book book : books) { if
(book.getTitle().toLowerCase().contains(searchTerm.toLowerCase()) ||
book.getAuthor().toLowerCase().contains(searchTerm.toLowerCase()) ||
book.getGenre().toLowerCase().contains(searchTerm.toLowerCase())) {
System.out.println("- " + book.getTitle() + " by " + book.getAuthor() +
" (Genre: " + book.getGenre() + ", Available: " + book.isAvailable() + ")");
}
}
}

public void displayAllBooks() {


System.out.println("Library Collection:");
for (Book book : books) {
System.out.println("- " + book.getTitle() + " by " + book.getAuthor() +
" (Genre: " + book.getGenre() + ", Available: " + book.isAvailable() + ")");
}
}
}

public class Main {


public static void main(String[] args) {
Library library = new Library("City Library", "123 Library St.");

Book book1 = new Book("The Alchemist", "Paulo Coelho", "123456789", "Fiction");


Book book2 = new Book("Sapiens", "Yuval Noah Harari", "987654321", "Non-Fiction");
Book book3 = new Book("Brief History of Time", "Stephen Hawking", "555555555", "Science");

library.addBook(book1);
library.addBook(book2); library.addBook(book3);

User user = new User("John Doe", "U001", "[email protected]");

System.out.println("\nUser searches for 'history':");


library.searchBooks("history");

System.out.println("\nUser borrows a book:");


user.borrowBook(book3);

System.out.println("\nDisplaying borrowed books:");


user.displayBorrowedBooks();
System.out.println("\nUser returns a book:");
user.returnBook(book3);

System.out.println("\nDisplaying all books in the library:");


library.displayAllBooks();
}
}

Output:

You might also like