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

Chapter 15: The Old Runaround

In this chapter:

Narcissist’s code

import java.util.Scanner;

public class Narcissist {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String name;
        int howMany;
        
        System.out.print("What's your name? ");
        name = keyboard.nextLine();
        System.out.print("How many times would you like me to display your name? ");
        howMany = keyboard.nextInt();
        
        for (int i = 1; i <= howMany; i++) {
            System.out.println(name);
        }
        
        keyboard.close();
    }
}

British pounds to US dollars

public class PoundsToDollars {
    
    public static void main(String[] args) {
        
        System.out.println("Pounds   Dollars");
        
        for (int pounds = 1; pounds <= 9; pounds++) {
            System.out.print("  ");
            System.out.print(pounds);
            System.out.print("        ");
            System.out.println(pounds * 1.25);
        }
        
    }
}

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();
        }
    }

}

Do I hear an echo?

import java.util.Scanner;

public class Echo {

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

        do {
            System.out.print("Enter a number: ");
            number = keyboard.nextInt();
            System.out.println(number);
            System.out.print("Continue? (y/n) ");
            reply = keyboard.findWithinHorizon(".", 0).charAt(0);
            System.out.println();
        } while (reply != 'n');

        System.out.println("Done!");
        
        keyboard.close();
    }

}

Tally ho!

import java.util.Scanner;

public class Tally {

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

        do {
            System.out.print("Enter a non-zero number, or zero to quit: ");
            number = keyboard.nextInt();
            sum += number;
        } while (number != 0);

        System.out.println(sum);

        keyboard.close();
    }

}

Enumerate the possibilities

public class Possibilities {

    enum Hand {
        paper, scissors, stone
    }

    public static void main(String[] args) {
        for (Hand hand : Hand.values()) {
            System.out.println(hand);
        }
    }

}

Show your hands

public class ShowYourHands {

    enum Hand {
        paper, scissors, stone
    }

    public static void main(String[] args) {
        for (Hand hand1 : Hand.values()) {
            for (Hand hand2 : Hand.values()) {
                System.out.print(hand1);
                System.out.print(" ");
                System.out.println(hand2);
            }
        }
    }

}