0% found this document useful (0 votes)
11 views11 pages

Java Appliance Management System

The document outlines a series of programming tasks focused on object-oriented design principles such as abstraction, inheritance, interfaces, and adapters in Java. Each task involves creating classes and methods to manage appliances, users, books, media content, and tasks, incorporating features like electricity usage calculations, user limits, and event handling in GUI applications. The tasks are structured to build upon each other, emphasizing the use of abstract classes, final attributes, and interface implementations.

Uploaded by

peachypaimon
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)
11 views11 pages

Java Appliance Management System

The document outlines a series of programming tasks focused on object-oriented design principles such as abstraction, inheritance, interfaces, and adapters in Java. Each task involves creating classes and methods to manage appliances, users, books, media content, and tasks, incorporating features like electricity usage calculations, user limits, and event handling in GUI applications. The tasks are structured to build upon each other, emphasizing the use of abstract classes, final attributes, and interface implementations.

Uploaded by

peachypaimon
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

Question 1, 2 and 3 are connected

1. (Abstract)
Create an abstract class called Appliance with an attribute brand and an abstract method
calculateElectricityUsage(). Then create two classes, WashingMachine and Refrigerator, that
extend Appliance with the attribute of powerUsagePerHour. Implement the
calculateElectricityUsage() method in each class. calculateElectricityUsage() function of
washing machine should be multipled of powerUsagePerHour by 2, which means 2 hours of
usage, on the other hand assume that refrigerator will be used 24 hours. The main is given and
output should look like below.

2. (Abstract)
Create a class called SmartAppliance that extends the Appliance class. This class should
include an additional attribute, a List<String> called smartFeatures, which will hold the
names of the smart features. The SmartAppliance class should provide a method called
addSmartFeature that takes a String feature and a double additionalUsage as parameters. This
method should add the new feature to the smartFeatures list and also increase the
powerUsagePerHour by the additionalUsage amount corresponding to the smart feature.
Finally, update the calculateElectricityUsage method to return the total electricity usage
considering the current power usage for 2 hours of operation. The main is given and output
should look like below.
3. (Abstract)
Create a class called ApplianceManager that is responsible for managing multiple appliances
derived from the Appliance class. This class should contain a List<Appliance> to store the
appliances being managed. You will need to implement the method addAppliance(Appliance
appliance), which takes an Appliance object as a parameter and adds it to the list of
appliances. Additionally, implement the method calculateTotalElectricityUsage(), which
should iterate through the list of appliances and sum their electricity usage by invoking their
respective calculateElectricityUsage() methods. Finally, this method should return the total
electricity usage of all the appliances managed by the ApplianceManager. The main is given
and output should look like below.

4. (Final keyword)
Create a class named Configuration that contains a final attribute MAX_USERS. This
attribute will hold the maximum number of users allowed in the system, and it should be
declared as a public static final integer. The final keyword indicates that this value cannot be
changed once it is initialized. Next, create a class called UserManagementSystem. This class
should maintain the current number of users and provide a method to add new users. The
addUser method should first check if the current number of users is less than the
MAX_USERS value from the Configuration class. If it is, the method should increase the
count of current users by one and print a message indicating that a user has been added, along
with the new total number of users. If adding a user would exceed the MAX_USERS limit,
the method should print a message stating that the maximum limit has been reached. The
main is given and output should look like below.
5. (Final keyword)
Create a class named LibraryConfig that includes a final attribute MAX_BOOKS set to 4,
representing the maximum number of books that can be added to the library. This attribute
should be declared as public static final int. Create a class called BookManagementSystem
that manages a collection of book titles using a List<String>. This class should have a method
addBook(String title) that checks whether the current number of books is less than
MAX_BOOKS. If the current size of the book list is below the limit, the method should add
the book title to the list and print a message indicating that the book has been successfully
added (e.g., "The Great Gatsby has been added to the library."). However, if the maximum
limit has been reached (4 books), the method should print a message stating that no more
books can be added (e.g., "Cannot add more books, the maximum limit has been reached.").
The main is given and output should look like below.

6. (Superclasses)
Create a superclass named Device that has a String attribute called model to represent the
device's model name. This class should also include a method turnOn() that prints a generic
message indicating that the device is turning on. Next, extend this class with two subclasses:
Smartphone and Laptop. In each subclass, override the turnOn() method to provide a specific
message for each type of device. The main is given and output should look like below.
7. (Superclasses)
Create a superclass named Product that has attributes for productName, price, and quantity.
Include a method called displayProductInfo() that prints the product’s details. Next, create
two subclasses: Electronics and Clothing. The Electronics class should have an additional
attribute for warrantyPeriod, while the Clothing class should have an attribute for size.
Override the displayProductInfo() method in both subclasses to include specific details related
to each type of product. The main is given and output should look like below.

