1
answer
0
watching
131
views
12 Jun 2018

Section 3 - Programming Questions 7. Role playing video games (RPGs) form a loosely defined genre of video games in which the player controls a small number of game characters. These characters form a group of adventurers whose purpose is to fulfill one or many quests. A common feature of RPGs is that the group of adventurers can collect various items and keep them for future me, sach items are added to the player's inventory. In this question, you will write classes which could be used to manage a player's inventory in an RPG. Part 1 Write a class called Item. An Item object represents an entire category of items carried by a group of adventurers, and each Item object has the following attributes: • The description of the items in this category (a String) The quantity of tems in this category that the group of adventurers is currently carrying can int). For example, suppose the group of adventurers is carrying 50 Heal Potions and 10 Life Potions. The SO Heal Pations would he represented by a single Item object, the value of the description attribute of this item object would be the string "Heal Potion, and the value of the quantity attribute of this Item object woukl be 50. The 10 Life Potions would also be represented by a single Item object, which is different from the Ttem object representing the Heal Potions. The value of the description attribute of this second Ttem object would he Life Potion", and the value of the quantity attribute of this second liem object would be 10. The Item class MUST provide the following INSTANCE methods, note that in the method descrip tions below, the phrase "this Item" means the item on which the method in question is called • A constructor, which takes as its only parameter a string representing the description attribute of the newly created Item. This constructor sets the quantity attribute of the newly created Item to 1. • A method called get Description(), which takes no parameters and returns a string representing the value of the description attribute of this Item • A method called getQuantity O. which takes no parameters and returns an int represent ing the value of the quantity attribute of this Item • A method called set Quantity), which takes a value of type int as its only parameter and returns nothing. This method changes the value of the quantity attribute of this Item so that its new value is equal to the int value this method accepts as parameter, this happens regardless of what the value of the int parameter is, even if it represents a sensical value Note that you MUST respect proper encapsulation practices that is, the attributes and methods of your class MUST he declared using the proper visibility modifiers. Part 2 Write a class called Inventory, which represents the inventory of a group of adventures. An Inventory object has only one attributes a collection of items, which specifies all the types of items that a group of adventurers is currently carrying along with the mumber of items of each type that the group is carrying Inventory objects MUST keep track of the items being carried by adventurers using a PLAIN ARRAY of Items, you MUST NOT se ArrayLists (or any other class which is part of the the Java Collection Framework such as Linkedin.ist) within the Inventory class under ANY circumstances. Each position in the array contains cither • The address of an Item object in memory, an Item is considered to be in the Inventory if and only if its address in memory is stored in the array . null Fach Item object in the inventory MUST appear exactly once in this array, in other words, there MUST NOT be any duplicate elements in the array under ANY circumstances, whether these dupli cates are deep copies or aliases The Inventory class MUST provide the following INSTANCE methods, note that an actual im plementation used in an RPG would provide additional methods, in particular methods to add an item to the Inventory. Also note that in the method descriptions below, the phrase "this inventory" means "the Inventory on which the method in question is called • A constructor, which takes a value of type int as its only parameter, and initializes the newly created Inventory so that the number of item types it can store is equal to the value of the parameter. The number of item types which can be stored in the Inventory is fixed during instantiation, and can never change during the lifetime of the Inventory object • A method called get.Capacity (), which takes no parameters and returns a value of type int representing the capacity of this Inventory (that is, the number of different item types which can be stored in this Inventory). • A method called getItem(), which takes a value of type int as its only parameter, and returns the Item Nered at the commenpanding position in the array holding the Items stond in this Inventory, this method returns null if the element at the position specified by the parameter value is null or if the parameter represents an invalid index in this array. • A method called consumeOne(), which takes as its only parameter a string represent ing the description of an item, and returns a value of type boolean. The method searches the inventory for an item whose description is equal to the parameter String, with the comparison being performed in a case-INSENSITIVE manner. If no such Ttem exists in the inventory, the method returns false, and the state of the Inventory does not change. On the other hand, if such an Item exist in the inventory, the method returns true and modifies the state of the Inventory as follows: -If the quantity attribute of the item is greater than or equal to 2, then its value is reduced by 1. -If the quantity attribute of the Item is equal to 1, then the item is removed from the Inventory. Note that removing an Item can be done by simply assigning null to the amay position that contains it. The state of the array before and after calling this method can consist of addresses of Item objects interspersed with null elements. For example, in an Inventory whose capacity is 20, it is possible that the array contains the addresses of Item objects al positions 3 and 17. while the other amay position contains null. This implies that the method does NOT have to "compact the array so that the addresses of all the actual tem objects appear at the beginning of the array while all the null elements appear at the end. You MAY assume that the string parameter is not null; in other words, your method does not have to handle cases in which the string parameter is indeed null Akmal notes • You MUST respect proper encapulation practices, that is the attributes and methods of your class MUST be declared using the proper visibility modifiers. • Y MUNT call the methods you were asked to write in the previous part to retrieve the descrip tion and quantity attributes of an Item object, or to change the quantity attribute of an Item object. You MAY me that the item class has been implemented correctly, even if you did not successfully complete the previous part WRITE YOUR Inventory CLASS IN THE SPACE BELOW:

For unlimited access to Homework Help, a Homework+ subscription is required.

Elin Hessel
Elin HesselLv2
14 Jun 2018

Unlock all answers

Get 1 free homework help answer.
Already have an account? Log in
Start filling in the gaps now
Log in