1. Lets say we were designing a program for managing a store's inventory. The store sells a wide variety of products, but it needs all of them to share some similar functionality (such as getting and setting the price of each product). We decide that our program will use a class called "Inventoryltem" as the base class of our program, which allows each individual product to derive from that base class's methods. Should Inventoryltem be a concrete class or an abstract class? Give some reasons why you think so. 2. Following from the last question, lets say our products use bar codes so they can be easily scanned. We decide to implement the bar code functionality using an interface. Why might we choose to do that? What advantages does using an interface give us over directly programming them into the base class? 3. In your own words, say what you think the differences are between concrete classes, abstract classes, and interfaces. Can you give some examples of when you should use each of them?
Java
Algorithm for Product Class:
Class: Product
Attributes:
- name (String)
- price (double)
Constructor:
- Product(name: String, price: double)
Method: displayInfo()
Display "Product: " followed by the name attribute
Display "Price: $" followed by the price attribute
Algorithm for Shape Class:
Class: Shape (Abstract)
Attributes:
- color (String)
Constructor:
- Shape(color: String)
Abstract Method: calculateArea() (To be implemented by subclasses)
Method: displayInfo()
Display "Color: " followed by the color attribute
Algorithm for Drawable Interface:
Interface: Drawable
Abstract Method: draw() (To be implemented by classes that implement this interface)
Algorithm for Circle Class:
Class: Circle (Extends Shape, Implements Drawable)
Attributes:
- color (String)
- radius (double)
Constructor:
- Circle(color: String, radius: double)
Method: calculateArea()
Calculate and return the area of the circle using the formula: π * radius * radius
Method: draw()
Display "Drawing a circle"
Algorithm for Rectangle Class:
Class: Rectangle (Extends Shape, Implements Drawable)
Attributes:
- color (String)
- width (double)
- height (double)
Constructor:
- Rectangle(color: String, width: double, height: double)
Method: calculateArea()
Calculate and return the area of the rectangle using the formula: width * height
Method: draw()
Display "Drawing a rectangle"
Step by step
Solved in 4 steps with 7 images