10 - Looping 2
Experiments
Experiment 1
- Create new class
Star
then save asStar.java
- Add
main()
function inside the class Because in the program we need input from user, add import
Scanner
at first lineimport java.util.Scanner;
Declare object
Scanner
asscanner
.Scanner scanner = new Scanner(System.in);
In the next line, ask user to input the value of
N
System.out.print("Input N: "); int N = scanner.nextInt();
Add basic syntax loop using keyword
for
for (int i = 1; i <= N; i++) { System.out.print("*"); }
Note: use
print
notprintln
, because we need to display in single line without new line.Compile then execute the program!
Make an observation, then check your result of program! Ensure your output the program like below.
Input N: 5 *****
Questions
What happen if the initial value
i = 1
changed intoi = 0
? Explain your argument!What happen if the condition
i <= N
changed intoi > N
? Explain your argument!What happen if the statement
i++
changed intoi--
? Explain your argument!
Experiment 2
In this experiment, we'll make nested loop program. We want to display square of star based on
N
value. If the valueN
is 5, the output program becomes:***** ***** ***** ***** *****
Create class
Square
and save asSquare.java
Add statement in first line to import
Scanner
libraryimport java.util.Scanner;
Add
main()
function inside the classInside
main()
function, add same logic in the previous experiment.Scanner scanner = new Scanner(System.in); System.out.print("Input N: "); int N = scanner.nextInt(); for (int i = 1; i <= N; i++) { System.out.print("*"); }
Compile and then execute the program. Ensure the output is equals like the previous experiment.
Check looping syntax in the previous step. The syntax display * as many as N times. We need to repeat that process, so one more looping is needed.
for (int iOuter = 1; iOuter <= N; iOuter++) { for (int i = 1; i <= N; i++) { System.out.print("*"); } System.out.println(); }
- Save, compile then execute the program!
- Make an observation, test the program!
Input N: 5 ***** ***** ***** ***** *****
Questions
- What happen if the initial value
iOuter = 1
changed intoiOuter = 0
? Why? - Revert back the initial value
iOuter = 1
. Then change the initial valuei = 1
in inner loop intoi = 0
. Make an observation and explain! - What the purpose of
System.out.println();
? Why we need this statement inside the program? What happen if the statement removed? Modify the program to display triangle of star!
* ** *** **** *****
Experiment 3
- Create a new class
Quiz
and save asQuiz.java
- import library
Random
andScanner
in the first lineimport java.util.Random; import java.util.Scanner;
- Add
main()
function Inside
main()
function, declare objects fromRandom
andScanner
.Random
class used to get a generate number.Random random = new Random(); Scanner input = new Scanner(System.in);
In the next line, add syntaxes below
char menu = 'y'; do { int number = random.nextInt(10) + 1; boolean success = false; do { System.out.print("Guess a number (1 - 10): "); int answer = input.nextInt(); input.nextLine(); success = (answer == number); } while(!success); System.out.print("Do you want to try again (Y/y)?"); menu = input.findInLine(".").charAt(0); input.nextLine(); } while (menu == 'y' || menu == 'Y');
Note: Statement
input.nextLine()
used to skip new line input.Compile then execute the program!
Questions
- Explain the flow of program above!
- Modify the program, add additional hint to guess a number. Display smaller or bigger based on the answer!
Assignment
(NumberTriangle) Create a program to display Number Triangle like below (row's value is 2 and 4)! The minimal of row's value is 1.
1 12 1 123 12 1234
(HourGlass) Create a program to display a hourglass based on the value of N! The minimal value of N is 2.
********* ******* ***** *** * *** *** ***** * ******* *** ********* N=2 N=4
(SquaredNumber) Create a program to display square like below (row is 3 and 5)! The minimal value of row is 3.
55555 5 5 333 5 5 3 3 5 5 333 55555
(OppositeNumber) Create a program to display opposite number like figure below! Row's value between 3 and 6.
123456 654321 123456 123 654321 321 123456 123 654321
(PineTree) Create a program to display pine tree like below (N is 2 and 3)! The minimal value of N is 2.
* *** ***** * *** * ***** *** * * *** *** *****
(PrimeNumber) Display the prime number as many as N! The minimal value of N is 1.
N = 4 2 3 5 7 N = 1 2