SlideShare a Scribd company logo
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)
Ad

Recommended

Stars
Stars
Gurpreet singh
 
Introduction to python programming
Introduction to python programming
Rakotoarison Louis Frederick
 
The Ring programming language version 1.6 book - Part 49 of 189
The Ring programming language version 1.6 book - Part 49 of 189
Mahmoud Samir Fayed
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
Carlos V.
 
Dr.nouh part summery
Dr.nouh part summery
عمر محمد
 
Haskell 101
Haskell 101
Roberto Pepato
 
Data Structure
Data Structure
sheraz1
 
Star pattern programs in java Print Star pattern in java and print triangle ...
Star pattern programs in java Print Star pattern in java and print triangle ...
Hiraniahmad
 
How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012
Connor McDonald
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
Java Unit 1 Project
Java Unit 1 Project
Matthew Abela Medici
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750
richardchandler
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
Mvenkatarao
 
Earthworm Platformer Package( Unity Asset Store )
Earthworm Platformer Package( Unity Asset Store )
Oh DongReol
 
Java file
Java file
simarsimmygrewal
 
Java file
Java file
simarsimmygrewal
 
JAVA.pdf
JAVA.pdf
jyotir7777
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
apexelectronices01
 
Presentation1 computer shaan
Presentation1 computer shaan
walia Shaan
 
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
aparnatiwari291
 
C++ programming pattern
C++ programming pattern
Raushankumar550
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
kokah57440
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
archana singh
 
Tugas algoritma fibonacci
Tugas algoritma fibonacci
Melina Krisnawati
 
Sam wd programs
Sam wd programs
Soumya Behera
 
Trace the following part of codes step by step- Show exactly what it w.docx
Trace the following part of codes step by step- Show exactly what it w.docx
gtameka
 
DS LAB RECORD.docx
DS LAB RECORD.docx
davinci54
 

More Related Content

What's hot (8)

How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012
Connor McDonald
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
Java Unit 1 Project
Java Unit 1 Project
Matthew Abela Medici
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750
richardchandler
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
Mvenkatarao
 
Earthworm Platformer Package( Unity Asset Store )
Earthworm Platformer Package( Unity Asset Store )
Oh DongReol
 
How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012
Connor McDonald
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
minkminkk
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750
richardchandler
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
Mvenkatarao
 
Earthworm Platformer Package( Unity Asset Store )
Earthworm Platformer Package( Unity Asset Store )
Oh DongReol
 

Similar to Nested For Loops and Class Constants in Java (20)

Java file
Java file
simarsimmygrewal
 
Java file
Java file
simarsimmygrewal
 
JAVA.pdf
JAVA.pdf
jyotir7777
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
apexelectronices01
 
Presentation1 computer shaan
Presentation1 computer shaan
walia Shaan
 
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
aparnatiwari291
 
C++ programming pattern
C++ programming pattern
Raushankumar550
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
kokah57440
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
archana singh
 
Tugas algoritma fibonacci
Tugas algoritma fibonacci
Melina Krisnawati
 
Sam wd programs
Sam wd programs
Soumya Behera
 
Trace the following part of codes step by step- Show exactly what it w.docx
Trace the following part of codes step by step- Show exactly what it w.docx
gtameka
 
DS LAB RECORD.docx
DS LAB RECORD.docx
davinci54
 
a)A.javapublic class A {   public static void main(String[] ar.pdf
a)A.javapublic class A {   public static void main(String[] ar.pdf
sudhinjv
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 
write a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdf
armcomputers
 
.net progrmming part2
.net progrmming part2
Dr.M.Karthika parthasarathy
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
Supriya Radhakrishna
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
apexelectronices01
 
Presentation1 computer shaan
Presentation1 computer shaan
walia Shaan
 
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
aparnatiwari291
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
kokah57440
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
archana singh
 
Trace the following part of codes step by step- Show exactly what it w.docx
Trace the following part of codes step by step- Show exactly what it w.docx
gtameka
 
DS LAB RECORD.docx
DS LAB RECORD.docx
davinci54
 
a)A.javapublic class A {   public static void main(String[] ar.pdf
a)A.javapublic class A {   public static void main(String[] ar.pdf
sudhinjv
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 
write a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdf
armcomputers
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
Supriya Radhakrishna
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 
Ad

Recently uploaded (20)

Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Ad

Nested For Loops and Class Constants in Java

  • 1. Examples: Nested For Loops and 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
  • 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 * ** ***
  • 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
  • 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 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
  • 18. * *** ***** 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
  • 19. 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
  • 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 0 or less Different Values for SIZE Size 1 * *** * Size 2 * *** ***** ******* ********* ******* ***** *** * (nothing printed)