SlideShare a Scribd company logo
1 of 22
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)

More Related Content

What's hot

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

What's hot (8)

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

Similar to Nested For Loops and Class Constants in Java

Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJava_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJayveeCultivo
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdfapexelectronices01
 
Presentation1 computer shaan
Presentation1 computer shaanPresentation1 computer shaan
Presentation1 computer shaanwalia Shaan
 
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdf1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdfaparnatiwari291
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfkokah57440
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory 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.docxTrace 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.docxgtameka
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docxdavinci54
 
a)A.javapublic class A {   public static void main(String[] ar.pdf
a)A.javapublic class A {   public static void main(String[] ar.pdfa)A.javapublic class A {   public static void main(String[] ar.pdf
a)A.javapublic class A {   public static void main(String[] ar.pdfsudhinjv
 
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.pdfI 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.pdfarchanaemporium
 
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.pdfwrite 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.pdfarmcomputers
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014Supriya 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.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfarishmarketing21
 

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

Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
JAVA.pdf
JAVA.pdfJAVA.pdf
JAVA.pdf
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJava_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdf
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 
Presentation1 computer shaan
Presentation1 computer shaanPresentation1 computer shaan
Presentation1 computer shaan
 
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdf1.aimport java.util.Scanner;public class Diamond {   public st.pdf
1.aimport java.util.Scanner;public class Diamond {   public st.pdf
 
C++ programming pattern
C++ programming patternC++ programming pattern
C++ programming pattern
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Tugas algoritma fibonacci
Tugas algoritma   fibonacciTugas algoritma   fibonacci
Tugas algoritma fibonacci
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
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.docxTrace 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
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
a)A.javapublic class A {   public static void main(String[] ar.pdf
a)A.javapublic class A {   public static void main(String[] ar.pdfa)A.javapublic class A {   public static void main(String[] ar.pdf
a)A.javapublic class A {   public static void main(String[] ar.pdf
 
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.pdfI 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
 
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.pdfwrite 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
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014
 
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.pdfRefer 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
 

Recently uploaded

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Recently uploaded (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

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)