Structural Programming Course Content and Data Structures • Introduction • Vectors • Objects • Testing/Debugging Winter 2000 • Methods • Arrays • Tracing Programs • Searching CMPUT 102: Sharing Resources • Object State • Files I/O • Sharing resources • Sorting Dr. Osmar R. Zaïane • Selection • Inheritance • Repetition • Recursion University of Alberta Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 1 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 2 2 Objectives of Lecture 13 Outline of Lecture 13 Common Resources Common Resources – – Static Variables and Methods Static Variables and Methods • Understand the use of static variables to share • Static variables common information between instances of the • Static methods same class. • Adventure Version 4 • Study the use of static methods to perform computations that independent of any object. • Re-write the Adventure program using some useful static variables . Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 3 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 4 Static Variables Resources Common to a Class • It is wasteful to require every object in a class to • Sometimes common sharable resources are use an instance variable to access these common needed by each object in a class. resources. – (DFK &LUFOH REMHFW PD\ QHHG WKH YDOXH RI � VR • Instead, we can define a static variable in the it can compute and return its area. class that can be bound to this common resource: – Each TreasureChest object may need a common maximum number of tokens so it can generate a public class Circle … random number ≤ this number when it is private static final float pi = 3.14159f; constructed (initialized). public class TreasureChest … – Each TreasureChest object may need a random private static final int maxTokens = 10; number generator object to generate its initial private static RandomInt generator; number of tokens. generator = new RandomInt(1); Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 5 Structural Programming and Data Structures University of Alberta 6 1
Object Independent Actions Outline of Lecture 13 • Some actions are not performed by a • Static variables specific instance of a class. • Static methods – Start an application program. • Adventure Version 4 – Perform an operation on some values. • In fact, no instance may even need to exist for these actions to be performed. Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 7 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 8 Static Methods Outline of Lecture 13 • A static method or class method is code that can be executed without sending a message to any object: • Static variables public class OurProgram … • Static methods public static void main(String args[]) public class String … • Adventure Version 4 public static String valueOf(int i) • The syntax of static method calls looks like a message is being sent to a class: String.valueOf(3) � “3” • However, no message is actually involved. Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 9 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 10 Adventure Version 4 Adventure - Code Change Summary • We are going to add some functionality to the Arithmetic Adventure game . • In the Adventure class we will: • We will put treasure chests in rooms. – replace the method enterRoom(Adventurer) • Add a class called Chest. • When the adventurer tries to open a chest we will generate an arithmetic question. • Add a class called Question. • The chest will contain a random number of • Add a class called RandomInt tokens that will be added or subtracted to the • Leave the Adventurer class unchanged. adventurer's total, depending on whether the adventurer answers the question correctly. Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 11 Structural Programming and Data Structures University of Alberta 12 2
Running Adventure 4 Program - Adventure 4.1 import java.util.*; NO CHANGES public class Adventure { /* Version 4 This program is an arithmetic adventure game … */ /* Constructors */ public Adventure () { /* Initialize an adventure by creating the appropriate objects. */ } Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 13 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 14 NO CHANGES Program - Adventure 4.3 Program - Adventure 4.2 /* Private Instance Methods */ NO CHANGES /* Main program */ private void play() { /* public static void main(String args[]) { Plays the Adventure game. Adventure game; */ game = new Adventure(); Adventurer adventurer; game.play(); } adventurer = this.greeting(); this.enterRoom(adventurer); this.farewell(adventurer); } Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 15 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 16 Program - Adventure 4.4 Program - Adventure 4.5 NO CHANGES NO CHANGES private Adventurer greeting() { /* System.out.print("Well "); Great the user and answer an Adventurer that System.out.print(playerName); represents the user. System.out.println (", after a day of hiking you spot a silver cube." ); */ System.out.println ("The cube appears to be about 5 meters on each side." ); String playerName; System.out.println ("You find a green door, open it and enter." ); System.out.println (" The door closes behind you with a soft whir and disappears." ); System.out.println(" Welcome to the Arithmetic Adventure game. "); System.out.println ( " There is a feel of mathematical magic in the air." ); System.out.print("The date is "); Keyboard.in.pause(); System.out.println(new Date()); return new Adventurer(playerName); System.out.println(); } System.out.print("What is your name?"); playerName = Keyboard.in.readString(); Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 17 Structural Programming and Data Structures University of Alberta 18 3
Program - Adventure 3.6 Program - Adventure 4.6 private void enterRoom(Adventurer adventurer) { /* private void enterRoom(Adventurer adventurer) { OLD The given adventurer has entered the /* NEW first room. The given adventurer has entered the */ first room. Integer myTokens; */ Chest chest; System.out.print(" How many tokens would you like, "); System.out.print(adventurer.name()); chest = new Chest(); System.out.print("?"); chest.display(); myTokens = Keyboard.in.readInteger(); adventurer.gainTokens(myTokens.intValue()); chest.open(adventurer); } } Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 19 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 20 Program - Adventure 4.7 Class - Chest 4.1 NO CHANGES import java.util.*; public class Chest { /* private void farewell(Adventurer adventurer) { An instance of this class represents a treasure chest in /* the Adventure game. A Chest contains a number of tokens. Say farewell to the user and report the game result. */ */ /* Constructor */ public Chest() { System.out.print("Congratulations "); /* System.out.print(adventurer.name()); Initialize me so that I contain a random number of System.out.print(" you have left the game with "); tokens. System.out.print(adventurer.tokens()); */ System.out.println(" tokens."); this.tokens = Chest.generator.next(Chest.maxTokens); } } Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 21 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 22 Class - Chest 4.3 Class - Chest 4.2 public void open(Adventurer adventurer) { /* Ask the user an arithmetic question and if a correct /* Instance Methods */ answer is given, add tokens to the given Adventurer. If it is answered incorrectly, remove tokens. */ public void display() { Question question; /* Output a description of myself. question = new Question(); */ question.ask(); // We really want to do only one of the next two System.out.println("There is a small carved chest in the center of // lines, depending on the user’s answer. the room."); System.out.println("It appears to be a treasure chest!"); this.correctAnswer(adventurer); this.wrongAnswer(question, adventurer); } } Dr. Osmar R. Zaïane, 2000 Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 23 Structural Programming and Data Structures University of Alberta 24 4
Recommend
More recommend