Examples:
Nested For Loops and
Class Constants
AP Computer Science A
Building Java Programs, 3rd Edition
In this chapter:
● Following code using variable tables
● Writing nested for loops
● Using class constants
Variable
Tables
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
i j
1 1
2 1
2
3 1
2
3
*
**
***
Nested For
Loops
*****
*****
*****
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++)
{
System.out.print("*");
}
System.out.println();
}
*
**
***
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
***
**
*
for (int i = 3; i >= 1; i--) {
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
***
****
*****
for (int i = 3; i <= 5; i++) {
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
*****
****
***
for (int i = 5; i >= 3; i--) {
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
***
*******
***********
for (int i = 3; i <= 11; i += 4) {
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
***********
*******
***
for (int i = 11; i >= 3; i -= 4) {
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
****++
***++++
**++++++
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i - 1; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= 5 - i; j++) {
System.out.print("*");
}
for (int j = 1; j < 2 * i; j++) {
System.out.print("+");
}
System.out.println();
row #
(i)
# of
spaces
# of
stars
# of
pluses
1 0 4 2
2 1 3 4
3 2 2 6
i - 1 5 - i i * 2
Function Method
Class
Constants
public class ClassConstants {
public static final int SIZE = value;
//methods go here
}
Class Constants
*
***
*****
*******
*****
***
*
Size 4
row #
(i)
# of
spaces
# of
stars
1 3 1
2 2 3
3 1 5
4 0 7
5 1 5
6 2 3
7 3 1
*
***
*****
***
*
Size 3
row #
(i)
# of
spaces
# of
stars
1 2 1
2 1 3
3 0 5
4 1 3
5 2 1
*
***
*****
*******
public static void topHalf() {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
row #
(i)
#
spaces
#
stars
1 3 1
2 2 3
3 1 5
4 0 7
Size 4 Top Half
Size 4 Bottom Half
*****
***
*
row #
(i)
#
spaces
#
stars
1 1 5
2 2 3
3 3 1
public static void bottomHalf() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 7 - 2 * i; j++) {
System.out.print("*");
}
System.out.println();
}
}
4 - i 2 * i - 1
i 7 - 2 * i
*
***
*****
public static void topHalf() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
row #
(i)
#
spaces
#
stars
1 2 1
2 1 3
3 0 5
Size 3 Top Half
Size 3 Bottom Half
***
*
row #
(i)
#
spaces
#
stars
1 1 3
2 2 1
public static void bottomHalf() {
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 5 - 2 * i; j++) {
System.out.print("*");
}
System.out.println();
}
}i 5 - 2 * i
3 - i 2 * i - 1
public static void topHalf() {
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Putting SIZE into
topHalf()
public static void topHalf() {
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
*
***
*****
*
***
*****
*******
SIZE = 3
SIZE = 4
Putting SIZE into
bottomHalf()
***
*
*****
***
*
SIZE = 3
SIZE = 4
public static void bottomHalf() {
for (int i = 1; i <= SIZE - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * SIZE - 1) - 2 * i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void bottomHalf() {
for (int i = 1; i <= SIZE - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * SIZE - 1) - 2 * i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public class ClassConstantDiamonds {
public static final int SIZE = 3; //or 4
public static void main(String[] args) {
topHalf();
bottomHalf();
}
public static void topHalf() {
for (int i = 1; i <= SIZE; i++) {
for (int j = 1;
j <= SIZE - i; j++) {
System.out.print(" ");
}
for (int j = 1;
j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void bottomHalf() {
for (int i = 1; i <= SIZE - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1;
j <= (2 * SIZE - 1) - 2 * i;
j++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
Size 5
Size 0 or less
Different Values for SIZE
Size 1
*
***
*
Size 2
*
***
*****
*******
*********
*******
*****
***
*
(nothing printed)

Nested For Loops and Class Constants in Java

  • 1.
    Examples: Nested For Loopsand Class Constants AP Computer Science A Building Java Programs, 3rd Edition
  • 2.
    In this chapter: ●Following code using variable tables ● Writing nested for loops ● Using class constants
  • 3.
  • 4.
    for (int i= 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } i j 1 1 2 1 2 3 1 2 3 * ** ***
  • 5.
  • 6.
    ***** ***** ***** for (int i= 1; i <= 3; i++) { for (int j = 1; j <= 5; j++) { System.out.print("*"); } System.out.println(); }
  • 7.
    * ** *** for (int i= 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
  • 8.
    *** ** * for (int i= 3; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
  • 9.
    *** **** ***** for (int i= 3; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
  • 10.
    ***** **** *** for (int i= 5; i >= 3; i--) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
  • 11.
    *** ******* *********** for (int i= 3; i <= 11; i += 4) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
  • 12.
    *********** ******* *** for (int i= 11; i >= 3; i -= 4) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
  • 13.
    ****++ ***++++ **++++++ for (int i= 1; i <= 3; i++) { for (int j = 1; j <= i - 1; j++) { System.out.print(" "); } for (int j = 1; j <= 5 - i; j++) { System.out.print("*"); } for (int j = 1; j < 2 * i; j++) { System.out.print("+"); } System.out.println(); row # (i) # of spaces # of stars # of pluses 1 0 4 2 2 1 3 4 3 2 2 6 i - 1 5 - i i * 2 Function Method
  • 14.
  • 15.
    public class ClassConstants{ public static final int SIZE = value; //methods go here } Class Constants
  • 16.
    * *** ***** ******* ***** *** * Size 4 row # (i) #of spaces # of stars 1 3 1 2 2 3 3 1 5 4 0 7 5 1 5 6 2 3 7 3 1 * *** ***** *** * Size 3 row # (i) # of spaces # of stars 1 2 1 2 1 3 3 0 5 4 1 3 5 2 1
  • 17.
    * *** ***** ******* public static voidtopHalf() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4 - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } } row # (i) # spaces # stars 1 3 1 2 2 3 3 1 5 4 0 7 Size 4 Top Half Size 4 Bottom Half ***** *** * row # (i) # spaces # stars 1 1 5 2 2 3 3 3 1 public static void bottomHalf() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.print(" "); } for (int j = 1; j <= 7 - 2 * i; j++) { System.out.print("*"); } System.out.println(); } } 4 - i 2 * i - 1 i 7 - 2 * i
  • 18.
    * *** ***** public static voidtopHalf() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3 - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } } row # (i) # spaces # stars 1 2 1 2 1 3 3 0 5 Size 3 Top Half Size 3 Bottom Half *** * row # (i) # spaces # stars 1 1 3 2 2 1 public static void bottomHalf() { for (int i = 1; i <= 2; i++) { for (int j = 1; j <= i; j++) { System.out.print(" "); } for (int j = 1; j <= 5 - 2 * i; j++) { System.out.print("*"); } System.out.println(); } }i 5 - 2 * i 3 - i 2 * i - 1
  • 19.
    public static voidtopHalf() { for (int i = 1; i <= SIZE; i++) { for (int j = 1; j <= SIZE - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } } Putting SIZE into topHalf() public static void topHalf() { for (int i = 1; i <= SIZE; i++) { for (int j = 1; j <= SIZE - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } } * *** ***** * *** ***** ******* SIZE = 3 SIZE = 4
  • 20.
    Putting SIZE into bottomHalf() *** * ***** *** * SIZE= 3 SIZE = 4 public static void bottomHalf() { for (int i = 1; i <= SIZE - 1; i++) { for (int j = 1; j <= i; j++) { System.out.print(" "); } for (int j = 1; j <= (2 * SIZE - 1) - 2 * i; j++) { System.out.print("*"); } System.out.println(); } } public static void bottomHalf() { for (int i = 1; i <= SIZE - 1; i++) { for (int j = 1; j <= i; j++) { System.out.print(" "); } for (int j = 1; j <= (2 * SIZE - 1) - 2 * i; j++) { System.out.print("*"); } System.out.println(); } }
  • 21.
    public class ClassConstantDiamonds{ public static final int SIZE = 3; //or 4 public static void main(String[] args) { topHalf(); bottomHalf(); } public static void topHalf() { for (int i = 1; i <= SIZE; i++) { for (int j = 1; j <= SIZE - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } } public static void bottomHalf() { for (int i = 1; i <= SIZE - 1; i++) { for (int j = 1; j <= i; j++) { System.out.print(" "); } for (int j = 1; j <= (2 * SIZE - 1) - 2 * i; j++) { System.out.print("*"); } System.out.println(); } } }
  • 22.
    * Size 5 Size 0or less Different Values for SIZE Size 1 * *** * Size 2 * *** ***** ******* ********* ******* ***** *** * (nothing printed)