ICS 111 - September 30
The following material will be used in upcoming labs, and is essential for you to know for the rest of the course and for the exam next week. Please review the following at home and make sure that you understand it fully. Please visit the following links and go through the tutorials. Test the ideas with short programs for your understanding. If you get stuck and have any questions, please contact one of your TAs for assistance.
Today's Topics:
1. While loops and Do-While loops:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html
// while example: // doesn't have to run once // since expression is evaluated // at the beginning of statements while (Boolean expression) { statement(s) } |
// do-while
example: |
2. For loops:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
// for loop structure (substitute code for italicized words)
for (initialization; termination; increment) {
statement
}Real code might resemble this:
for (int i = 1; i <= 10; i++) {
System.out.println("You're on iteration number " + i);
}This would print:
You're on iteration number 1
You're on iteration number 2
You're on iteration number 3
...
You're on iteration number 10
3. Java Tutorial's Summary of Control Flow Statements: (pay
attention to the 'loops' section)
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flowsummary.html
Code Examples:
Example 1: While1.java (simple while loop)
Example 2: WhileEx1.java (while loop reading user input, guessing game with 3 attempts)
Example 3: For1.java (for loop, using if / else to check for even / odd numbers)
Exercises:
Class exercise #1: You and a partner solve the following:
1. Write pseudo-code, and then Java code to do the following:
- Take the following program and modify it so that it:
- continues to ask the user for a number until it is in the range of 1-10
- will not crash or quit if the user doesn't type a number, or if the number is out of range
- Note: you will need a while loop to do this, and an if statement to check the range of the number upon successfully receiving an integer from the user
import java.io.*; public class While2 { public static void main (String arg[]) throws Exception{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String sNum = ""; int iNum = 0; boolean bGoodInput = false; // added this variable to help you // with your while loop and if / else try { System.out.println("Enter a number between 1 and 10: "); sNum = br.readLine(); iNum = Integer.parseInt(sNum); System.out.println("Your number was: " +iNum); } //try catch (NumberFormatException nfe){ System.out.println("You didn't type an integer!"); } //catch } //main } //class2. A group will be randomly selected to test their solution in front of the class- so be ready! You have 10 minutes to complete this exercise. Don't worry if you get lost- if you get chosen we will all help you out.
Class exercise #2: You and a partner solve the following:
1. Write pseudo-code, and then Java code to solve the following problem:
Take the completed program from the example above and modify it so that it will print "I love Java" the number of times that the user types in as their input (between 1 and 10). If they type 8, it'll print "I love Java" eight times, etc.
2. Again, a group will be randomly selected to test their solution in front of the class- so be prepared! You have 5 minutes to complete this exercise. Don't worry if you get lost- if you get chosen we will all help you out.