โฑ๏ธ Time: 2 Hours
๐ Maximum Marks: 50
๐ Instructions (Read Carefully):
- Attempt all questions.
- Write each program in a separate class/file.
- Use proper OOP concepts and naming conventions.
- Use Scanner class for user input wherever required.
- Display output in a clear and labeled format.
- Add comments to explain important parts of your code.
๐น Section A โ Core Programming (30 Marks)
Q1. Class, Object & Constructor (10 Marks)
A gym management system needs to store and display details of members.
๐ Write a Java program with the following requirements:
- Create a class named
GymMember. - Declare the following instance variables:
- memberId (int)
- memberName (String)
- membershipType (String)
- monthlyFee (double)
- Create a parameterized constructor:
- It should accept values for all data members
- Initialize them using the
thiskeyword
- Create a method
displayMemberDetails():- Display all member details in proper format
- Example:
Member ID: 1
Name: Rahul
Membership: Gold
Monthly Fee: 2000.0
- In the
main()method:- Ask the user to enter details for 2 members
- Create objects using constructor
- Display details of both members
Q2. Inheritance & Method Overriding (10 Marks)
A delivery company calculates shipping cost based on package type.
๐ Write a Java program following these steps:
- Create a base class
Shipment:- Method
calculateCost(double weight) - Default message:
"Cost calculation not defined"
- Method
- Create subclasses:
StandardShipmentExpressShipment
- Override the method:
- Standard โ โน20 per kg
- Express โ โน40 per kg
- In the
main()method:- Ask user to enter package weight
- Create objects of both subclasses
- Calculate and display cost
- Output must clearly show:
- Shipment type
- Weight
- Total cost
Q3. Encapsulation (10 Marks)
An online wallet system needs to securely manage user balance.
๐ Write a Java program with the following specifications:
- Create a class
Wallet. - Declare private variables:
- walletId (int)
- userName (String)
- balance (double)
- Provide:
- Getter and Setter methods
- Add validation:
- Balance should not be negative
- If invalid, display
"Invalid balance amount"
- Create methods:
addMoney(double amount)โ increase balancespendMoney(double amount)โ decrease balance if sufficient
- If balance is insufficient:
- Display
"Insufficient funds"
- Display
- In the
main()method:- Create object
- Set initial values
- Perform add and spend operations
- Display final wallet details
๐น Section B โ Advanced OOP Concepts (20 Marks)
Q4. Interface (10 Marks)
A multimedia application plays different types of media files.
๐ Write a Java program as follows:
- Create an interface
MediaPlayer:- Method
play() - Method
stop()
- Method
- Implement the interface in:
AudioPlayerVideoPlayer
- Define behavior:
- Audio โ
"Playing audio"/"Stopping audio" - Video โ
"Playing video"/"Stopping video"
- Audio โ
- In the
main()method:- Use interface reference
- Create objects of both classes
- Call methods
- Ensure:
- Proper interface implementation
- Clear output messages
Q5. Runtime Polymorphism (10 Marks)
A billing system calculates discount based on customer type.
๐ Write a Java program with the following steps:
- Create a class
Customer:- Method
getDiscount(double amount) - Default: return 0
- Method
- Create subclasses:
RegularCustomerPremiumCustomer
- Override method:
- Regular โ 5% discount
- Premium โ 15% discount
- In the
main()method:- Use parent class reference
- Assign subclass objects
- Calculate and display final amount
- Output must show:
- Customer type
- Original amount
- Discount applied
- Final amount
โญ Bonus Question (Optional โ 5 Marks)
Q6. Exception Handling with Validation
๐ Write a Java program to:
- Ask user to enter:
- Age
- Validate:
- Age must be greater than 0
- If invalid:
- Throw exception and display
"Invalid age entered"
- Throw exception and display
- Use:
try-catchthrowkeyword
- Finally block should print: