Lec 5
Lec 5
1
Redundancy between loops
for (int j = 1; j <= 5; j++) {
System.out.print(j + "\t"); Output:
} 1 2 3 4 5
System.out.println();
2 4 6 8 10
for (int j = 1; j <= 5; j++) {
System.out.print(2 * j + "\t");
3 6 9 12 15
}
System.out.println();
4 8 12 16 20
for (int j = 1; j <= 5; j++) {
System.out.print(3 * j + "\t");
}
System.out.println();
for (int j = 1; j <= 5; j++) {
System.out.print(4 * j + "\t"){
}
System.out.println();
2
Nested loops
nested loop: A loop placed inside another loop.
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print((i * j) + "\t");
}
System.out.println(); // to end the line
}
Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
3
Nested for loop exercise
What is the output of the following nested for loops?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print("*");
}
System.out.println();
}
Output:
**********
**********
**********
**********
**********
**********
4
Nested for loop exercise
What is the output of the following nested for loops?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
Output:
*
**
***
****
*****
******
5
Nested for loop exercise
What is the output of the following nested for loops?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
Output:
1
22
333
4444
55555
666666
6
Nested for loop exercise
A table for the dots we start with
Could do a second table for the other dots
Output:
....1
...2.
..3..
.4...
5....
8
Building Java Programs
9
Drawing complex figures
Use nested for loops to produce the following output.
print 12 stars.
for (each of 5 lines) {
print a star. ************
* *
print 10 spaces. * *
print a star. * *
} * *
* *
print 12 stars. ************
12
2. Tables
A table for the top half:
Compute spaces and dots expressions from line number
1 6 6 0 0 #================#
| <><> |
2 4 4 4 4
| <>....<> |
3 2 2 8 8 | <>........<> |
|<>............<>|
4 0 0 12 12
|<>............<>|
| <>........<> |
| <>....<> |
| <><> |
#================#
13
3. Writing the code
Useful questions about the top half:
What methods? (think structure and redundancy)
Number of (nested) loops per line?
#================#
| <><> |
| <>....<> |
| <>........<> |
|<>............<>|
|<>............<>|
| <>........<> |
| <>....<> |
| <><> |
#================#
14
Partial solution
// Prints the expanding pattern of <> for the top half of the figure.
public static void topHalf() {
for (int line = 1; line <= 4; line++) {
System.out.print("|");
for (int space = 1; space <= (line * -2 + 8); space++) {
System.out.print(" ");
}
System.out.print("<>");
for (int dot = 1; dot <= (line * 4 - 4); dot++) {
System.out.print(".");
}
System.out.print("<>");
for (int space = 1; space <= (line * -2 + 8); space++) {
System.out.print(" ");
}
System.out.println("|");
}
}
15
Limitations of variables
Idea: Make a variable to represent the size.
Use the variable's value in the methods.
16
Variable scope
scope: The part of a program where a variable exists.
From its declaration to the end of the { } braces
A variable declared in a for loop exists only in that loop.
A variable declared in a method exists only in that method.
17
Scope implications
Variables without overlapping scope can have same name.
for (int i = 1; i <= 100; i++) {
System.out.print("/");
}
for (int i = 1; i <= 100; i++) { // OK
System.out.print("\\");
}
int i = 5; // OK: outside of loop's scope
18
Class constants
class constant: A value visible to the whole class.
value can only be set at declaration
value can't be changed while the program is running
Syntax:
public static final type name = value;
name is usually in ALL_UPPER_CASE
Examples:
public static final int DAYS_IN_WEEK = 7;
public static final double INTEREST_RATE = 3.5;
public static final int SSN = 658234569;
19
Adding a constant
public class Sign {
public static final int HEIGHT = 5;
public static void main(String[] args) {
drawLine();
drawBody();
drawLine();
}
public static void drawLine() {
System.out.print("+");
for (int i = 1; i <= HEIGHT * 2; i++) {
System.out.print("/\\");
}
System.out.println("+");
}
public static void drawBody() {
for (int line = 1; line <= HEIGHT; line++) {
System.out.print("|");
for (int spaces = 1; spaces <= HEIGHT * 4; spaces++) {
System.out.print(" ");
}
System.out.println("|");
}
}
}
20
Using a constant
Constant allows many methods to refer to same value:
public static final int SIZE = 4;
public static void main(String[] args) {
topHalf();
printBottom();
}
public static void topHalf() {
for (int i = 1; i <= SIZE; i++) { // OK
...
}
}
public static void bottomHalf() {
for (int i = SIZE; i >= 1; i--) { // OK
...
}
}
21