0% found this document useful (0 votes)
6 views2 pages

Java Assignment-02

This document outlines an assignment for the Programming in Java course at Maharaja Agrasen Institute of Technology for the academic session 2024-2025. It includes questions on constructors, inner classes, OOP principles, and the design of a Ride-Booking System, requiring students to implement various programming concepts and demonstrate their understanding through code examples. The assignment emphasizes practical application of Java programming principles such as inheritance, encapsulation, polymorphism, and method dispatch.

Uploaded by

kartikchauhan190
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)
6 views2 pages

Java Assignment-02

This document outlines an assignment for the Programming in Java course at Maharaja Agrasen Institute of Technology for the academic session 2024-2025. It includes questions on constructors, inner classes, OOP principles, and the design of a Ride-Booking System, requiring students to implement various programming concepts and demonstrate their understanding through code examples. The assignment emphasizes practical application of Java programming principles such as inheritance, encapsulation, polymorphism, and method dispatch.

Uploaded by

kartikchauhan190
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/ 2

MAHARAJA AGRASEN INSTITUTE OF TECHNOLOGY

Department of Computer Science and Engineering


(Academic Session 2024-2025)

SUBJECT NAME: Programming in Java


(CIC-212)
ASSIGNMENT -02

Q.1(a) In Java, constructors cannot be inherited but can be invoked using super(). Explain
why constructors are not inherited in Java and how super() is used to invoke a superclass
constructor in a subclass. Provide a scenario where explicitly calling super() in a subclass
constructor is necessary.

Q.1(b) Implement a base class BankAccount with three constructors:

oA default constructor that initializes accountNumber to "000000",


holderName to "Unknown", and balance to 0.0.
o A parameterized constructor that initializes the account using values provided
by the user.
o A copy constructor that creates a new account by copying details from another
account.
• Also, Implement a derived class SavingsAccount that extends BankAccount.
The SavingsAccount class should include an interest rate and should use
constructor chaining to initialize the BankAccount fields.
• In the main() method, create an object using the copy constructor and display its
details.
Expected Output Example:

Account Details (Copied)


Account Number: 123456
Holder Name: John Doe
Balance: 5000.0
Interest Rate: 4.5%
(CO2)-5 Marks

Q.2 (a) Java supports inner classes (regular, static, local, and anonymous). Explain the key
differences between a local inner class and an anonymous inner class. In what situations
would an anonymous inner class be preferred over a regular inner class? Provide a real-world
use case where an anonymous inner class is the best choice.

Programming in Java (CIC -212)- Computer Science & Engineering Department


Q.2. (b) You are developing a Smart Home System where different devices (such as Light,
Fan, and AC) can be turned ON or OFF using a common interface.

1. Define an interface Device with a method turnOn().


2. Create an outer class SmartHome that contains:
o A non-static inner class Light that implements Device.
o The turnOn() method should print "Light is now ON".
3. Inside main(), demonstrate the use of both:
o A regular inner class instance to turn on a Light.
o An anonymous inner class to turn on a Fan (without creating a separate class).

(CO2)- 5 Marks

Q.3 (a) Suppose you are designing a software system for a bank. Which OOP principles
would you implement to ensure security and modularity? Justify your answer with examples.

(CO2)- 5 Marks
Q.4. You are designing a Ride-Booking System where different types of rides (CarRide,
BikeRide, LuxuryRide) follow a common interface but charge different fares.

1. Create an abstract class Ride that:


o Has a protected attribute baseFare (to ensure encapsulation).
o Defines an abstract method calculateFare(int distance).
2. Create three subclasses (CarRide, BikeRide, LuxuryRide) that:
o Implement calculateFare() differently based on the ride type.
o Encapsulate ride-specific fare calculation (e.g., Car: ₹10/km, Bike: ₹5/km,
Luxury: ₹20/km).
3. Demonstrate polymorphism by:
o Creating an array of Ride objects.
o Calling calculateFare() dynamically based on the ride type.

(CO2)- 5 Marks

Q.5 Design and implement a Java program that demonstrates both dynamic method dispatch
and Early Binding (Static method dispatch).
(CO2)- 5 Marks

Programming in Java (CIC -212)- Computer Science & Engineering Department

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy