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

Chapter 7: Numbers and Types

In this chapter:

Make it and break it

When I run the code, here's what happens:
How many gumballs? How many kids? 80.5 6
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at KeepingMoreKidsQuiet.main(KeepingMoreKidsQuiet.java:12)
I get an InputMismatchException because keyboard.nextInt() expects me to type an int value, and 80.5 isn't an int value.

Break it again

When I run the code, here's what happens:
How many gumballs? How many kids? "80" "6"
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at KeepingMoreKidsQuiet.main(KeepingMoreKidsQuiet.java:12)
I get an InputMismatchException because keyboard.nextInt() expects me to type an int value, and "80", with quotation marks, isn't an int value. (In fact, "80" is a string of characters -- the digit character 8 followed by the digit character 0. More on that in Chapter 18.)

A tiny adding machine

import java.util.Scanner;

class AddingMachine {

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

        System.out.print("Enter two numbers: ");
        firstNumber = keyboard.nextInt();
        secondNumber = keyboard.nextInt();

        sum = firstNumber + secondNumber;
        System.out.print("The sum is ");
        System.out.print(sum);
        System.out.println(".");

        keyboard.close();
    }
}

Java arithmetic

|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> 10 / 3
$1 ==> 3

jshell> 10 % 3
$2 ==> 1

jshell> 3 / 10
$3 ==> 0

jshell> 3 % 10
$4 ==> 3

jshell> 8 * 3 + 4
$5 ==> 28

jshell> 4 + 8 * 3
$6 ==> 28

jshell> 8 * (3 + 4)
$7 ==> 56

jshell> 34 % 5 - 2 * 2 + 21 / 5
$8 ==> 4

jshell> /exit
|  Goodbye

Variable values

|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> int a = 8
a ==> 8

jshell> int b = 3
b ==> 3

jshell> int c = b / a
c ==> 0

jshell> int d = a / b
d ==> 2

jshell> int e = a % b
e ==> 2

jshell> int f = 5 + e * d - 2
f ==> 7

jshell> /exit
|  Goodbye

Hiring a plumber

import java.util.Scanner;

class Plumber {

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

        System.out.print("How many hours does the plumber work? ");
        hours = keyboard.nextInt();

        charge = 75 + hours * 125;
        System.out.print("The plumber charges $");
        System.out.print(charge);
        System.out.println(".");

        keyboard.close();
    }
}

Making change again

import java.util.Scanner;

class MakeChange {

   public static void main(String args[]) {
      Scanner keyboard = new Scanner(System.in);
      int quarters, dimes, nickels, cents;
      int whatsLeft, total;
      int dollars;

      System.out.print("How many dollars? How many cents? ");
      dollars = keyboard.nextInt();
      cents = keyboard.nextInt();
      total = dollars * 100 + cents;

      System.out.println();
      System.out.println("From $" + dollars + "." + cents + " you get");
      
      quarters = total / 25;
      whatsLeft = total % 25;

      dimes = whatsLeft / 10;
      whatsLeft = whatsLeft % 10;

      nickels = whatsLeft / 5;
      whatsLeft = whatsLeft % 5;

      cents = whatsLeft;

      System.out.println(quarters + " quarters");
      System.out.println(dimes + " dimes");
      System.out.println(nickels + " nickels");
      System.out.println(cents + " cents");

      keyboard.close();
   }
}

How tall am I?

class HowTall {
    
    public static void main(String[] args) {
        double height;
        double inches;
        
        height = 5.5;
        inches = height * 12;        
        
        System.out.print("Barry is ");
        System.out.print(inches);
        System.out.println(" inches tall.");
    }
}
import java.util.Scanner;

class HowTall {
    
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        double height;
        double inches;
        
        System.out.print("How tall are you? (Example: 5.5 for five and half feet) ");
        height = keyboard.nextDouble();
        inches = height * 12;        
        
        System.out.print("You're ");
        System.out.print(inches);
        System.out.println(" inches tall.");
        
        keyboard.close();
    }
}
import java.util.Scanner;

class HowTall2 {
    
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int feet;
        int inches;
        int height;

        System.out.print("How many feet? How many inches? ");
        feet = keyboard.nextInt();
        inches = keyboard.nextInt();
        
        height = feet * 12 + inches;
        
        System.out.print("You're ");
        System.out.print(height);
        System.out.println(" inches tall.");
        
        keyboard.close();
    }
}

How many anniversaries?

import java.util.Scanner;

class Anniversaries {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int years;
        
        System.out.print("How many years? ");
        years = keyboard.nextInt();
        
        System.out.print("Number of anniversaries: ");
        System.out.println(years / 4);
        
        keyboard.close();
    }
}

Explore preincrement and postincrement using JShell

|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> int i = 8
i ==> 8

jshell> i++
$2 ==> 8

jshell> i
i ==> 9

jshell> i
i ==> 9

jshell> i++
$5 ==> 9

jshell> i
i ==> 10

jshell> ++i
$7 ==> 11

jshell> i
i ==> 11

jshell> /exit
|  Goodbye

Explore preincrement and postincrement in a Java program

10
10
8
9
9
8

Experiment with assignment operators

42
45
22

Making change yet again

import java.util.Scanner;

class MakeChange {

   public static void main(String args[]) {
      Scanner keyboard = new Scanner(System.in);
      int quarters, dimes, nickels, cents;
      int whatsLeft, total;

      System.out.print("How many cents do you have? ");
      total = keyboard.nextInt();

      quarters = total / 25;
      whatsLeft = total % 25;

      dimes = whatsLeft / 10;
      whatsLeft %= 10;

      nickels = whatsLeft / 5;
      whatsLeft %= 5;

      cents = whatsLeft;

      System.out.println();
      System.out.println("From " + total + " cents you get");
      System.out.println(quarters + " quarters");
      System.out.println(dimes + " dimes");
      System.out.println(nickels + " nickels");
      System.out.println(cents + " cents");

      keyboard.close();
   }
}