0% found this document useful (0 votes)
49 views9 pages

Java OOP Practice Problems Guide

The document outlines various object-oriented programming (OOP) practice problems in Java, including systems for managing libraries, laptops, student enrollments, travel agencies, and hospital appointments. Each system includes class definitions, tasks to be accomplished, input and output formats, and sample test cases. The problems focus on data management and retrieval using lists and classes, emphasizing the application of OOP principles.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views9 pages

Java OOP Practice Problems Guide

The document outlines various object-oriented programming (OOP) practice problems in Java, including systems for managing libraries, laptops, student enrollments, travel agencies, and hospital appointments. Each system includes class definitions, tasks to be accomplished, input and output formats, and sample test cases. The problems focus on data management and retrieval using lists and classes, emphasizing the application of OOP principles.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java OOP Practice Problems

📚 Library Management System


Design a system to manage books in multiple libraries.

📦 Classes:
 Class: Book (bookId: int, title: String, author: String, price: double, pages: int)
 Class: Library (libraryId: int, libraryName: String, books: List<Book>)

🎯 Tasks:
1. Find books with page count greater than X.
2. Calculate average price of books in a library.
3. Find the library with the highest number of books.

🔤 Input Format:
First line: Integer n (number of libraries)
For each library:
Library ID, Library Name, Number of books
For each book: Book ID, Title, Author, Price, Pages
Last line: An integer X for page count filter

📤 Output Format:
List of books with pages > X
Average book price of each library
Library with highest number of books

🧪 Sample Test Cases:


4. Test Case 1

Input:
2
101
CityLibrary
2
201
Java101
JohnDoe
500.0
300
202
Python
Alice
600.0
350
102
TownLibrary
1
203
C++
Bob
450.0
280
320

Output:
Books with pages > 320:
202 Python
Average book price:
CityLibrary: 550.00
TownLibrary: 450.00
Library with most books: CityLibrary

5. Test Case 2

Input:
1
103
CollegeLibrary
3
204
OS
Tanmay
300.0
290
205
DBMS
Ravi
350.0
310
206
CN
Kiran
400.0
270
300
Output:
Books with pages > 300:
205 DBMS
Average book price:
CollegeLibrary: 350.00
Library with most books: CollegeLibrary

💻 Laptop Inventory System


Manage laptop models available in various stores.

📦 Classes:
 Class: Laptop (laptopId: int, model: String, brand: String, ramSize: int, price: double)
 Class: Store (storeId: int, storeName: String, laptops: List<Laptop>)

🎯 Tasks:
6. Find the most expensive laptop in each store.
7. Filter laptops by RAM size.
8. Find the store with the highest total laptop value.

🔤 Input Format:
First line: Integer n (number of stores)
For each store:
Store ID, Store Name, Number of laptops
For each laptop: Laptop ID, Model, Brand, RAM Size, Price
Last line: RAM size threshold

📤 Output Format:
List of laptops with RAM >= threshold
Most expensive laptop in each store
Store with highest total laptop value

🧪 Sample Test Cases:


9. Test Case 1

Input:
2
1
ElectroHub
2
101
ThinkPad
Lenovo
8
70000.0
102
MacBook
Apple
16
150000.0
2
GadgetWorld
1
103
Pavilion
HP
8
60000.0
8

Output:
Laptops with RAM >= 8:
101 ThinkPad
102 MacBook
103 Pavilion
Most expensive laptops:
ElectroHub: MacBook
GadgetWorld: Pavilion
Store with highest value: ElectroHub

10. Test Case 2

Input:
1
3
LaptopBazaar
2
201
XPS
Dell
16
120000.0
202
Inspiron
Dell
4
50000.0
12
Output:
Laptops with RAM >= 12:
201 XPS
Most expensive laptops:
LaptopBazaar: XPS
Store with highest value: LaptopBazaar

🏫 Student Course Enrollment System


Track student enrollment in different courses.

📦 Classes:
 Class: Student (studentId: int, name: String, age: int)
 Class: Course (courseId: int, courseName: String, students: List<Student>)

🎯 Tasks:
11. Find the average age of students in a course.
12. Find the course with the most students.
13. Print a message if a course has no students.

🔤 Input Format:
First line: Integer n (number of courses)
For each course:
Course ID, Course Name, Number of students
For each student: Student ID, Name, Age
Last line: Course ID to calculate average age for

📤 Output Format:
Average age of students in selected course
Course with most students

🧪 Sample Test Cases:


14. Test Case 1

Input:
2
101
Java
2
201
Alice
20
202
Bob
22
102
Python
1
203
Charlie
21
101

Output:
Average age of students: 21.00
Course with most students: Java

15. Test Case 2

Input:
1
103
C++
0
103

Output:
No students enrolled in course: C++
Course with most students: C++

✈️Travel Agency Booking System


Manage traveler records under different travel agencies.

📦 Classes:
 Class: Traveler (travelerId: int, name: String, destination: String, budget: double)
 Class: Agency (agencyId: int, agencyName: String, travelers: List<Traveler>)

🎯 Tasks:
16. Count travelers going to a specific destination.
17. Find agency with most travelers.
18. Calculate average budget of travelers in each agency.

🔤 Input Format:
First line: Integer n (number of agencies)
For each agency:
Agency ID, Name, Number of travelers
For each traveler: ID, Name, Destination, Budget
Last line: Destination name to filter
📤 Output Format:
Count of travelers to destination
Average budget per agency
Agency with most travelers

