CSE 142 / AP Computer Science A Lab 1b: Loops ILHS - CSE 142 / AP Computer Science A Online Lab 1b: Loops ILHS - CSE 142 / AP Computer Science A Online Lab 1b: Loops Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp. Modified by Alec McTavish, with permission. Based on lab document created by Marty Stepp, Stuart Reges and Whitaker Brand Basic lab instructions These slides work best if you use the left & right arrow keys to navigate through them. (Slides are sometimes skipped when you click the forward/back buttons at the bottom of this screen) Talk to your classmates for help. You can even work on the lab with a partner if you like. Stuck? Confused? Have a question? Ask McT for help, or refer back to class notes. Complete as many problems as you can within the allotted time. You don't need to keep working on these exercises after you leave the lab. Occasionally, you'll see a check mark inside a circle to the right of a slide's page title. If you click on that check mark, you'll be taken to a question on Practice-It. Feel free to complete problems in any order. Today's lab Goals for today: trace and write for loops for repeating lines of code for loops A for loop repeats a group of statements a given number of times.
for (initialization; test; update) {
statement(s) to repeat;
}
Example:
for (int i = 1; i <= 10; i++) {
System.out.println("We're number one!");
}
Exercise : for loop practice Create a pop.java file in your Replit teams work area, and Copy and paste the following code to that file.
public class Pop {
public static void main(String[] args) {
for ( fill me in! ) {
System.out.println( fill me in! );
}
}
}
continued on the next slide... Exercise - continued Modify the code to produce the song 99 Bottles of Pop on the Wall:
99 bottles of pop on the wall, 99 bottles of pop
Take one down, pass it around, 98 bottles of pop on the wall
98 bottles of pop on the wall, 98 bottles of pop
Take one down, pass it around, 97 bottles of pop on the wall
... (output continues in the same pattern) ...
1 bottles of pop on the wall, 1 bottles of pop
Take one down, pass it around, 0 bottles of pop on the wall
Use the Output Comparison Tool to check your work. To copy output: Select it in the Console window, right-click, and choose Copy from the context menu that comes up. Exercise : What's the output? What output is produced by the following Java program? Write the output in the box on the right side.
public class OddStuff {
public static void main(String[] args) {
int number = 32;
for (int count = 1; count <= number; count++) {
System.out.println(number);
number = number / 2;
}
}
}
Output: 32
16
8
4
Exercise : What's the output? What output is produced by the following Java method?
static void stars() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j <= 20 - 2 * i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
(Do your best to figure it out without running the code... If you give up, copy and paste it into your IDE and run it!) Hint: Think of each inner loop from 1 to X as a loop that prints X copies of the given character. Exercise - answer Answer:
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********************
Exercise : for loop practice Copy and paste the following code into your IDE.
public class Triangle {
public static void main(String[] args) {
for (int line = 1; line <= 4; line++) {
for (int stars = 1; stars <= expression ; stars++) {
System.out.print("*");
}
System.out.println();
}
}
}
continued on the next slide... Exercise - fill in a table We want to produce the following output:
***********
********
*****
**
Fill in the table below indicating how many stars appear on each line of output. Line Stars 1 11 2 8 3 5 4 2 continued on the next slide... Exercise - complete the expression We need an expression for the number of stars on each line of this form: multiplier * line + constant First pick a multiplier (slope). Looking at the table, the line numbers go up by 1 each time, and the number of stars changes by how much each time? -3 Now pick a constant (y-intercept). Look at the table to see how many stars are on line 1. Given your multiplier, what constant do you need to use in the expression to get that number of stars for line 1? 14 Using your multiplier and constant, fill in the expression for this line of code in Visual Studio:
for (int stars = 1; stars <= expression ; stars++) {
Your program should now produce the correct output. You can verify with the Output Comparison Tool. Exercise : printing a design Write a Java program named NumberTriangle to produce the following output with nested for loops. Use a table to help you figure out the patterns of characters on each line.
-----1-----
----333----
---55555---
--7777777--
-999999999-
Line Dashes Numbers 1 5 1 2 4 3 3 3 5 4 2 7 5 1 9 dashes expression -1 * line + 6 numbers expression 2 * line + -1 Use the Output Comparison Tool to check your work.