Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 6th 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;

public class AddingMachine {

    public static void main(String[] args) {
        var 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

10 / 3 = 3

10 % 3 = 1

3 / 10 = 0

3 % 10 = 3

8 * 3 + 4 = 28

4 + 8 * 3 = 28

8 * (3 + 4) = 56

34 % 5 - 2 * 2 + 21 / 5 = 4

Variable values

Defined field int a = 8

Defined field int b = 3

Defined field int c = 0

Defined field int d = 2

Defined field int e = 2

Defined field int f = 7

Hiring a plumber

import java.util.Scanner;

public class Plumber {

    public static void main(String[] args) {
        var 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;

public class MakeChange {

   public static void main(String[] args) {
      var 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?

public 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;

public class HowTall {
    
    public static void main(String[] args) {
        var 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;

public class HowTall2 {
    
    public static void main(String[] args) {
        var 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;

public class Anniversaries {

    public static void main(String[] args) {
        var 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

Since the book's publication, I've found that IntelliJ's JShell console insists on your ending certain expressions with semicolons. Type this:
int i = 8
i++
i;
i;
i++
i;
++i
i;
Then the response is:
field int i = 8

i++ = 8

int i = 9

int i = 9

i++ = 9

int i = 10

++i = 11

int i = 11

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;

public class MakeChange {

   public static void main(String[] args) {
      var 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();
   }
}