Lesson 11 – 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

Exam

Concept: Loops

Concept: while loop

loop 100 times

initialize a counter to 1
while counter <= 100
    drive around the track again
    add 1 to the counter. (also called incrementing)  //very important and easy to  forget

Activity - Simple while loop

Record your participation in Piazza clicker question Lesson11 Q1

Using Bluej, write a program with a while loop that prints powers of 2 from 1 to 5 (inclusive) all on one line separated by a space. Create a class, put a main method in it, then write your code. (If you are stuck, think about using Math.pow)

21 22 23 24 ... yields output 2 4 8 16 ...

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it

Here is a starter project

*To think about:
What if you want the answer as an int?
What if you only wanted the odd powers 1, 3, 5, ... 9?

Concept: for loop

 

Activity - for loop

Record your participation in Piazza clicker question Lesson11 Q2

Change the the first problem to use a for loop to print the powers of 2 from 1 to 5 (inclusive). You can put the code in the same class or make a new one.

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it

 

Activity - declaration of variable in a loop

Record your participation in Piazza clicker question Lesson11 Q3

What would this code segment print?

... 
int i = 1;  
while (i <= 5)
{
   double power = Math.pow(2, i);
   System.out.print(power + " ");
   i++;
   ...
}
System.out.println("The last power is " + power) ;
  1. 2 4 8 16 32 The last power is 32
  2. 2 4 8 16 32 The last power is 64
  3. Nothing is printed. The code has a syntax error
  4. Nothing is printed. The code throws an exception

Activity - for loop with decrement

Record your participation in Piazza clicker question Lesson11 Q4

Write a program to print the numbers starting at 10 and going backwards to 1 (inclusive). Use decrement operator (i--)

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it

*To think about:
What if you want only the even numbers?

Activity - for loop control variable scope

Record your participation in Piazza clicker question Lesson11 Q5

What is the value of i printed in the last line of the code segment?

public class HelloPrinter
{
   public static void main(String[] args)
   {
      for (int i = 1; i < 5; i++)
      {
          System.out.println("Hello");
      }
      System.out.println(i);
   }
}
  1. 5
  2. 4
  3. Nothing. There is syntax error
  4. Nothing. The code throws an exception

*To think about:
If I remove the last statement, how many times will "Hello" be printed

Concept: processing a string with a loop

A for loop is ideal for picking up one character in a string at a time and doing something with it.

Let's write a code segment to print the letters of a string phrase one character per line

 

Activity - processing a string with a loop

Record your participation in Piazza clicker question Lesson11 Q6

Complete the code to count the number of o's in a phrase. For this to work, you have to initialize a variable to count the o's before the loop. That is done for you in this starter(string_processing.zip)

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it

 

Concept: processing a string in reverse with a loop


Activity - processing a string in reverse with a loop

Record your participation in Piazza clicker question Lesson11 Q7

Write a program to read a string from the user and print the characters of the string in reverse order one character per line.

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it

Here is input_skeleton2

*To think about:
What if you were writing a method and wanted to return the string in reverse order. What could you do?

Activity - processing a string in reverse with a loop

Record your participation in Piazza clicker question Lesson11 Q8

Complete a class called Words  that has one method reverse which will return the string in reverse order. Test in the Bluej workbench.

Starter project is here (words.zip)

  1. I got it, no real problem
  2. I got it, but it was hard
  3. I couldn't do it