M1G413283 Introduction to Programming 2 3. Creating Classes: Room and Item.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Chapter 10 Introduction to Arrays
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Computer Science 1620 Programming & Problem Solving.
Chapter 9 Introduction to Arrays
Chapter 1 Program Design
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Chapter 2: Algorithm Discovery and Design
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Object Oriented Software Development
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Array.
Simple Program Design Third Edition A Step-by-Step Approach
Chapter 3 Introduction to Collections – Stacks Modified
Nachos Phase 1 Code -Hints and Comments
Invitation to Computer Science, Java Version, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
ArrayList, Multidimensional Arrays
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
By the end of this session you should be able to...
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Object Oriented Software Development
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
The Software Development Process
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
M1G Introduction to Programming 2 5. Completing the program.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Chapter 9 Introduction to Arrays Fundamentals of Java.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Algorithms and Problem Solving
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Software Development Techniques
Presentation transcript:

M1G Introduction to Programming 2 3. Creating Classes: Room and Item

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 Item 2

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 Item 3

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 Item 4

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 Item 5

Class Diagram Introduction to Programming 2 3. Creating Classes: Room and Item 6

The Item class Demonstration Introduction to Programming 2 3. Creating Classes: Room and Item 7

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 Item 8

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 Item 9

Room class - outline Introduction to Programming 2 3. Creating Classes: Room and Item 10 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.

Room class - outline Introduction to Programming 2 3. Creating Classes: Room and Item 11

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 Item 12

Object diagram Introduction to Programming 2 3. Creating Classes: Room and Item 13

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 Item 14

Object diagram Introduction to Programming 2 3. Creating Classes: Room and Item 15

Object diagram as new item is added Introduction to Programming 2 3. Creating Classes: Room and Item 16

Object diagram after new item is added Introduction to Programming 2 3. Creating Classes: Room and Item 17

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 Item 18

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 Item 19

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 Item 20

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 Item 21

Stepwise refinement First attempt at getItem algorithm: 1. while (target not found and end of array not reached) 2.check next item 3. return target item Introduction to Programming 2 3. Creating Classes: Room and Item 22

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 Item 23

Stepwise refinement 1.1 count = set target not found 1.3. while (not found and end of array not reached) 2.1if item is target 2.2set target found 2.3increment count 3.1 return item from array with index = count Now close to something which can be translated directly into Java code Introduction to Programming 2 3. Creating Classes: Room and Item 24

The getItem method Introduction to Programming 2 3. Creating Classes: Room and Item 25

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 Item 26

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 Item 27

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 Item 28

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 Item 29

Creating a test class and running a test Demonstration Introduction to Programming 2 3. Creating Classes: Room and Item 30

The getItem method - fixed Introduction to Programming 2 3. Creating Classes: Room and Item 31

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 Item 32

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 Item 33

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 Item 34

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 Item 35

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 Item 36

Class diagram Introduction to Programming 2 3. Creating Classes: Room and Item 37

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 Item 38

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 Item 39

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 Item 40

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 Item 41

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 Item 42

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 Item 43

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 Item 44

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 Item 45

Testing the Player class Demonstration Introduction to Programming 2 3. Creating Classes: Room and Item 46

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 Player 47