Review The Scanner has methods to check if

  • Slides: 9
Download presentation
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: exceptions, return values If something goes wrong, such as an invalid parameter value,

Review: exceptions, return values If something goes wrong, such as an invalid parameter value, Java allows us to "throw an exception" which will print an error message and terminate the program. if (n < 0) { throw new Illegal. Argument. Exception("negative n"); } 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 && Husky. Buck$ >= 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: scanner lookahead, break The Scanner has methods to check if the next token

Review: scanner lookahead, break 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; }. . . }