🧪 Sample Test Cases:


19. Test Case 1

Input:
2
1
HolidayTours
2
101
Amit
Paris
100000
102
Rita
Rome
80000
2
DreamTravels
1
103
Sohan
Paris
120000
Paris

Output:
Travelers to Paris: 2
Average budgets:
HolidayTours: 90000.00
DreamTravels: 120000.00
Agency with most travelers: HolidayTours

20. Test Case 2

Input:
1
3
FunRides
1
104
Anita
Dubai
70000
London

Output:
Travelers to London: 0
Average budgets:
FunRides: 70000.00
Agency with most travelers: FunRides

🏥 Hospital Appointments System (Extended)


Track doctor-patient appointments.

📦 Classes:
 Class: Appointment (appointmentId: int, doctorId: int, patientId: int, date: String)
 Class: Doctor (doctorId: int, name: String)
 Class: Patient (patientId: int, name: String)

🎯 Tasks:
21. Print all appointments for a doctor.
22. Count appointments on a specific date.
23. Find doctor with most appointments.

🔤 Input Format:
First line: Integer n (number of appointments)
For each: Appointment ID, Doctor ID, Patient ID, Date
Last line: Doctor ID and Date to search

📤 Output Format:
Appointments for selected doctor
Count for selected date
Doctor with most appointments

🧪 Sample Test Cases:


24. Test Case 1

Input:
3
1
101
201
2024-01-10
2
102
202
2024-01-11
3
101
203
2024-01-10
101
2024-01-10

Output:
Appointments for Doctor 101:
201, 203
Appointments on 2024-01-10: 2
Doctor with most appointments: 101

25. Test Case 2

Input:
2
4
103
205
2024-02-12
5
104
206
2024-02-15
104
2024-02-12

Output:
Appointments for Doctor 104:
206
Appointments on 2024-02-12: 1
Doctor with most appointments: 103

Common questions

Powered by AI

The system design choice considerably impacts the scalability of calculating the average laptop price. In a system where each store maintains a running total and count of laptop prices, calculating the average can be done in constant time O(1). However, in a design where the price must be recalculated from scratch each time the average is needed, the time complexity would be O(n) where n is the number of laptops, impacting scalability as stores grow. Choosing a design that updates the average dynamically can significantly reduce computational overhead and improve real-time query performance .

An efficient method involves maintaining a running sum and count of book prices. When a new book is added, update the total sum by adding the new book’s price and increment the count by one. The average can then be updated in constant time (O(1)) by dividing the updated sum by the count. This approach avoids recalculating the total from scratch, thereby offering a scalable solution as the number of books grows .

A balanced binary search tree, such as a red-black tree, would be ideal for dynamic filtering of appointments by dates, offering logarithmic time complexity for insertion, deletion, and lookup operations. This structure allows for efficient range queries, crucial for filtering appointments by specific date ranges. Additionally, implementing an interval tree could further enhance performance when working with overlapping date ranges, providing a robust solution for dynamic data .

To efficiently find books with page counts greater than a given threshold in a library management system, an iterative approach over the list of books in each library can be employed. For each book, simply compare its page count to the threshold. This approach has a time complexity of O(n), where n is the total number of books, as each book is checked exactly once. Efficient indexing or parallel processing can speed up this process depending on the context .

Accurate calculation of the average student age requires ensuring that all student age entries are valid and within a logical range. Implementing data validation checks during student enrollment can prevent errors. Handling missing data by excluding incomplete records from calculations can ensure accuracy. Additionally, using data integrity constraints in the database helps maintain consistent and error-free entries, thereby ensuring that calculations are based on complete and correct data .

To calculate the average budget, sum the budgets of travelers per agency and divide by the number of travelers. This task, done in a single pass through the dataset, has a time complexity of O(n), where n is the total number of travelers. As agencies increase in number or traveler data scales, map-reduce techniques or batch processing can be used to split the task into manageable computations, improving both speed and reliability without impacting the system's overall performance .

Enhancing the design could involve implementing a multidimensional index structure, such as B-trees or hashmaps, keyed on frequently queried attributes like author or price range. This allows rapid lookups. Additionally, integrating a full-text search engine can expedite complex queries involving text fields or numeric ranges. Ensuring that the system supports reliable indexing and query optimization would also improve search efficiency for queried criteria .

When designing a query to filter laptops based on RAM size, considerations include efficiency and response time. Utilizing an indexed attribute for RAM size would allow for quick lookups. The query should handle edge cases such as identical RAM sizes efficiently through range filtering techniques. Additionally, ensuring that the system can dynamically handle increases in data without affecting performance is vital; this might involve using a database management system that supports scaling and load balancing to accommodate large volumes of data .

Challenges in tracking doctors with the most appointments include handling large datasets and ensuring real-time updates with minimal lag. Memory constraints can be addressed by using efficient data structures like hashmaps to keep a count of doctor appointments dynamically. Real-time performance can be enhanced using event-driven programming to update counts as appointments are booked or canceled. Furthermore, caching results for frequently accessed data can reduce repetitive computation, thereby addressing scalability and performance issues .

To determine the agency with the most travelers, iterating through each agency and maintaining a count of travelers for each is efficient. A simple max-comparison during iteration will identify the agency with the maximum count. This approach ensures that the solution is scalable and operates with a time complexity of O(n), where n is the total number of travelers across all agencies. This method is both straightforward and computationally inexpensive .

You might also like