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
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 ...
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?
for (int laps = 1; laps <= 100; laps++) drive around the track again
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.
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) ;
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--)
*To think about:
What if you want only the even numbers?
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); } }
*To think about:
If I remove the last statement, how many times will "Hello" be printed
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
for (int i = 0; i < phrase.length(); i++) { String ch = phrase.substring(i,i + 1); System.out.println(ch); }
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)
for (int i = phrase.length() - 1; i >= 0; i--)
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.
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?
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)