CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Java Basics M Taimoor Khan
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Chapter 41 Defining Classes and Methods Chapter 4.
Introduction to Computers and Programming Introduction to Methods in Java.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
CS 201 Functions Debzani Deb.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Understanding class definitions Looking inside classes.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
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.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Writing Classes (Chapter 4)
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Classes CS 21a: Introduction to Computing I First Semester,
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Flow of Control Part 1: Selection
CPS120: Introduction to Computer Science Decision Making in Programs.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
Procedural programming in Java Methods, parameters and return values.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Algorithms Writing instructions in the order they should execute.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Event 1 Principles of Programming Hugh Davis ECS The University of Southampton, UK users.ecs.soton.ac.uk/hcd.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Chapter 7 User-Defined Methods.
Methods Chapter 6.
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
MSIS 655 Advanced Business Applications Programming
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
IFS410 Advanced Analysis and Design
CS2011 Introduction to Programming I Methods (I)
Defining Classes and Methods
CS2011 Introduction to Programming I Arrays (I)
Chapter 6 Methods.
Classes CS 21a: Introduction to Computing I
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Introduction to Computer Science and Object-Oriented Programming
Introduction to Methods and Interfaces
Presentation transcript:

CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012

Remember the length “method”? String message = “Hello, Watson”; System.out.println( message.length() ); // prints 13  Method – useful code packaged with a name  In Java, methods belong to classes  With an object like message, use the object variable to “call” the method  works on information inside the object message refers to 2

Why write a method? 3  Code can get very long and unwieldy  Methods let us break up code into logical chunks  Readability – with appropriately named methods, your code is easier to read and follow  Reuse – make repeated code into a method. Write once, use many times.

Why write a method? 4  Code can get very long and unwieldy  Methods let us break up code into logical chunks  Readability – with appropriately named methods, your code is easier to read and follow  Reuse – make repeated code into a method. Write once, use many times. MODULARITY

Writing methods 5 public static method-name ( parameters ) { statements; return ;// Details later }

Writing methods 6 public static method-name ( parameters ) { statements; return ;// Details later } What “kind” of Method this is – More on this later.

Writing methods 7 public static method-name ( parameters ) { statements; return ;// Details later } Does this method produce a value, and if so, what type?

Writing methods 8 public static method-name ( parameters ) { statements; return ;// Details later } What name is used to “invoke” this method?

Writing methods 9 public static method-name ( parameters ) { statements; return ;// Details later } What information is expected from the caller in order to run this method? (Can be empty.)

Writing methods 10 public static method-name ( parameters ) { statements; return ;// Details later } The first line of a method is called the “method header” or “method signature”

Writing methods 11 public static method-name ( parameters ) { statements; return ;// Details later } What’s inside is called the “method body”

Writing methods 12 public static method-name ( parameters ) { statements; return ;// Details later } Statements executed when this method is called.

Writing methods 13 public static method-name ( parameters ) { statements; return ;// Details later } If the method “returns a value,” this is how we make that happen.

Example: Back to FunBrain… 14

Example: Back to FunBrain… 15 Hmm… looks suspiciously like a method…

Example: Back to FunBrain… 16 Check the guess against the secret number and generate a message to the user. Let’s make it into a method.

Compare The Two Implementations 17

Parameter passing 18 Formal parameter list

Parameter passing 19 Actual parameter list

Parameter passing 20 Can be any expression that evaluates to the type of the matching formal parameter.

What happens in memory? 21 … = checkGuess( userGuess, randomNumber, guesses ); userGuess randomNumber guesses … Memory used for variables in main() Memory used for variables in main()

What happens in memory? 22 … = checkGuess( userGuess, randomNumber, guesses ); public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ … userGuess randomNumber guesses … currentGuess secret numGuesses Memory used for formal parameters in checkGuess Memory used for formal parameters in checkGuess

What happens in memory? 23 … = checkGuess( userGuess, randomNumber, guesses ); public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ … userGuess randomNumber guesses … currentGuess secret numGuesses When method call is made, values stored in the arguments… …are copied into the formal parameters When method call is made, values stored in the arguments… …are copied into the formal parameters

What happens in memory? 24 … = checkGuess( userGuess, randomNumber, guesses ); public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ … userGuess randomNumber guesses … currentGuess secret numGuesses Arguments & formal parameters initially have the same values… but occupy different spaces in memory Arguments & formal parameters initially have the same values… but occupy different spaces in memory

Method facts 25  In Java, parameters are “passed by value”  (AKA “pass by copy”)  Each call to a method executes in its own memory space  Function: a method that creates and “returns” a value  Procedure: a method that only does work and returns no value (i.e., return type “void”)

Classes in Java 26  Classes contain two things:  methods (also called “behavior”)  data (also called “state”)  Encapsulate related data & methods  A good way of structuring large programs  e.g,. the Java libraries String class Random class Scanner class

Example: String 27  What’s the data for a String object?  The sequence of characters it was initialized with  The length of the sequence  What behavior is available?  length()  charAt(int pos)  indexOf(char c)  substring(int begin, int pastEnd)  … it goes on and on …

Methods with different jobs 28  Some methods let you request information from the class  “Accessor” methods  names often start with “get” (but not always)  e.g., charAt method in String class  Some methods cause a change to the state (i.e., data) held by the class  “Mutator” methods  names may start with “set” (but not always)  e.g., setSeed method in Random class

In order to use a class… 29  … need to know what methods it provides  … need to know what parameters they expect  … need to know how the methods behave  … don’t need to know how they were written  Application Programming Interface (API) Application Programming Interface  Again, need a variable of that class type  Use that variable to call methods in the class

In lab, who you gonna call? 30

Lab setup 31  What you are given:  GameBoard Class  Room Class  Ghostbusters skeleton  API documentation for GameBoard and Room  What you will write:  Several methods in Ghostbusters needed by GameBoard to run the game  Your methods will use methods from GameBoard and Room

Your methods 32  public void setupGame()  Sets up the initial game configuration (similar to Wumpus)  public boolean handleMove( String direction )  Logic to control Ghostbuster moving from room to room  public void handleFire( String direction )  Logic to control Ghostbuster firing proton Slimer  public boolean checkForLoss()  Determine if the Ghostbuster encountered Slimer or a portal  public void checkForGhostTrap()  Determine if the Ghostbuster found a desirable ghost trap

Ghostbusters: Two Classes Provided 33 public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row, int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row, int col) … } public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … }

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } 34 GameBoard

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } 35 GameBoard Keeps track of certain data related to the game, but you cannot assign these values directly. You must use methods provided by the class. Keeps track of certain data related to the game, but you cannot assign these values directly. You must use methods provided by the class.

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } 36 GameBoard Keeps track of certain data related to the game, but you cannot assign these values directly. You must use methods provided by the class. Keeps track of certain data related to the game, but you cannot assign these values directly. You must use methods provided by the class.

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } 37 GameBoard Keeps track of certain data related to the game, but you cannot assign these values directly. You must use methods provided by the class. Keeps track of certain data related to the game, but you cannot assign these values directly. You must use methods provided by the class.

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } 38 GameBoard To change anything with respect to the GameBoard, your program must have a variable of type GameBoard: GameBoard game = GameBoard.getGame(); To change anything with respect to the GameBoard, your program must have a variable of type GameBoard: GameBoard game = GameBoard.getGame();

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerCol(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom(int row,int col) … } 39 GameBoard Then you can use that variable to call methods within the GameBoard class: game.setPlayerRow( randomRow ); game.setPlayerCol( randomCol ); int theScore = game.getScore(); Then you can use that variable to call methods within the GameBoard class: game.setPlayerRow( randomRow ); game.setPlayerCol( randomCol ); int theScore = game.getScore();

public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } 40 Room

