โฑ๏ธ Time: 2 Hours
๐ Maximum Marks: 50
๐ Instructions (Must Read Carefully):
- Attempt all questions.
- Each question must be implemented in separate classes/files.
- Follow proper Java naming conventions.
- Use Scanner class wherever user input is required.
- Output must be properly labeled and formatted.
- Write comments to explain important parts of your code.
๐น Section A โ Core Programming (30 Marks)
Q1. Class, Object & Constructor (10 Marks)
A cinema booking system needs to store movie details and display them to users.
๐ You are required to develop a Java program with the following steps:
- Create a class named
Movie. - Declare the following instance variables:
- movieId (int)
- movieName (String)
- ticketPrice (double)
- Create a parameterized constructor:
- It should accept values for all variables
- Assign values using the
thiskeyword
- Create a method named
displayMovieDetails():- This method should print:
- Movie ID
- Movie Name
- Ticket Price
- Output format should be clear, for example: Movie ID: 101
Movie Name: Avengers
Ticket Price: 250.0
- This method should print:
- In the
main()method:- Ask the user to enter details for at least 2 movies
- Create objects using constructor
- Call the display method for each object
Q2. Inheritance & Method Overriding (10 Marks)
A ride-sharing application calculates fare differently based on vehicle type.
๐ Write a Java program with the following requirements:
- Create a base class
Ride:- Method
calculateFare(double distance) - Default implementation should print:
"Fare calculation not defined"
- Method
- Create two subclasses:
BikeRideCarRide
- Override
calculateFare()in both classes:- BikeRide:
- Fare = distance ร โน5
- CarRide:
- Fare = distance ร โน10
- BikeRide:
- In the
main()method:- Ask user to enter distance
- Create objects of both subclasses
- Call the method and display calculated fare
- Ensure:
- Method overriding is clearly demonstrated
- Output shows which type of ride is being calculated
Q3. Encapsulation (10 Marks)
A library system needs to securely manage book borrowing records.
๐ Write a Java program with the following specifications:
- Create a class
LibraryAccount. - Declare private variables:
- accountId (int)
- userName (String)
- booksIssued (int)
- Provide:
- Getter and Setter methods
- Add validation in setter:
- booksIssued should not be negative
- If negative value is given, display
"Invalid number of books"
- Create methods:
issueBook()โ increase booksIssued by 1returnBook()โ decrease booksIssued by 1 (but not below 0)
- In the
main()method:- Create object
- Set initial values
- Perform multiple issue/return operations
- Display final account details
๐น Section B โ Advanced OOP Concepts (20 Marks)
Q4. Interface (10 Marks)
A payment gateway supports multiple payment options.
๐ Write a Java program as follows:
- Create an interface
PaymentMethod:- Method
makePayment(double amount)
- Method
- Implement this interface in:
DebitCardPaymentNetBankingPayment
- Define method behavior:
- DebitCard โ print
"Payment made using Debit Card: amount" - NetBanking โ print
"Payment made using Net Banking: amount"
- DebitCard โ print
- In the
main()method:- Use interface reference
- Create objects of both classes
- Call the method for both
- Ensure:
- Proper use of interface
- Clear output for both payment types
Q5. Runtime Polymorphism (10 Marks)
A food ordering app calculates total bill differently based on food type.
๐ Write a Java program with the following steps:
- Create a class
FoodOrder:- Method
calculateBill(double price) - Default: return same price
- Method
- Create subclasses:
VegOrderNonVegOrder
- Override the method:
- VegOrder โ add 5% tax
- NonVegOrder โ add 10% tax
- In the
main()method:- Use parent class reference
- Assign objects of both subclasses
- Call method and print final bill
- Ensure:
- Same method behaves differently (runtime polymorphism)
- Output clearly shows calculation
โญ Bonus Question (Optional โ 5 Marks)
Q6. Exception Handling with User Input
๐ Write a Java program to:
- Ask user to enter:
- Two integers
- Perform division: result = number1 / number2
- Handle exceptions:
- Division by zero โ
"Error: Cannot divide by zero" - Invalid input โ
"Error: Invalid input"
- Division by zero โ
- Use:
try-catchblocksfinallyblock to print: Program execution completed