0% found this document useful (0 votes)
3 views

Practice Questions for Mid

The document outlines the differences between classes, abstract classes, and interfaces in Object Oriented Programming, detailing their characteristics and usage. It includes practice questions for a midterm exam, focusing on creating classes, implementing interfaces, and managing tasks through abstract classes. Additionally, it provides implementation details for a hospital management system involving doctors and patients.
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)
3 views

Practice Questions for Mid

The document outlines the differences between classes, abstract classes, and interfaces in Object Oriented Programming, detailing their characteristics and usage. It includes practice questions for a midterm exam, focusing on creating classes, implementing interfaces, and managing tasks through abstract classes. Additionally, it provides implementation details for a hospital management system involving doctors and patients.
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/ 4

Object Oriented Programming Lab

Class Vs Abstract Class Vs Interface:

Class
class Animal {
void sound() {
 A regular blueprint to create objects.
System.out.println("Animal sound");
 Can have both properties and methods (with full implementation). }
 Can use inheritance (one class can extend another). }

Abstract Class

 A partially completed class.


 Can have both implemented and unimplemented (abstract) methods.
 Can't be instantiated directly.
 Used as a base class in inheritance. abstract class Animal {
abstract void sound(); // no body
Interface void sleep() {
System.out.println("Sleeping...");
}
 A completely abstract type }
 Contains only method signatures (by default).
 Used to define a contract for classes to follow.
 A class implements an interface (can implement multiple interfaces).

interface Animal {
void sound(); // no body
Relationship with Inheritance: }

Summary:
Practice Questions for Midterm

1.
1. Define an interface `Product` with methods `String getProductName()` and `void produce()`. [2]
2. Create a class `CentralLocking` implementing the `Product` interface: [3]
- Add a private property `String productName`
- Implement the constructor to initialize `productName`.
- Define `getProductName()` to return the product name.
- Define `produce()` to print "Producing Central Locking System".

3. Create a class `GearLocking` implementing the `Product` interface: [3]


- Add a private property `String productName`
- Implement the constructor to initialize `productName`.
- Define `getProductName()` to return the product name.
- Define `produce()` to print "Producing Gear Locking System".

4. Create an abstract class `Car`: [3]


- Add two protected properties: `Product product` and `String carType`.
- Create a constructor to initialize `product` and `carType`.
- Define a method `printDetails()` to print car type and product name.
- Add two abstract methods: `void assemble()` and `void produceProduct()`.

5. Create a concrete class `BMW` extending `Car`: [3]


- Implement `assemble()` to print: "Assembling [Central Locking System] for [BMW X5 Model]". The []
parts must be printed by calling the method of suitable variable.
- Implement `produceProduct()` to call `produce()` on the product and print: "Modifying product [Gear
Locking System] according to [BMW X5 Model]".

6. Create a concrete class `Mercedes` extending `Car`: [3]


- Implement `assemble()` to print: "Assembling [Central Locking System] for [Mercedes GLS Model]". -
Implement `produceProduct()` to call `produce()` on the product and print: "Modifying product [Gear
Locking System] according to [Mercedes GLS Model]"

7. Create a `Main` class: [3]


- Instantiate `CentralLocking` and `GearLocking` objects.
- Create `BMW` and `Mercedes` objects using respective products.
- Call `produceProduct()` and `assemble()` methods on both car objects
2.
Write a Java program to manage a New Year To-Do List by following the instructions bellow:
A. Create an abstract class Task:
→ Variables: taskName (String) and deadline (String), Status (String); all the variables must be
protected.
→ A constructor to initialize these variables, but Status will have default value as “Incomplete”
→ An abstract method perform() that specifies the tasks operation.
→ An abstract method getDetails() that provides the tasks details.
→ A non-abstract method displayDeadline() that prints task deadline only.
Example: “Task Deadline Month Name.”
→ Another non-abstract method displayStatus() that prints the tasks status
complete/incomplete with task name.
→ Example: “Learning OOP is Incomplete.”
B. Create concrete subclasses PersonalTask and ProfessionalTask that inherit from Task.
→ Class PersonalTask: Print a message about completing the task and include details like
taskName and deadline.
→ Class ProfessionalTask: Similar to PersonalTask, but emphasize its professional context.
→ Both class will override displayDeadline(), displayStatus() to format the deadline differently
for each type of task. DisplayDeadline(): “Skill development deadline August.” And
displayStatus(): “Skill development is incomplete.”
→ Both class will have a common method TaskComplete(), calling this method will change the
status of the corresponding task from incomplete to completed.

C. In the TestClass:
→ Create 2 object of PersonalTask and 2 objects of ProfessionalTask, initializing them with
appropriate values.
Example:
Personal Task : Losing 5kg, Ocotber.
Professional Task: Being pro in programming, December
→ Call all the methods using the objects created.
→ At the end update all the task to complete state and call displayStatus() to check the task
update.
3.
Implementation Details:
● Use an interface named Individual which contains the methods printDetails() and calculate(). 4
● Create an abstract class named Person which implements the Individual interface, has the
common properties name and age and has Constructors to initialize them. 4
● Create concrete classes Doctor and Patient that extend the Person abstract class,
implementing the additional methods and properties. 4
● Create another class named HospitalManagement that contains the main method. 1
● Take input from the user the details of the following three different doctors and initialize the
objects using constructors. 2
○ name="Dr. Smith", age=35, specialization="Cardiology", yearsExperience=8,
patientsTreated=600, visitAmount=1200, outdoorHours=20
○ name="Dr. Johnson", age=38, specialization="Neurology", yearsExperience=10,
patientsTreated=820, visitAmount=1400, outdoorHours=18
○ name="Dr. Green", age=42, specialization="Gynecology", yearsExperience=15,
patientsTreated=1300, visitAmount=1500, outdoorHours=20
● Take input from the user the details of four different patients and initialize the objects using
constructors. 2
○ name="Alice Wilstone", age=20, disease="Flu", visitAmount=400, testAmount=1000,
wasAdmitted=false
○ name="Bob Nielsen", age=35, disease="Diabetes", visitAmount=1200,
testAmount=8000, wasAdmitted=true
○ name="Jake Peralta", age=56, disease="Heart disease", visitAmount=2000,
testAmount=1200, wasAdmitted=true
○ name="Emma Stone", age=45, disease="Cancer", visitAmount=500,
testAmount=1500, wasAdmitted=true
● Calculate the amount of bonus Dr. Johnson should receive by invoking the appropriate 1
methods.
● Mr. Nielsen has visited the hospital and paid 2000 taka as doctors visit and spent 10000 taka for
the test. Update his total bill invoking appropriate methods. 1
● Calculate the percentage of discount Mr. Nielsen should receive by invoking the appropriate
Method. 1

You might also like