Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012."— Presentation transcript:

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

2 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

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

4 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

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

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

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

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

9 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.)

10 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”

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

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

13 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.

14 Example: Back to FunBrain… 14

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

16 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.

17 Compare The Two Implementations 17

18 Parameter passing 18 Formal parameter list

19 Parameter passing 19 Actual parameter list

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

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

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

23 What happens in memory? 23 … = checkGuess( userGuess, randomNumber, guesses ); public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ … userGuess randomNumber guesses 25 47 2 … currentGuess secret numGuesses 25 47 2 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

24 What happens in memory? 24 … = checkGuess( userGuess, randomNumber, guesses ); public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ … userGuess randomNumber guesses 25 47 2 … currentGuess secret numGuesses 25 47 2 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

25 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”)

26 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

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

28 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

29 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

30 In lab, who you gonna call? 30

31 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

32 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 pack @ 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

33 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() … }

34 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

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

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

37 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.

38 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();

39 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();

40 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

41 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

42 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);//

43 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()) …

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

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

46 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

47 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

48 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


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

Similar presentations


Ads by Google