mat 2170
play

Mat 2170 Review Week 9 Random Overloading Craps Objects and - PowerPoint PPT Presentation

Mat 2170 Week 9 Objects and Classes Week 9 Mat 2170 Review Week 9 Random Overloading Craps Objects and Classes Clients Packages Randomness Seed Spring 2014 Lab 9 Intervals Classes Student Responsibilities Mat 2170 Week 9


  1. Mat 2170 Week 9 Objects and Classes Week 9 Mat 2170 Review Week 9 Random Overloading Craps Objects and Classes Clients Packages Randomness Seed Spring 2014 Lab 9 Intervals Classes

  2. Student Responsibilities Mat 2170 Week 9 Objects and Classes Week 9 Review Random Reading: Textbook, Sections 6.1 – 6.3 Overloading Craps Lab 9 Clients Packages Attendance Randomness Seed Lab 9 Intervals Classes

  3. Recall: Writing Methods Mat 2170 Week 9 Decomposition : break a problem down into smaller Objects and subproblems Classes Week 9 Use methods whenever you can in labs from now on. Review Random scope type name (argument list) { Overloading statements in the method body Craps } Clients Packages 1. scope indicates who has access to the method (public) Randomness Seed 2. type indicates what type of value the method returns Lab 9 3. name is the name of the method Intervals 4. argument list is the list of declarations for the variables used Classes to hold the values of each argument

  4. Notes About Using Methods Mat 2170 A method invocation or call uses its name and supplies Week 9 arguments that correspond to the parameters in the method Objects and Classes implementation. Week 9 A predicate method returns a boolean value. Review Random You must be aware of the return type of any method you Overloading invoke, since you will either be: Craps using it in an expression Clients assigning it to an object Packages or displaying it Randomness -or- if(!isPalindrome(n)) double x = sqrt(y) Seed Lab 9 Do not place a print or println statement in a method to Intervals display a calculated value unless that is the express purpose Classes of the method. If the return type isn’t void , the method shouldn’t display any results.

  5. Chapter Six: Objects and Classes Mat 2170 Before writing our own classes , it helps to look more closely at Week 9 how to use classes that someone else has developed. Objects and Classes Using the RandomGenerator Class Week 9 Review The RandomGenerator class makes it possible to write Random programs that simulate random processes, such as flipping a Overloading coin or rolling a die. Craps Clients Programs that involve random processes like this are said to Packages be non–deterministic . Randomness Seed Non–determinism is essential to many applications, such as Lab 9 computer games . Intervals It also has important practical uses in simulations , Classes computer security , and algorithmic research .

  6. Creating a RandomGenerator Object Mat 2170 Week 9 The first step in writing a program that uses randomness is to Objects and Classes create an instance (object) of the RandomGenerator class. Week 9 Review Random The best way to do so, is to call the getInstance() method, Overloading which returns a single shared instance of a random Craps generator. Clients Packages Randomness The standard for that declaration looks like this: Seed Lab 9 private RandomGenerator rgen = Intervals RandomGenerator.getInstance(); Classes

  7. Mat 2170 Week 9 This declaration usually appears outside of any method (but Objects and Classes still in the program class), and is therefore an example of an Week 9 instance variable . Review Random Overloading The keyword private indicates that this variable can be used Craps from any method within this class , but is not accessible to Clients other classes. Packages Randomness Seed Lab 9 To obtain a random value, send a message to the generator Intervals ( rgen in the last example), which responds with the result. Classes

  8. RandomGenerator Method Interfaces Mat 2170 public int nextInt(int low, int high) Week 9 Returns a random int in interval [ low .. high ] Objects and Classes public int nextInt(int n) Week 9 Returns a random int in interval [0.. n − 1] Review public double nextDouble(double low, double high) Random Returns a random double d , low ≤ d < high Overloading public double nextDouble() Craps Returns a random double d , 0 ≤ d < 1 Clients Packages public boolean nextBoolean() Returns a random boolean , which is true 50% of the time Randomness Seed public boolean nextBoolean(double p) Lab 9 Returns a random boolean , which is true with probability Intervals p , 0 ≤ p < 1 Classes public Color nextColor() Returns a random color

  9. Using RandomGenerator Methods Mat 2170 Week 9 Objects and To use RandomGenerator methods, invoke them using the Classes name of your RandomGenerator instance (e.g., rgen ) as the Week 9 receiver. Review Random Overloading As an example, you could simulate rolling a die by: Craps Clients int die = rgen.nextInt(1, 6); Packages Randomness Seed To simulate flipping a coin : Lab 9 Intervals boolean isHeads = rgen.nextBoolean(); Classes

  10. Notes on Overloading Methods Mat 2170 Week 9 Objects and Classes The nextInt() , nextDouble() , and nextBoolean() Week 9 methods all exist in more than one form. Review Random Overloading Java determines which version is used by checking the Craps number and types of arguments used. Clients Packages Randomness Methods that have the same name but differ in their Seed Lab 9 argument structure are said to be overloaded . Intervals Classes

  11. Examples: Generating Random Values Mat 2170 Week 9 To set the variable total to the sum of two six–sided dice: Objects and Classes int d1 = rgen.nextInt(1, 6); Week 9 int d2 = rgen.nextInt(1, 6); Review int total = d1 + d2; Random Overloading Craps To flip a coin that comes up heads 60% of the time: Clients Packages boolean isHeads = rgen.nextBoolean(0.6); Randomness Seed Lab 9 To randomly change the fill color of rect : Intervals Classes rect.setFillColor(rgen.nextColor());

  12. The Dice Game ”Craps” At the beginning of the game, the player rolls a pair of dice Mat 2170 Week 9 and computes the total . Objects and Classes 1. If the total is 2, 3, or 12 (called ”craps”), the player loses , Week 9 game over. Review 2. If the total is 7 or 11 (called a ”natural”), the player wins , Random game over. Overloading Craps 3. If the total is any other number (4, 5, 6, 8, 9, or 10), that Clients number becomes the ” point .” Packages From here, the player keeps rolling the dice until: Randomness 3.1 the point comes up again, in which case the player wins or Seed Lab 9 3.2 a 7 appears, in which case the player loses . Intervals Classes (The numbers 2, 3, 11, and 12 have no special significance after the first roll.)

  13. Craps Algorithm Mat 2170 Week 9 Roll two dice, yielding total Objects and Classes If total is 7 or 11 Week 9 player automatically wins Review Random otherwise if total is 2, 3, or 12 Overloading player automatically loses Craps Clients otherwise Packages (player has rolled Randomness 4, 5, 6, 8, 9, or 10, their point) Seed Lab 9 player continues to roll dice until Intervals Classes they roll their point and win, or they roll a 7 and lose

  14. Simulating Craps – rollTwoDice() Mat 2170 Week 9 /* Rolls two dice and returns their sum. */ Objects and private int rollTwoDice() Classes { Week 9 int d1 = rgen.nextInt(1, 6); Review int d2 = rgen.nextInt(1, 6); Random return d1 + d2; Overloading } Craps Clients /* Private instance variables / Packages private RandomGenerator rgen = Randomness RandomGenerator.getInstance(); Seed Lab 9 Intervals Classes Warning : do not use print() or println() in a method unless that is the method’s purpose.

  15. The Craps Program Mat 2170 Week 9 Objects and Classes public void run() { Week 9 int total = rollTwoDice(); Review Random switch (total) Overloading { Craps case 7: case 11: Clients println("You rolled a natural. You win."); break; Packages Randomness case 2: case 3: case 12: Seed println("You rolled " + total + ". You lose."); Lab 9 break; Intervals Classes

  16. Mat 2170 Week 9 default: // rolled 4, 5, 6, 8, 9, or 10 Objects and int point = total; Classes println("Your point is: " + point + "."); total = rollTwoDice(); Week 9 Review while (total != 7 && total != point){ Random println("You rolled " + total + ", rolling again."); Overloading total = rollTwoDice(); Craps } // end while Clients if (total == point) Packages println("You made your point. You win."); Randomness else // (total == 7) Seed println("You rolled a 7. You lose."); Lab 9 Intervals } // end switch Classes } // end run()

  17. Clients and Implementers Mat 2170 Week 9 It is useful to recognize that there are two perspectives that Objects and Classes we can take with respect to a particular class. Week 9 Review Often, we will find ourselves using a class we didn’t write (for Random example, the RandomGenerator class). Overloading Craps When this happens, we are acting as a client of the class. Clients Packages Randomness When we write the code for a method, we are acting as an Seed implementer . Lab 9 Intervals Classes Clients and Implementers look at a class in different ways.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend