Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 6th Edition
by Barry Burd

Chapter 18: Creating Loops within Loops

In this chapter:

End of the road

The output is JAVA.

Seeing stars

The output is
*****
***
*****

Loop soup

The output is
5
321
4
321
3
321
2
321
1
321

Make some changes around here


public class LoopSoup {

    public static void main(String[] args) {
        int i = 5;
        int j;
        while (i > 0) {
            System.out.println(i);
            i--;
            j = 1;
            while (j < 4) {
                System.out.print(j);
                j++;
            }
            System.out.println();
        }

    }

}

Seeing stars

import java.util.Scanner;

public class SeeingStars1 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany, count;
        
        System.out.print("How many stars? ");
        howMany = keyboard.nextInt();
        
        count = 0;
        while (count < howMany) {
            System.out.print("*");
            count++;
        }
        
        System.out.println();
        
        keyboard.close();
    }

}
import java.util.Scanner;

public class SeeingStars2 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany, count;
        char reply;

        System.out.print("Do you want a row of stars? (y/n) ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);

        while (reply == 'y') {
            System.out.print("How many stars? ");
            howMany = keyboard.nextInt();

            count = 0;
            while (count < howMany) {
                System.out.print("*");
                count++;
            }

            System.out.println();
            System.out.println();
        
            System.out.print("Do you want a row of stars? (y/n) ");
            reply = keyboard.findWithinHorizon(".", 0).charAt(0);
        }

        keyboard.close();
    }

}

Apropos of nothing

Defined field String name = null

System.out.println(name.length())
You're trying to find the length of a string of characters referred to by the name variable. But the name variable has no value. So JShell doesn't display a length.
Compare this with what you get when you run System.out.println("abc".length()).

Mystery code

The first Mystery program displays this:
*****
*****
*****
*****
*****
The second Mystery program displays this:
*
**
***
****
*****

Draw a pattern

import java.util.Scanner;

public class DrawPattern1 {
    
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany;
        
        howMany = keyboard.nextInt();
        
        for (int i = 1; i <= howMany; i++) {
            System.out.print("-");
        }
        System.out.println();
        
        keyboard.close();
    }
}
import java.util.Scanner;

public class DrawPattern2 {
    
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany;
        
        howMany = keyboard.nextInt();
        
        for (int i = 1; i <= howMany; i++) {
            System.out.print("-");
        }
        System.out.println();
        
        for (int i = 1; i <= howMany - 1; i++) {
            System.out.print("-");
        }
        System.out.println();
        
        keyboard.close();
    }
}
import java.util.Scanner;

public class DrawPattern3 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany;

        howMany = keyboard.nextInt();

        for (int row = 1; row <= howMany; row++) {
            for (int i = 1; i <= howMany - row + 1; i++) {
                System.out.print("-");
            }
            System.out.println();
        }

        keyboard.close();
    }
}
import java.util.Scanner;

public class DrawPattern4 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int howMany;

        howMany = keyboard.nextInt();

        for (int row = 1; row <= howMany; row++) {
            for (int i = 1; i <= howMany - row; i++) {
                System.out.print("-");
            }
            System.out.println("/");
        }

        keyboard.close();
    }
}

Times table

public class TimesTable1 {

    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            System.out.print(i);
            System.out.print("\t");
        }
    }

}
public class TimesTable2 {

    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            System.out.print(2 * i);
            System.out.print("\t");
        }
    }

}
public class TimesTable3 {

    public static void main(String[] args) {
        for (int row = 1; row <= 9; row++) {
            for (int i = 1; i <= 9; i++) {
                System.out.print(row * i);
                System.out.print("\t");
            }
            System.out.println();
        }
    }

}
For this fourth part of this exercise, you might have to make some adjustments to the code depending on the number of spaces in your console's tab stop.
public class TimesTable4 {

    public static void main(String[] args) {

        for (int i = 1; i <= 9; i++) {
            System.out.print("\t");
            System.out.print(i);
        }
        System.out.println();

        System.out.print("\t");
        System.out.println("------------------------------------------------------------------");

        for (int row = 1; row <= 9; row++) {
            System.out.print(row);
            System.out.print("     |");
            for (int i = 1; i <= 9; i++) {
                System.out.print("\t");
                System.out.print(row * i);
            }
            System.out.println();
        }
    }

}