student responsibilities mat 2170 week 9
play

Student Responsibilities Mat 2170 Week 9 Reading: Textbook, - PDF document

Student Responsibilities Mat 2170 Week 9 Reading: Textbook, Sections 6.1 6.3 Objects and Classes Lab 9 Attendance Spring 2014 1 2 Recall: Writing Methods Notes About Using Methods A method invocation or call uses its name


  1. Student Responsibilities Mat 2170 Week 9 ◮ Reading: Textbook, Sections 6.1 – 6.3 Objects and Classes ◮ Lab 9 ◮ Attendance Spring 2014 1 2 Recall: Writing Methods Notes About Using Methods ◮ A method invocation or call uses its name and supplies ◮ Decomposition : break a problem down into smaller subproblems arguments that correspond to the parameters in the method implementation. ◮ Use methods whenever you can in labs from now on. ◮ A predicate method returns a boolean value. scope type name (argument list) { ◮ You must be aware of the return type of any method you statements in the method body invoke, since you will either be: } ◮ using it in an expression ◮ assigning it to an object ◮ or displaying it 1. scope indicates who has access to the method (public) 2. type indicates what type of value the method returns -or- if(!isPalindrome(n)) double x = sqrt(y) 3. name is the name of the method ◮ Do not place a print or println statement in a method to 4. argument list is the list of declarations for the variables used to display a calculated value unless that is the express purpose of hold the values of each argument the method. If the return type isn’t void , the method shouldn’t display any results. 3 4 Chapter Six: Objects and Classes Creating a RandomGenerator Object Before writing our own classes , it helps to look more closely at how to use classes that someone else has developed. ◮ The first step in writing a program that uses randomness is to create an instance (object) of the RandomGenerator class. Using the RandomGenerator Class ◮ The RandomGenerator class makes it possible to write ◮ The best way to do so, is to call the getInstance() method, programs that simulate random processes, such as flipping a coin which returns a single shared instance of a random generator. or rolling a die. ◮ Programs that involve random processes like this are said to be non–deterministic . ◮ The standard for that declaration looks like this: ◮ Non–determinism is essential to many applications, such as computer games . private RandomGenerator rgen = RandomGenerator.getInstance(); ◮ It also has important practical uses in simulations , computer security , and algorithmic research . 5 6

  2. RandomGenerator Method Interfaces ◮ This declaration usually appears outside of any method (but still public int nextInt(int low, int high) Returns a random int in interval [ low .. high ] in the program class), and is therefore an example of an instance variable . public int nextInt(int n) Returns a random int in interval [0.. n − 1] public double nextDouble(double low, double high) Returns a random double d , low ≤ d < high ◮ The keyword private indicates that this variable can be used from any method within this class , but is not accessible to public double nextDouble() Returns a random double d , 0 ≤ d < 1 other classes. public boolean nextBoolean() Returns a random boolean , which is true 50% of the time ◮ To obtain a random value, send a message to the generator public boolean nextBoolean(double p) Returns a random boolean , which is true with probability ( rgen in the last example), which responds with the result. p , 0 ≤ p < 1 public Color nextColor() Returns a random color 7 8 Using RandomGenerator Methods Notes on Overloading Methods ◮ To use RandomGenerator methods, invoke them using the name ◮ The nextInt() , nextDouble() , and nextBoolean() methods of your RandomGenerator instance (e.g., rgen ) as the receiver. all exist in more than one form. ◮ As an example, you could simulate rolling a die by: ◮ Java determines which version is used by checking the number int die = rgen.nextInt(1, 6); and types of arguments used. ◮ To simulate flipping a coin : ◮ Methods that have the same name but differ in their argument structure are said to be overloaded . boolean isHeads = rgen.nextBoolean(); 9 10 Examples: Generating Random Values The Dice Game ”Craps” ◮ At the beginning of the game, the player rolls a pair of dice and ◮ To set the variable total to the sum of two six–sided dice: computes the total . int d1 = rgen.nextInt(1, 6); 1. If the total is 2, 3, or 12 (called ”craps”), the player loses , game int d2 = rgen.nextInt(1, 6); over. int total = d1 + d2; 2. If the total is 7 or 11 (called a ”natural”), the player wins , game over. 3. If the total is any other number (4, 5, 6, 8, 9, or 10), that ◮ To flip a coin that comes up heads 60% of the time: number becomes the ” point .” boolean isHeads = rgen.nextBoolean(0.6); From here, the player keeps rolling the dice until: 3.1 the point comes up again, in which case the player wins or 3.2 a 7 appears, in which case the player loses . ◮ To randomly change the fill color of rect : rect.setFillColor(rgen.nextColor()); (The numbers 2, 3, 11, and 12 have no special significance after the first roll.) 11 12

  3. Craps Algorithm Simulating Craps – rollTwoDice() Roll two dice, yielding total /* Rolls two dice and returns their sum. */ private int rollTwoDice() If total is 7 or 11 { int d1 = rgen.nextInt(1, 6); player automatically wins int d2 = rgen.nextInt(1, 6); return d1 + d2; otherwise if total is 2, 3, or 12 } player automatically loses otherwise /* Private instance variables / (player has rolled private RandomGenerator rgen = RandomGenerator.getInstance(); 4, 5, 6, 8, 9, or 10, their point) player continues to roll dice until they roll their point and win, or Warning : do not use print() or println() they roll a 7 and lose in a method unless that is the method’s purpose. 13 14 The Craps Program default: // rolled 4, 5, 6, 8, 9, or 10 int point = total; println("Your point is: " + point + "."); total = rollTwoDice(); public void run() { while (total != 7 && total != point){ int total = rollTwoDice(); println("You rolled " + total + ", rolling again."); total = rollTwoDice(); switch (total) } // end while { case 7: case 11: if (total == point) println("You rolled a natural. You win."); println("You made your point. You win."); break; else // (total == 7) println("You rolled a 7. You lose."); case 2: case 3: case 12: println("You rolled " + total + ". You lose."); } // end switch break; } // end run() 15 16 Clients and Implementers Two Views of Methods ◮ Clients need to know: ◮ It is useful to recognize that there are two perspectives that we ◮ what methods are available in a class, and can take with respect to a particular class. ◮ how to call them ◮ Clients are not interested in the details of how a method works. ◮ Often, we will find ourselves using a class we didn’t write (for example, the RandomGenerator class). ◮ The Implementer , on the other hand, is primarily interested in ◮ When this happens, we are acting as a client of the class. precisely those details . ◮ The Implementer of a class should try to hide complexity from ◮ When we write the code for a method, we are acting as an its clients. implementer . ◮ The RandomGenerator class hides a considerable amount of complexity. ◮ Clients and Implementers look at a class in different ways. 17 18

Recommend


More recommend