Lesson 12 – More Loops

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Concept: do Loop

Activity - Getting valid input

Record your participation in Piazza clicker question Lesson12 Q1

Supply the code to get a positive integer from the keyboard. Keep prompting until the input is positive. Use input_practice.zip as a starter file.

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start

 

Concept: do loop

 

Activity - reading loop code

Record your participation in Piazza clicker question Lesson 12 Q2

What does the following loop print? Make a trace table
     int n = 1;
     while (n < 30)
     {
         n = 2 * n;
         System.out.print(n + " ");
     }
 
  1. 2 4 8 16
  2. 2 4 8 16 32
  3. 1 2 4 8 16
  4. 1 2 4 8 16 32

Activity - Problem Solving: Hand-Tracing

Record your participation in Piazza clicker question Lesson 12 Q3

What value is displayed by this code? Trace the code and make a trace table

Record the values of "n", "sum", and "digit" on a piece of paper or a txt file

int n = 2016;
int sum = 0;
while (n > 0)
{
    int digit = n % 10;
    sum = sum + digit;
    n = n / 10;
}
System.out.println(sum);

Activity - Hand-Tracing

Record your participation in Piazza clicker question Lesson 12 Q4

Concept - Sentinel value

 

Concept - Sentinel value

 

 

Concept - Sentinel value

Activity - Using Q as a sentinel value

Record your participation in Piazza clicker question Lesson12 Q5

What would happen is you entered X in the program in the last slide to input temperatures.

  1. There would be a syntax error
  2. The code will throw an exception
  3. The loop will terminate
  4. Something else would happen

 

Activity - Using Q as a sentinel value

Record your participation in Piazza clicker question Lesson12 Q6

We are throwing a dart at a number line. (numbers can be positive, negative or zero doubles) Write a program to ask the user for input to represent the result of the throw. Find the sum of the numbers and print it. Use Q as a sentinel value. You can use input_practice.zip again as a starter

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start

 

Activity - Using Q as a sentinel value

Record your participation in Piazza clicker question Lesson12 Q7

In the previous activity, you found the sum of inputs using Q as the sentinel value. Now modify the program to also find and print the average.

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it in the time allowed
  4. I didn't know where to start