Exercise 4.1 Given the shopping list of groceries and the UML diagram (Figure 4.1), develop the following methods:
isInList, which determines whether a given item is already on the list;
countItems, which counts how many items are on the shopping list;
removeFromList, which produces a new list with the given item removed;
addToList, which adds a new item to the shopping list.
Exercise 4.2 Develop the collection of classes for representing a list of actors in a cast. We assume, each actor is listed only once. It is sufficient to just record actor's full name as one String: the last name followed by the first name.
Develop the method isInCast, which determines whether some actor is in the cast.
Develop the method castSize, which determines how many actors are in the cast.
Develop the method sortList, which return a sorted list of actors.
Develop the method sortedIsInCast, which determines whether some actor is in the cast, and takes advantage of the fact that the list is already sorted.
Exercise 4.3 The playbill is a list of actor-role pairs, each actor and each role can be represented as one String. Every actor plays only one role.
Develop the collection of classes to represent a playbill for a play.
Develop the method whosePart, which determines the actor playing the given role.
Develop the method whatPart, which determines the role played by a given actor
Develop the method substitute. The method consumes an actor and a role. It produces a new playbill where the given role is played by the given actor. If the given role does not appear in the playbill, the playbill is unchanged.
Exercise 4.4 The course information in the registrar's database contains the course number (four digits), the course title, and the number of credits.
Develop the classes to represent a list of courses.
Develop the method departmentList, which produces a list of all courses offered in the given department. A department is identified by the first two digits of the course number.
Develop the method credits, which determines the number of credits student earns in a given course.
Exercise 4.5 A town soccer team has a phone tree to notify players about game cancellations and other changes in the schedule. Each player is assigned to call at most two other players.
Develop the classes to represent the phone tree, recording each player's name and phone number.
Develop the method phoneNumber, which returns the phone number of a given player.
Develop the method atFault, which returns the list containing the names of all players in the tree that are in the calling chain leading to the given player.
Develop the method myPhoneTree, which returns the phone subtree starting with the given player.
Develop the method countPlayers, which counts how many players are in the phone tree.
Develop the method checkList, which determines whether every player in a given list of players also appears in the given phone tree.
Exercise 4.6 Design the class Item that contains some attribute that defines an ordering of Item objects.
Develop the classes to represent a binary search tree of Items.
Develop the method insert, which inserts a new Item into the BST.
Develop the method inBST, which determines whether an Item with the given attribute appears in the BST.
Develop the method find, which produces the Item with the given attribute in the BST, or null object if not found.
Exercise 4.7 The grocery store keeps the inventory as a list of lists of grocery items of different kind (Coffee, IceCream, Juice - see an earlier exercise).
Develop the classes to represent the inventory for this grocery store.
Develop the method grossIncome, which computes the gross income from all products in the inventory.
Develop the method incomeForClass, which determines the gross income for a given class of products, given the class name for the products. Use item.getClass().getName() to retrieve a String that is the name of the class. Your method should consume a similar String.
Develop the method incomeForBrand, which determines the gross income for all items in the given class and with the given brand name.
Draw the UML diagram for this collection of classes.