![Page 1: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/1.jpg)
M1G413283
Introduction to Programming 2
3. Creating Classes:
Room and Item
![Page 2: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/2.jpg)
Interaction between Room and Items
Create Java code to implement the Room and Item classes
The link between a room and its items is similar to that between the game and its players
Each room can hold several items, and there is a “has-a” relationship between Room and Item
Introduction to Programming 2 3. Creating Classes: Room and Item2
![Page 3: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/3.jpg)
Interaction between Room and Items
There are some differences, however: There will be more than one room in the game
world Each room may have a different number of items We would like to be able to add and remove items
from a room as the game progresses
Introduction to Programming 2 3. Creating Classes: Room and Item3
![Page 4: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/4.jpg)
Interaction between Room and Items
The Item class will have a behaviour called use, which will perform whatever action the item is used for
Another object can then send a message to an item by calling the use method
However, it would not make sense for the room to use the item
What kind of object do you think should use an item?
Introduction to Programming 2 3. Creating Classes: Room and Item4
![Page 5: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/5.jpg)
Interaction between Room and Items
The room simply has the job of holding the items
It should provide these items to other objects to use
It should provide: one item (getItem) its whole set of items (getItems)
Introduction to Programming 2 3. Creating Classes: Room and Item5
![Page 6: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/6.jpg)
Class Diagram
Introduction to Programming 2 3. Creating Classes: Room and Item6
![Page 7: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/7.jpg)
The Item class
Demonstration
Introduction to Programming 2 3. Creating Classes: Room and Item7
![Page 8: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/8.jpg)
Implementing the interaction
If you look through the code for the Item class, there is no mention of the Room class.
That is because an Item object does not need to communicate with the Room it is in.
Also the Room does not need to communicate with its items.
Introduction to Programming 2 3. Creating Classes: Room and Item8
![Page 9: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/9.jpg)
Implementing the interaction
However Room needs to store its items Implmented using the same “HAS-A
(ARRAY)” pattern which we used for Game and Player.
Remember that the solution for that pattern was: “the class which needs to send the message
has a field which is an array of objects whose type is the name of the other class.”
Introduction to Programming 2 3. Creating Classes: Room and Item9
![Page 10: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/10.jpg)
Room class - outline
Introduction to Programming 2 3. Creating Classes: Room and Item10
keeps track of the number of items which have been added. so that new items are added in the right place.
keeps track of the number of items which have been added. so that new items are added in the right place.
![Page 11: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/11.jpg)
Room class - outline
Introduction to Programming 2 3. Creating Classes: Room and Item11
![Page 12: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/12.jpg)
Null references
The array when it is first created contains null references
Null reference is a reference to an object which does not exist
Each element in the array should point to an Item object, but no Item objects have been created yet
When the first Item is added, the first element of the array will point to it while the remaining elements are still null references
Introduction to Programming 2 3. Creating Classes: Room and Item12
![Page 13: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/13.jpg)
Object diagram
Introduction to Programming 2 3. Creating Classes: Room and Item13
![Page 14: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/14.jpg)
The addItem method
This simply takes an Item object and sets the next available array element in items to refer to that object.
Introduction to Programming 2 3. Creating Classes: Room and Item14
![Page 15: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/15.jpg)
Object diagram
Introduction to Programming 2 3. Creating Classes: Room and Item15
![Page 16: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/16.jpg)
Object diagram as new item is added
Introduction to Programming 2 3. Creating Classes: Room and Item16
![Page 17: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/17.jpg)
Object diagram after new item is added
Introduction to Programming 2 3. Creating Classes: Room and Item17
![Page 18: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/18.jpg)
Using the addItem method
An example of the use of this method will be within the setup method of the Game, which might have code like this:
The getItem and removeItem methods are a little bit more complicated, and will require a bit of thought to work out the algorithms which are needed to implement them
Introduction to Programming 2 3. Creating Classes: Room and Item18
![Page 19: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/19.jpg)
Algorithms
An algorithm is a list of well-defined steps for completing a task
We need to work out what steps are required to search for a particular Item and to remove a particular Item.
The getItem method must take a string as a parameter, and return the Item in the items array which has that value of description
Introduction to Programming 2 3. Creating Classes: Room and Item19
![Page 20: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/20.jpg)
Algorithms
How do we find a particular element in an array?
Start at the beginning of the array Check each element in turn to see if it is the
one we want If it is, then the target is found This is called a linear search
Introduction to Programming 2 3. Creating Classes: Room and Item20
![Page 21: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/21.jpg)
Pseudocode
We can write the algorithm for the search using pseudocode
Pseudocode is not a real programming language
it is simply a way of thinking about and writing down what the steps for an algorithm are
Don’t worry about the details of the actual code until we are clear about how it will work
Introduction to Programming 2 3. Creating Classes: Room and Item21
![Page 22: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/22.jpg)
Stepwise refinement
First attempt at getItem algorithm:
1. while (target not found and end of array not reached)
2. check next item3. return target item
Introduction to Programming 2 3. Creating Classes: Room and Item22
![Page 23: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/23.jpg)
Stepwise refinement
The step in line 2 looks as if it needs a bit more thought
How do we check the item and what do we do when the item is the target item?
We’ll need to keep count of how far through the array we get so that the correct item can be returned
We’ll also need to set a flag which will indicate that the target item has been found
Introduction to Programming 2 3. Creating Classes: Room and Item23
![Page 24: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/24.jpg)
Stepwise refinement
1.1 count = 01.2 set target not found1.3. while (not found and end of array not
reached)2.1 if item is target2.2 set target found2.3 increment count3.1 return item from array with index = count Now close to something which can be
translated directly into Java codeIntroduction to Programming 2 3. Creating Classes: Room and Item
24
![Page 25: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/25.jpg)
The getItem method
Introduction to Programming 2 3. Creating Classes: Room and Item25
![Page 26: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/26.jpg)
Will this work?
This code looks OK, and it will compile However, it has at least two serious flaws Before we move on, we really need to do
some testing of the Room class Check that the methods really do what they
are supposed to do.
Introduction to Programming 2 3. Creating Classes: Room and Item26
![Page 27: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/27.jpg)
Unit testing
The process of testing an individual class is known as unit testing
This contrasts with testing the full program Unit testing is vitally important in object
oriented programming Unit tests can be repeated as we continue to
develop a class Make sure that we don’t inadvertently break a
part of a class which was working correctly
Introduction to Programming 2 3. Creating Classes: Room and Item27
![Page 28: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/28.jpg)
Unit testing
We’ve already done a little bit of unit testing We used BlueJ to run some tests on the first
versions of the Player and Game classes Good unit tests actually need some careful
thought Make sure that they test a class thoroughly in
all possible circumstances
Introduction to Programming 2 3. Creating Classes: Room and Item28
![Page 29: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/29.jpg)
Test cases for Room class
Add items to the array Add sufficient items to fill the array Try to add an item when the array is full (the
desired result is that this should not work) Get an item which is in the array Try to get an item which is not in the array
when the array is not full, and when it is full
Introduction to Programming 2 3. Creating Classes: Room and Item29
![Page 30: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/30.jpg)
Creating a test class and running a test
Demonstration
Introduction to Programming 2 3. Creating Classes: Room and Item30
![Page 31: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/31.jpg)
The getItem method - fixed
Introduction to Programming 2 3. Creating Classes: Room and Item31
![Page 32: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/32.jpg)
Documentation
A Java object in an object-oriented program is likely to be used by other objects
For example, in the completed game, an Item object will be used by a Player object
The interface of a class specifies how objects of that class may be used
Introduction to Programming 2 3. Creating Classes: Room and Item32
![Page 33: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/33.jpg)
Interface of a class
Includes Public fields – fields declared with the public key
word Public methods – methods defined with the
public key word Does not include
Private fields Private methods
Introduction to Programming 2 3. Creating Classes: Room and Item33
![Page 34: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/34.jpg)
Javadoc
It is helpful to designers of classes which use your class if you take time to carefully document the interface of your class
Documentation is done by writing Javadoc comments in your code
See demonstration
Introduction to Programming 2 3. Creating Classes: Room and Item34
![Page 35: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/35.jpg)
Viewing documentation
Javadoc comments can be used by the Javadoc tool
Automatically generates a set of HTML pages which document a single class or an entire programme
They are also used by the BlueJ editor when you choose to view a class as Documentation instead of Source Code
Introduction to Programming 2 3. Creating Classes: Room and Item35
![Page 36: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/36.jpg)
The Player class – using Room and Item
We have now created and tested initial versions of the Room and Item classes
These classes exist in the game because a Player needs to be located in a Room and can use Items in the room
The Player class therefore needs to be able to interact with Room and Item
Introduction to Programming 2 3. Creating Classes: Room and Item36
![Page 37: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/37.jpg)
Class diagram
Introduction to Programming 2 3. Creating Classes: Room and Item37
![Page 38: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/38.jpg)
Player and Room
A Player object needs to maintain an association with the current room in which the player is located
This is the “has-a” code pattern Problem: how do you implement a “has-a”
relationship, where a “whole” object needs to send messages to its “part” objects?
Solution: the class which needs to send the message has a field whose type is the name of the other class
Introduction to Programming 2 3. Creating Classes: Room and Item38
![Page 39: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/39.jpg)
Player and Room
What message will a Player object need to send to a Room object?
The room stores an array of items, so the player may need to ask the room for to provide it with: a list of the available items a reference to the specific Item object it wants to
use
Introduction to Programming 2 3. Creating Classes: Room and Item39
![Page 40: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/40.jpg)
Player and Room
The association implemented by having a field of type Room in the Player class
Introduction to Programming 2 3. Creating Classes: Room and Item40
![Page 41: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/41.jpg)
Player and Item
A Player object does not need to own any items
These belong to a room, not a player However, it may need to use an Item object This is an example of a new coding pattern: “Uses-a”
Introduction to Programming 2 3. Creating Classes: Room and Item41
![Page 42: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/42.jpg)
Code pattern: “Uses-a”
Problem: how do you implement a “uses-a” relationship, where an object needs to send a message to another object
Solution: the class which needs to send the message has a method parameter or local variable whose type is the name of the other class
Introduction to Programming 2 3. Creating Classes: Room and Item42
![Page 43: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/43.jpg)
Code pattern: “Uses-a”
This type of association is a bit like booking a holiday through a travel agent
You use the agent to making the booking, and you then own that booking
However, you have no link to the agent once the booking has been made
Temporary, or transient, association The association is implemented in the
takeTurn method of Player
Introduction to Programming 2 3. Creating Classes: Room and Item43
![Page 44: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/44.jpg)
The takeTurn method
1. A message is sent to the currentRoom object, calling its getItems method
2. An array of Item objects is obtained as a result – this is a local variable, and each Item in the array is itself accessed individually as a local variable
3. Each Item object is used in turn, by calling its use method
Introduction to Programming 2 3. Creating Classes: Room and Item44
![Page 45: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/45.jpg)
Simplified for loop
Simplified, or enhanced, for loop is useful when stepping through arrays
No need to specify end condition or to initialise and increment counter variable
for(Item it : items){...
}
Introduction to Programming 2 3. Creating Classes: Room and Item45
![Page 46: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/46.jpg)
Testing the Player class
Demonstration
Introduction to Programming 2 3. Creating Classes: Room and Item46
![Page 47: M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item](https://reader035.vdocuments.us/reader035/viewer/2022070416/5697c01e1a28abf838cd0c9e/html5/thumbnails/47.jpg)
Summary
Programming techniques:
stepwise refinement, unit testing, documentation, enhanced for loop
Class associations:
“has-a” relationships“uses-a” relationships
Introduction to Programming 2 2. Creating Classes: Game and Player47