public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } 41 Room

public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } 42 Room To change anything with respect to a particular room, you must have a variable of the Room type. You get at a particular room via the GameBoard. GameBoard game = GameBoard.getGame(); Room firstRoom = game.getRoom(0,0);// To change anything with respect to a particular room, you must have a variable of the Room type. You get at a particular room via the GameBoard. GameBoard game = GameBoard.getGame(); Room firstRoom = game.getRoom(0,0);//

public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } public class Room { … public boolean containsSlimer() public boolean containsPortal() public boolean containsGhostTrap() public void showPlayerEnters() public void showPlayerExits() public void addSlimer() public void showSlimer() public void addPortal() public void showPortal() public void addGhostTrap() public void removeGhostTrap() public void showGhostTrap() public void showPlayerSlimed() public void showSlimerCaptured() … } 43 Room Then you can use that Room variable to call methods within the Room class: firstRoom.addPortal();; firstRoom.showPortal(); if (firstRoom.containsPortal()) … Then you can use that Room variable to call methods within the Room class: firstRoom.addPortal();; firstRoom.showPortal(); if (firstRoom.containsPortal()) …

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } 44 Ghostbusters Your setupGame() method is called by the GameBoard class when the game is first started.

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } 45 Ghostbusters And your method is responsible for putting things onto the game board appropriately…

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } 46 Ghostbusters Your handleMove(…) method is called by the GameBoard class whenever the user clicks an arrow button to move

public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class GameBoard { … int playerRow; int playerCol; int score; Room[][] gridOfRooms; public GameBoard getGame() public void setPlayerRow(int row) public void setPlayerRow(int col) public int getPlayerRow() public int getPlayerCol() public int getScore() public void updateScore(int score) public void updateMessage(String m) public Room getRoom() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } 47 Ghostbusters Your handleFire(…) method is called by the GameBoard class whenever the user clicks a red arrow button to fire

public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } public class Ghostbusters { … public void setupGame() public boolean handleMove(String dir) public void handleFire(String dir) public boolean checkForLoss() public void checkForGhostTrap() … } 48 Ghostbusters Your checkForLoss() and checkForGhostTrap() methods are called by your own handleMove(…) method Your checkForLoss() and checkForGhostTrap() methods are called by your own handleMove(…) method