// APS101, Winter 2009: Lecture 19 (Feb. 26) // // Review: last time we wrote the Grades class, with two methods: // // Grades.passFail(-1) // Grades.passFail(200) // Grades.passFail(50) // Grades.passFail(49) // // Grades.letterGrade(-1) // Grades.letterGrade(200) // Grades.letterGrade(70) // // we also learned about StringTokenizer... // import java.util.*; // StringTokenizer st = new StringTokenizer("This is a test.") // st.hasMoreTokens() // st.countTokens() // st.nextToken() // st.nextToken() // st.nextToken() // st.nextToken() // st.hasMoreTokens() // // by default, spaces (and a few other things) are used as delimiters. // but what if we want to specify our own delimiters? // // StringTokenizer st = new StringTokenizer("Barack Obama; Hilary Clinton; John McCain", ";") // st.countTokens() // st.nextToken() // st.nextToken() // what's the problem here? // st.nextToken() // (there is an unnecessary space at the beginning!) // // to try to fix this, we can specify more than one delimiter... // StringTokenizer st = new StringTokenizer("Barack Obama; Hilary Clinton; John McCain", "; ") // st.countTokens() // st.nextToken() // st.nextToken() // st.nextToken() // // what's the problem now? it breaks "Barack Obama" into two tokens, "Barack" and "Obama" // you can also specify other delimiters. Ex: // StringTokenizer st = new StringTokenizer("Barack, Obama; Hilary, Clinton; John, McCain", ";,") // (hint for A2!) // // now, let's write a game where you have to guess a number that is generated randomly, // and the program will tell you whether your guess is too high or too low (or correct). // // we need to use the Random class... // import java.util.Random; // Random r = new Random() // r.nextInt(10) // // (take a look at HighLow.java) // // let's start playing... // HighLow h = new HighLow(10) // h.makeGuess(5) // h.makeGuess(3) // h.makeGuess(4) // HighLow h = new HighLow(10) // h.makeGuess(5) // // what if we want to make this game more user-friendly? // we can write a GUI for it - a Graphical User Interface // // import javax.swing.JOptionPane; // // String inp = JOptionPane.showInputDialog("Enter a number:") // int inp = JOptionPane.showInputDialog("Enter a number:") // doesn't work! // JOptionPane.showMessageDialog(null, "Test! Can you see this message?") // // (take a look at HighLowGUI.java) // // HighLowGUI h = new HighLowGUI(10); // note: no need to pass a number to makeGuess anymore // h.makeGuess() // h.makeGuess() // h.makeGuess() // // h = new HighLowGUI(100000); // h.makeGuess() // h.makeGuess() // how many more times?! // // let's use a "while" loop // (take a look at HighLowGUI2.java) <- we'll go over this next time // // Also, you can read about the "for" loop // (it will be useful to you for the Spy class in A2) // // Here is the syntax: /* for (initialization; condition; increment or update){ body; } Note: * The initialization expression initializes the loop; it's executed once, as the loop begins. * When the termination expression evaluates to false, the loop terminates. * The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value. */ // example, increasing for (int i = 0; i < 10; i++){ System.out.println(i); } // example, decreasing for (int j = 8; j > 2; j--){ System.out.println(j); } // the following causes an infinite loop... why? for (int j = 8; j > 2; j++){ System.out.println(j); }