8. (Interface)
Create an interface named Storable with a method storeItem(). Then create two classes:
Warehouse and Store, both of which implement the Storable interface. The Warehouse class
should have an attribute for capacity and implement the storeItem() method to indicate how
many items can be stored. The Store class should have an attribute for location and implement
the storeItem() method to indicate that an item is being displayed. The main is given and
output should look like below.
9. (Interface)
Create an interface called Payable that declares a method processPayment(). Then create two
classes: CreditCard and PayPal, both of which implement the Payable interface. The
CreditCard class should have attributes for cardNumber and cardHolder, while the PayPal
class should have an attribute for email. Implement the processPayment() method in each
class to display a message indicating the payment method being used. The main is given and
output should look like below.

10. (Interface)
Create an interface called Notifiable that declares a method sendNotification(). Then create
two classes: EmailNotification and SMSNotification, both of which implement the Notifiable
interface. The EmailNotification class should have an attribute for emailAddress, while the
SMSNotification class should have an attribute for phoneNumber. Implement the
sendNotification() method in each class to display a message indicating the type of
notification being sent along with the relevant details. The main is given and output should
look like below.
11. (Adapter)
Create a LegacyPrinter class that has an attribute for the printer's name and a method
printDocument() for printing documents. The new printing system requires a method called
print(), defined by a NewPrinter interface. Your task is to implement an adapter class called
PrinterAdapter that will make the LegacyPrinter compatible with the new system by
implementing the NewPrinter interface. The main is given and output should look like below.

12. (Adapter)
Create a class named LegacyAudioPlayer that includes an attribute for the audio file name
and a method called playAudio() to play the audio file. In addition to this method, the
LegacyAudioPlayer should have extra methods such as stopAudio() and pauseAudio(), which
will not be utilized in the context of the new system. Next, create an interface named
ModernAudioPlayer that defines three methods: play(), stop(), and pause(). Your task is to
implement an adapter class called AudioPlayerAdapter that adapts the LegacyAudioPlayer to
the new system by implementing the ModernAudioPlayer interface. In the adapter class,
implement the play() method to call the playAudio() method of LegacyAudioPlayer, but leave
the stop() and pause() methods empty to illustrate that they are not needed for this specific
[Link] main is given and output should look like below.
13. (Adapter)
Create a class named LegacyPaymentProcessor that includes an attribute for the payment
amount and a method called processPayment() to handle the payment processing.
Additionally, the LegacyPaymentProcessor should have extra methods such as
refundPayment() and cancelPayment(), which will not be utilized in the context of the new
system. Next, create an interface named PaymentGateway that defines three methods:
makePayment(), refund(), and cancel(). Your task is to implement an adapter class called
PaymentAdapter that adapts the LegacyPaymentProcessor to the new system by
implementing the PaymentGateway interface. In the adapter class, implement the
makePayment() method to call the processPayment() method of LegacyPaymentProcessor,
but leave the refund() and cancel() methods empty to illustrate that they are not needed for
this specific functionality. The main is given and output should look like below.

Question 14 and 15 are connected


14. (Inheritance)
Create a superclass called MediaContent with attributes for title and duration. Then, create
two subclasses: Video and Audio, each containing additional attributes specific to their types
(e.g., resolution for Video and bitrate for Audio). The main is given and output should look
like below.
15. (instanceof keyword)
In a class called ContentManager, implement a method named
displayContentDetails(MediaContent content) that uses the instanceof operator to check if the
content is a Video or Audio. The method should print detailed information about the content
based on its type. The main is given and output should look like below.

16. (Key event)


Create a Java Swing application that contains a text field. When the user types a character in
the text field, display that character in a label below the text field. Additionally, if the user
presses the Enter key, display a message saying "You pressed Enter!" in the label. The main is
given and output should look like below.
17. (Key event)
Create a simple Java Swing application with a label that listens to keyboard input. When the
user presses the 'A' key on the keyboard, the label should display "A". If the user presses any
other key, the label should display "X". The main is given and output should look like below.

Pressed “A” Pressed “B” Pressed “P”

Question 18 and 19 are connected


18. (Key event and Inheritance)
Create a Java Swing application with a superclass called BaseWindow that initializes a text
field and a label. The text field should allow the user to type text, and the label should display
a default message. The main is given and output should look like below.
19. (Key event and Inheritance)
Extend the BaseWindow class by creating a subclass called KeyPressWindow that
implements the KeyListener interface. In this subclass, override the keyPressed method to
update the label with the key that was pressed. Make sure to create an instance of
KeyPressWindow in the main method. The main is given and output should look like below.

20. (Class)
Create a task management system by implementing a Task class and a TaskManager class.
The Task class should include attributes for title, description, and a boolean isCompleted to
track the task's status. It should have methods to mark the task as completed completeTask()
and to display the task's details displayTask(), which shows the title, description, and whether
it is completed. The TaskManager class will maintain a list of Task objects and should
include methods to add a new task (addTask(Task task)), display all tasks displayAllTasks(),
and display only incomplete tasks displayIncompleteTasks(). The main is given and output
should look like below.

Common questions

Powered by AI

