1. Listed next is the skeleton for a class named InventoryItem. Each inventory item has a name and a unique ID number: class InventoryItem private String name; private int uniqueItemID; } Flesh out the class with appropriate accessors, constructors, and mutators. The uniqueItemID 's are assigned by your store and can be set from outside the InventoryItem class-your code does not have to ensure that they are unique. Next, modify the class so that it implements the Comparable interface. The compareTo() method should compare the uniqueItemID 'S; e.g., the InventoryItem with item ID 5 is less than the InventoryItem with ID 10. Test your class by creating an array of sample InventoryItem 's and sort them using a sorting method that takes as input an array of type Comparable.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 5RQ
icon
Related questions
Question

I need some help with this coding problem

1. Listed next is the skeleton for a class named InventoryItem. Each inventory item has a name and a
unique ID number:
class InventoryItem
private String name;
private int uniqueItemID;
Flesh out the class with appropriate accessors, constructors, and mutators. The uniqueItemID 's are
assigned by your store and can be set from outside the InventoryItem class-your code does not have
to ensure that they are unique. Next, modify the class so that it implements the Comparable interface.
The compareTo() method should compare the uniqueItemID 's; e.g., the InventoryItem with item ID 5 is
less than the InventoryItem with ID 10. Test your class by creating an array of sample InventoryItem 's
and sort them using a sorting method that takes as input an array of type Comparable.
2. Define an interface named Shape with a single method named area that calculates the area of the
geometric shape:
public double area();
Next, define a class named Circle that implements Shape . The Circle class should have an instance
variable for the radius, a constructor that sets the radius, accessor/ mutator methods for the radius,
and an implementation of the area method. Also define a class named Rectangle that implements
Shape. The Rectangle class should have instance variables for the height and width, a constructor that
sets the height and width, accessor and mutator methods for the height and width, and an
implementation of the area method.
The following test code should then output the area of the Circle and Rectangle objects:
public static void main(String[] args)
{
Circle c = new Circle(4); // Radius of 4
Rectangle r = new Rectangle(4,3); // Height
showArea (c);
showArea (r);
4, width = 3
}
public static void showArea (Shape s)
double area = s.area();
System.out.println("The area of the shape is
area);
+
Transcribed Image Text:1. Listed next is the skeleton for a class named InventoryItem. Each inventory item has a name and a unique ID number: class InventoryItem private String name; private int uniqueItemID; Flesh out the class with appropriate accessors, constructors, and mutators. The uniqueItemID 's are assigned by your store and can be set from outside the InventoryItem class-your code does not have to ensure that they are unique. Next, modify the class so that it implements the Comparable interface. The compareTo() method should compare the uniqueItemID 's; e.g., the InventoryItem with item ID 5 is less than the InventoryItem with ID 10. Test your class by creating an array of sample InventoryItem 's and sort them using a sorting method that takes as input an array of type Comparable. 2. Define an interface named Shape with a single method named area that calculates the area of the geometric shape: public double area(); Next, define a class named Circle that implements Shape . The Circle class should have an instance variable for the radius, a constructor that sets the radius, accessor/ mutator methods for the radius, and an implementation of the area method. Also define a class named Rectangle that implements Shape. The Rectangle class should have instance variables for the height and width, a constructor that sets the height and width, accessor and mutator methods for the height and width, and an implementation of the area method. The following test code should then output the area of the Circle and Rectangle objects: public static void main(String[] args) { Circle c = new Circle(4); // Radius of 4 Rectangle r = new Rectangle(4,3); // Height showArea (c); showArea (r); 4, width = 3 } public static void showArea (Shape s) double area = s.area(); System.out.println("The area of the shape is area); +
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT