๐Ÿงพ Java OOP Practical Exam โ€“ Set 6

โฑ๏ธ 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:

  1. Create a class named Movie.
  2. Declare the following instance variables:
    • movieId (int)
    • movieName (String)
    • ticketPrice (double)
  3. Create a parameterized constructor:
    • It should accept values for all variables
    • Assign values using the this keyword
  4. 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
  5. 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:

  1. Create a base class Ride:
    • Method calculateFare(double distance)
    • Default implementation should print: "Fare calculation not defined"
  2. Create two subclasses:
    • BikeRide
    • CarRide
  3. Override calculateFare() in both classes:
    • BikeRide:
      • Fare = distance ร— โ‚น5
    • CarRide:
      • Fare = distance ร— โ‚น10
  4. In the main() method:
    • Ask user to enter distance
    • Create objects of both subclasses
    • Call the method and display calculated fare
  5. 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:

  1. Create a class LibraryAccount.
  2. Declare private variables:
    • accountId (int)
    • userName (String)
    • booksIssued (int)
  3. Provide:
    • Getter and Setter methods
  4. Add validation in setter:
    • booksIssued should not be negative
    • If negative value is given, display "Invalid number of books"
  5. Create methods:
    • issueBook() โ†’ increase booksIssued by 1
    • returnBook() โ†’ decrease booksIssued by 1 (but not below 0)
  6. 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:

  1. Create an interface PaymentMethod:
    • Method makePayment(double amount)
  2. Implement this interface in:
    • DebitCardPayment
    • NetBankingPayment
  3. Define method behavior:
    • DebitCard โ†’ print "Payment made using Debit Card: amount"
    • NetBanking โ†’ print "Payment made using Net Banking: amount"
  4. In the main() method:
    • Use interface reference
    • Create objects of both classes
    • Call the method for both
  5. 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:

  1. Create a class FoodOrder:
    • Method calculateBill(double price)
    • Default: return same price
  2. Create subclasses:
    • VegOrder
    • NonVegOrder
  3. Override the method:
    • VegOrder โ†’ add 5% tax
    • NonVegOrder โ†’ add 10% tax
  4. In the main() method:
    • Use parent class reference
    • Assign objects of both subclasses
    • Call method and print final bill
  5. 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:

  1. Ask user to enter:
    • Two integers
  2. Perform division: result = number1 / number2
  3. Handle exceptions:
    • Division by zero โ†’ "Error: Cannot divide by zero"
    • Invalid input โ†’ "Error: Invalid input"
  4. Use:
    • try-catch blocks
    • finally block to print: Program execution completed

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *