Array of Objects
Objectives
• Perform basic operations supported by an array, including adding, deleting, searching,
and updating objects.
Case Study
Problem:
• An antique shop needs to manage its inventory, which includes items like vases, statues,
and paintings.
Solution:
• Manage a list of objects (vases, statues, paintings) in an array.
Classes and Methods
1. Item Class:
o Fields: value (int), creator (String)
o Methods:
▪ Constructors
▪ Getters/Setters
▪ input(): For inputting fields using Scanner and handling exceptions.
▪ toString(): Returns a string with the item's details.
2. Vase Class (extends Item):
o Additional Fields: height (int), material (String)
o Methods:
▪ Constructors
▪ Getters/Setters
▪ input(): For inputting vase-specific fields.
▪ toString(): Returns a string with vase details.
3. Statue Class (extends Item):
o Additional Fields: weight (int), colour (String)
o Methods:
▪ Constructors
▪ Getters/Setters
▪ input(): For inputting statue-specific fields.
▪ toString(): Returns a string with statue details.
1
4. Painting Class (extends Item):
o Additional Fields: height (int), width (int), isWatercolour (boolean), isFramed
(boolean)
o Methods:
▪ Constructors
▪ Getters/Setters
▪ input(): For inputting painting-specific fields.
▪ toString(): Returns a string with painting details.
5. ItemList Class:
o Fields: list (Item array), numOfItem (int), MAX (final int)
o Methods:
▪ addItem(Item item): Adds an item to the list.
▪ displayAll(): Prints all items.
▪ findItem(String creator): Finds an item by creator.
▪ findItemIndex(String creator): Finds the index of an item by creator.
▪ updateItem(int index): Updates an item at a specified index.
▪ removeItem(int index): Removes an item at a specified index.
▪ displayItemsByType(String type): Displays items by type (Vase, Statue,
Painting).
▪ sortItems(): Sorts items in ascending order based on their value.
Main Class
antiqueShop:
• Main method to interact with the inventory, offering options to add, display, find, update,
remove, and sort items.
2
Questions and Answers
1. Q: What is the main objective of managing an array of objects in the antique shop case
study?
A: To perform basic operations like adding, deleting, searching, and updating objects in
the shop's inventory.
2. Q: What are the three types of items managed in the antique shop inventory?
A: Vases, statues, paintings.
3. Q: What fields are declared in the Item class?
A: value (int) and creator (String).
4. Q: What is the purpose of the input method in the Item class?
A: To input all fields of an Item object using the Scanner class and handle exceptions.
5. Q: What does the toString method in the Item class return?
A: A string that includes the value and creator of an Item object.
6. Q: What additional fields are present in the Vase class?
A: height (int) and material (String).
7. Q: What is the height range constraint for a vase in the Vase class?
A: The height must be between 0 and 2000.
8. Q: What additional fields are present in the Statue class?
A: weight (int) and colour (String).
9. Q: What is the weight range constraint for a statue in the Statue class?
A: The weight must be between 0 and 1000.
10. Q: What additional fields are present in the Painting class?
A: height (int), width (int), isWatercolour (boolean), and isFramed (boolean).
3
11. Q: What is the height range constraint for a painting in the Painting class?
A: The height must be between 0 and 2000.
12. Q: What is the width range constraint for a painting in the Painting class?
A: The width must be between 0 and 3000.
13. Q: What field in the Painting class indicates if the painting uses watercolor?
A: isWatercolour (boolean).
14. Q: What field in the Painting class indicates if the painting has a frame?
A: isFramed (boolean).
15. Q: What is the maximum number of items that can be stored in the ItemList class?
A: 100 items.
16. Q: What method in the ItemList class adds an item to the list?
A: addItem(Item item).
17. Q: What method in the ItemList class prints all items in the list?
A: displayAll().
18. Q: What method in the ItemList class finds an item by its creator?
A: findItem(String creator).
19. Q: What method in the ItemList class finds the zero-based index of the first occurrence
of an item by its creator?
A: findItemIndex(String creator).
20. Q: What method in the ItemList class updates an item at a specified index?
A: updateItem(int index).
21. Q: What method in the ItemList class removes an item at a specified index and shifts
subsequent elements to the left?
A: removeItem(int index).
4
22. Q: What method in the ItemList class prints out all items of a given type?
A: displayItemsByType(String type).
23. Q: What method in the ItemList class sorts items in ascending order based on their
value?
A: sortItems().
5
Please choose one correct answer.
1. Q: What type of data is stored in the creator field of the Item class?
o A: String
o B: int
o C: boolean
o D: double
2. Q: What method in the Item class is used to input fields using the Scanner class?
o A: input
o B: setFields
o C: initialize
o D: configure
3. Q: What additional field does the Vase class have that the Item class does not?
o A: height
o B: weight
o C: width
o D: depth
4. Q: What is the maximum height constraint for a vase in the Vase class?
o A: 2000
o B: 1500
o C: 1000
o D: 500
5. Q: What additional field does the Statue class have that the Item class does not?
o A: weight
o B: height
o C: material
o D: width
6. Q: What is the maximum weight constraint for a statue in the Statue class?
o A: 1000
o B: 2000
o C: 1500
o D: 500
7. Q: What boolean field in the Painting class indicates if the painting is a watercolor?
o A: isWatercolour
o B: isOilPainting
o C: isAcrylic
o D: isInk
8. Q: What boolean field in the Painting class indicates if the painting has a frame?
o A: isFramed
o B: isMounted
o C: isWrapped
o D: isCovered
6
9. Q: What is the maximum number of items that can be stored in the ItemList class?
o A: 100
o B: 200
o C: 50
o D: 150
10. Q: What method in the ItemList class returns a boolean indicating if an item was added?
o A: addItem
o B: insertItem
o C: pushItem
o D: appendItem
11. Q: What method in the ItemList class prints out all items?
o A: displayAll
o B: printAll
o C: showAll
o D: listAll
12. Q: What method in the ItemList class finds an item by its creator?
o A: findItem
o B: searchItem
o C: locateItem
o D: getItem
13. Q: What method in the ItemList class updates an item at a specified index?
o A: updateItem
o B: modifyItem
o C: changeItem
o D: editItem
14. Q: What method in the ItemList class removes an item at a specified index?
o A: removeItem
o B: deleteItem
o C: eraseItem
o D: clearItem
15. Q: What method in the ItemList class prints out all items of a given type?
o A: displayItemsByType
o B: listItemsByType
o C: showItemsByType
o D: printItemsByType
16. Q: What method in the ItemList class sorts items in ascending order based on their
value?
o A: sortItems
o B: orderItems
o C: arrangeItems
o D: rankItems
7
17. Q: What is the maximum width constraint for a painting in the Painting class?
o A: 3000
o B: 2000
o C: 1500
o D: 1000
18. Q: What method in the Item class is responsible for handling exceptions during input?
o A: input
o B: toString
o C: getValue
o D: setCreator
19. Q: In the ItemList class, what type of array is used to store items?
o A: Item[]
o B: Object[]
o C: String[]
o D: int[]