Programs 7 craps simulator design crapsRandom rand do

  • Slides: 11
Download presentation
Programs 7 – craps simulator - design craps(Random rand) { do 1 st roll

Programs 7 – craps simulator - design craps(Random rand) { do 1 st roll - process immediate win or loss do subsequent rolls to make point or hit 7 print final win/lose message and return }

Midterm 2 COS 160 Exam 2 100 points Open book, open notes Usual class

Midterm 2 COS 160 Exam 2 100 points Open book, open notes Usual class room Full class meeting time and over if needed Exam format: 4 or 5 problems with subproblems Writing programs, methods, code segments (not in any sort of method). Understanding programs, methods, code segments (not in any sort of method). Exam coverage: Chapters 3. 3 through Chapter 5. 3 But you will have to write and understand methods, for loops, etc. that were covered in the earlier sections and chapters. Open book, open notes, open labs, open homework. Ch 3, section 3. 3 Supplement 3 G (graphics) Chapter 4 (conditional execution) Chapter 5 (while loops & boolean) The test should mainly cover what we have done in class.

Review The Scanner has methods to check if the next token is what you

Review The Scanner has methods to check if the next token is what you want. has. Next. Int() has. Next. Double() has. Next() A sentinel loop uses a special value to indicate the end of the loop. while (input != -1) { The break statement will immediately break out of its enclosing loop. for (; ; ) {. . . if (number == -1) { break; }. . . }

Activity Write a method to print a table of the powers of a number.

Activity Write a method to print a table of the powers of a number. 10^0 10^1 10^2 10^3 10^4 10^5 10^6 10^7 10^8 10^9 = = = = = 1 10 100000 1000000000 stop at the highest representable value Here is the start of the method. Use a forever loop and break. public static void power. Table(int base) { int power = 0; int value = 1; Hint: 10^3 = 10 * 10

Review: Graphics Book's Drawing. Panel class: Drawing. Panel panel = new Drawing. Panel(sizex, sizey);

Review: Graphics Book's Drawing. Panel class: Drawing. Panel panel = new Drawing. Panel(sizex, sizey); Graphics g = panel. get. Graphics(); panel. set. Background(color); Graphics methods: g. draw. Line(x 1, y 1, x 2, y 2); g. draw. Rect(x, y, width, height); g. draw. Oval(x, y, width, height); g. fill. Rect(x, y, width, height); g. fill. Oval(x, y, width, height); g. draw. String(string, x, y); g. set. Color(color); g. set. Font(font); Color constructor: new Color(red, green, blue) Font constructor: new Font(name, style, size)

Review: if statement Single case if (gpa >= 3. 4) { msg = "You

Review: if statement Single case if (gpa >= 3. 4) { msg = "You made the Dean's List"; } 1 of 2 cases if (score >= 60) { grade = "Pass"; } else { grade = "Fail"; } 1 of many cases if (score >= 90){ grade = "A"; } else if (score >= 80){ grade = "B"; } else if (score >= 70){ grade = "C"; } else if (score >= 60){ grade = "D"; } else { grade = "F"; }

Review: Text Processing You can loop over ranges of characters. for (char c =

Review: Text Processing You can loop over ranges of characters. for (char c = 'A'; c <= 'Z'; c++) { System. out. print(c); } You can compare characters for alphabetical order. if (c 1 < c 2) { System. out. println(c 1 + " precedes " + c 2); } The Character class has useful static methods for operating on characters. if (Character. is. Letter(ch)) { System. out. println(ch + " is a letter"); }

Review: Return values You can return from anywhere within a method, but you must

Review: Return values You can return from anywhere within a method, but you must make sure that every path through the method returns a value. public static int index. Of(String s, char c) { for (int i = 0; i < s. length(); i++) { if (s. char. At(i) == c) { return i; } } return -1; // not found }

Review: while loop, Random while loop – used for indefinite loops where we don't

Review: while loop, Random while loop – used for indefinite loops where we don't know the loop bounds ahead of time public static int digit. Sum(int n) { int sum = 0; while (n > 0) { sum += n % 10; // add last digit to sum n = n / 10; // remove last digit } return sum; } The Random class is used to generate sequences of random numbers. Random rand = new Random(); int n = rand. next. Int(10); // random value from 0 to 9

Review: boolean type, do/while loop boolean is a primitive type to represent logical values,

Review: boolean type, do/while loop boolean is a primitive type to represent logical values, either true or false. You can create boolean variables, pass boolean parameters, return boolean values from methods, . . . boolean minor = (age < 21); if (!minor && wallet$ >= 3) { System. out. println("Here is your beer"); } The do/while loop executes statements repeatedly while a condition is true, testing it at the end of each repetition. do { x = rand. next. Int(); } while (x%2 != 1); System. out. printf("Your odd number is %d", x);

Review: break A sentinel loop uses a special value to indicate the end of

Review: break A sentinel loop uses a special value to indicate the end of the loop. while (input != -1) { The break statement will immediately break out of its enclosing loop. for (; ; ) {. . . if (number == -1) { break; }. . . }