10 - Looping 2

Experiments

Experiment 1

  • Create new class Star then save as Star.java
  • Add main() function inside the class
  • Because in the program we need input from user, add import Scanner at first line

    import java.util.Scanner;
    
  • Declare object Scanner as scanner.

    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 not println, 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 into i = 0? Explain your argument!

  • What happen if the condition i <= N changed into i > N? Explain your argument!

  • What happen if the statement i++ changed into i--? 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 value N is 5, the output program becomes:

    *****
    *****
    *****
    *****
    *****
    
  • Create class Square and save as Square.java

  • Add statement in first line to import Scanner library

    import java.util.Scanner;
    
  • Add main() function inside the class

  • Inside 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

  1. What happen if the initial value iOuter = 1 changed into iOuter = 0? Why?
  2. Revert back the initial value iOuter = 1. Then change the initial value i = 1 in inner loop into i = 0. Make an observation and explain!
  3. What the purpose of System.out.println();? Why we need this statement inside the program? What happen if the statement removed?
  4. Modify the program to display triangle of star!

    *
    **
    ***
    ****
    *****
    

Experiment 3

  • Create a new class Quiz and save as Quiz.java
  • import library Random and Scanner in the first line
      import java.util.Random;
      import java.util.Scanner;
    
  • Add main() function
  • Inside main() function, declare objects from Random and Scanner. 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

  1. Explain the flow of program above!
  2. Modify the program, add additional hint to guess a number. Display smaller or bigger based on the answer!

Assignment

  1. (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
    
  2. (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
    
  3. (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
    
  4. (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
    
  5. (PineTree) Create a program to display pine tree like below (N is 2 and 3)! The minimal value of N is 2.

             *
            ***
           *****
             *
            ***
      *    *****
     ***     *
      *     ***
     ***   *****
    
  6. (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
    

results matching ""

    No results matching ""