Solutions to the Try It Out Exercises in Java For Dummies, 8th Edition
by Barry Burd

Chapter 4: Making the Most of Variables and Their Values

In this chapter:

Number Format Experiments

How to Display Values

When you change print to println, the output becomes
You have $
1000050.22
 in your account.

To make the program display "Now you have even more ..."
public class Millionaire {

  public static void main(String[] args) {
    double amountInAccount;

    amountInAccount = 50.22;
    amountInAccount = amountInAccount + 1000000.00;

    System.out.print("You have $");
    System.out.print(amountInAccount);
    System.out.println(" in your account.");

    System.out.println("Now you have even more! You have 2000000.00 in your account.");
    }
}

Take a Flying Leap

public class Main {

    public static void main(String[] args) {
        int years;
        int anniversaries;

        years = 4;
        anniversaries = years / 4;
        System.out.println("Number of anniversaries: " + anniversaries);

        years = 7;
        anniversaries = years / 4;
        System.out.println("Number of anniversaries: " + anniversaries);

        years = 8;
        anniversaries = years / 4;
        System.out.println("Number of anniversaries: " + anniversaries);
    }
}

Smooth Operators

jshell> 5 / 4
$1 ==> 1

jshell> 5 / 4.0
$2 ==> 1.25

jshell> 5.0 / 4
$3 ==> 1.25

jshell> 5.0 / 4.0
$4 ==> 1.25

jshell> "5" + "4"
$5 ==> "54"

jshell> 5 + 4
$6 ==> 9

jshell> " " + 5 + 4
$7 ==> " 54"

Prognostication Game

10
10
8
9
9
8
20
41
Consider the last six statements in the main method:
  System.out.println(i); // The value of i is 8
  i++;                   // The value of i becomes 9
  i = i++ + ++i;         // See the "Statements and Expressions" sidebar...
       9     11
                         //     As an expression, the value of i++ is 9. As a statement, i++ adds 1 to i, making i be 10.
                         //     Then, because i has become 10,
                         //        As an expression, the value of ++i is 11. As a statement, ++1 adds 1 to i, but that doesn't matter because it's only temporary.
                         //     As an expression, the value of i++ + ++i is 9 + 11, which is 20. So i is assigned the value 20.
  System.out.println(i); //  Prints 20
  i = i++ + i++;
       20    21
                         //     As an expression, the value of the leftmost is i++ is 20. As a statement, the leftmost i++ adds 1 to i, making i be 21.
                         //     Then, because i has become 21,
                         //        As an expression, the value of the rightmost i++ is 21. As a statement, the rightmost i++ adds 1 to i, but that doesn't matter because it's only temporary.
                         //     As an expression, the value of i++ + i++ is 20 + 21, which is 41. So i as assigned the value 41.
  System.out.println(i); //  Prints 41

See Plus Plus

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++
$8 ==> 22

jshell> i++ + i
$9 ==> 25

The Opertion is a Success

42
45
22