/*A couple of announcemnets: * 1. Midterm marks posted last week, pick them up tomorrow in Tutorial 2. IMPORTANT: due to building maintenence, tomorrow's tutorial room change: WB342-> BA1210 Very Good tutorial for A2: practice StringTokenizer and loop! Also this week lab will be very useful! 3. A1 marking is almost done, will email the result by the end of this week. 4. Next week we have a QUIZ in tutorial Recall the if statement: if(boolean-expression) { here goes then-part } else { here goes else-part } Also recall while statement (loop/repetition): while (boolean-expression){ here goes the body } We also talked about: StringTokenizer Also, we mentioned how to print strings on screen using System.out.print(...) */ if (true) { System.out.println("Hi"); } if(34> 76) { System.out.println("Hi"); } if(34 < 76) { System.out.println("Hi"); } if(34 < 76 && 56 > 23) { System.out.println("Hi"); } //let's write a simple game program // the program generates a random number, // we try to guess it! import java.util.Random; // a random number generator Random r = new Random(); // set the number to guess to a random integer // between 0 and 99 r.nextInt(100) r.nextInt(100) r.nextInt(100) //let's write the class HighLow HighLow h = new HighLow(10); //let's start playing! guess a number! h.makeGuess(5) h.makeGuess(6) h.makeGuess(8) HighLow h = new HighLow(10); //let's add a GUI to our program! import javax.swing.JOptionPane; String g = JOptionPane.showInputDialog("Enter a number:"); g if (g == 100){ System.out.println("right!"); } //does not work, need to convert g into integer! Integer.parseInt("100") Integer.parseInt("13.3") Integer.parseInt("one") Integer.parseInt("85") g int guess = Integer.parseInt(g); JOptionPane.showMessageDialog(null, "Test! Can you see this message?"); //Ok, now let's write the class HighLowGUI HighLowGUI h1 = new HighLowGUI(10); //note:no need to pass a number to makeGuess anymore h1.makeGuess() h1.makeGuess() h1.makeGuess() h1 = new HighLowGUI(100000); h1.makeGuess() h1.makeGuess() //how many more times :( //let's use "while" loop! HighLowGUI2 h12 = new HighLowGUI2(10); h12.makeGuess() //continue asking until done h12 = new HighLowGUI2(10000); h12.makeGuess() //the same, continue until correct guess /* Also read "for" loop. 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 (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 infinite loop, why? for (int j=8; j>2; j++){ System.out.println(j); } /*javadoc, 2 new tags: @param to add comments for parameters of a method @return to add comments about the return value of a non-void method Syntax: @param pname1 comments @param pname2 comments ... @return commants */