The implementation of an abstract class and its derived classes supports polymorphism by allowing different classes to be treated as instances of the abstract class. This is achieved by defining a common interface or method signature in the abstract class, which must be implemented by the derived classes. In the given document, the Appliance class serves as the abstract base class with a method calculateElectricityUsage(), which is then implemented differently in the WashingMachine and Refrigerator classes. This allows for polymorphic behavior where the same method call can result in different executions based on the specific subclass. Moreover, code extensibility is supported as new types of appliances can be easily integrated into the system by simply extending the base class and providing specific implementations of the abstract methods .

Extending an abstract class with additional attributes and methods, evident in the SmartAppliance class, demonstrates class inheritance by building upon the base structure of the abstract class, Appliance. Inheritance allows SmartAppliance to inherit the attributes and methods of Appliance, while also adding new functionality through the smartFeatures attribute and addSmartFeature method. This mechanism exemplifies how new subclasses can be created with enhanced behaviors and attributes tailored to specific requirements, promoting code reusability and efficiency by leveraging existing code structures and extending them with specialized logic .

User interface event handling, as implemented in the KeyPressWindow class, is crucial for enhancing user experience by providing responsive and interactive software that reacts to user inputs in real time. By implementing the KeyListener interface and overriding the keyPressed method, the class updates the user interface with bespoke responses to key events, like displaying the specific key pressed. This creates a dynamic interaction that makes the software feel more intuitive and engaging for users, as their actions are immediately acknowledged and reflected in the display, thus improving the overall usability and satisfaction .

Implementing interfaces in Java, such as the Storable interface, enhances flexibility by allowing different classes to agree on a method signature without enforcing a class hierarchy. This means multiple classes can implement the same interface and thus guarantee the presence of certain methods, like storeItem(), while freely structuring their internal implementations differently. It also supports modularity because interfaces define a clear contract that implementing classes must adhere to, allowing parts of the system to be developed, tested, and upgraded independently, provided they fulfill the interface contract. This is particularly useful in a diverse system where multiple types of storage facilities (e.g., Warehouse and Store) can perform different operations while using a consistent interface .

The adapter design pattern facilitates integration between old and new systems by allowing a class with a specific interface to be used as if it were a different interface without modifying its code. In the case of the PrinterAdapter class, it acts as a bridge between the LegacyPrinter's printDocument() method and the NewPrinter interface's print() method. This allows the existing LegacyPrinter to work with the new system's requirements without altering its original code structure. The adapter encapsulates the necessary transformations, enabling seamless interaction between components with incompatible interfaces, thus preserving legacy code while integrating newer systems .

Using the 'instanceof' keyword for type checking, as implemented in the ContentManager class, provides the benefit of allowing developers to safely determine an object's type at runtime, enabling them to execute context-specific logic depending on the subclass type, such as Video or Audio. This can simplify the code, as it avoids the need for duplicative type-specific methods. However, it also has limitations, such as hindering polymorphic design principles because it requires knowledge of specific subtypes within the method logic, potentially leading to code that is less flexible and harder to maintain. It can also introduce performance overhead due to type checks at execution time .

Maintaining a collection of instances within a managing class, as implemented in ApplianceManager, supports system scalability and maintainability by centralizing control and coordination of objects. The ApplianceManager class, with its list of Appliance objects and methods like addAppliance and calculateTotalElectricityUsage, simplifies the management of multiple component objects, allowing for easy addition, removal, and iteration over appliances. This architecture improves scalability, as new appliances can be integrated without modifying existing logic. Additionally, it enhances maintainability by encapsulating the aggregation logic, thus minimizing the impact of changes and reducing complexity outside the manager class .

Encapsulating task details within a Task class improves code organization and readability by centralizing the management of task-related data and logic into a single, cohesive unit. This encapsulation ensures that task attributes such as title, description, and completion status are consistently managed and easily accessed through defined methods like completeTask() and displayTask(). It also reduces complexity by separating concerns, enabling developers to interact with well-defined interfaces without needing to understand the underlying implementation details, thus promoting clearer, modular, and more maintainable code. Additionally, it highlights the concept of information hiding, which shields the internal state from outside interference .

The use of the 'final' keyword when defining constants like MAX_USERS in a class offers several advantages, notably ensuring that the constant's value cannot be altered once it's set. This guarantees the integrity of limit values that are critical to system logic, such as user limits, by preventing accidental or unauthorized changes. It also clarifies the code by signaling to other developers that MAX_USERS is a constant and intended to remain unchanged, thus enhancing readability and maintainability. Additionally, 'final' variables can improve performance since they allow for potential optimizations at compile time .

Overriding methods in derived classes supports polymorphism by allowing subclasses to provide specific implementations of methods defined in a superclass. This principle is evident in the Device superclass, where subclasses like Smartphone and Laptop override the turnOn() method. This enables a common method call to elicit different behaviors depending on the object instance at runtime. Polymorphism through method overriding facilitates more dynamic and flexible code, as developers can write more general code that works with parent class references, yet results in behavior that is specific to the actual subclass object, thus enhancing extensibility and maintainability of systems .

You might also like