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

Chapter 4: Making the Most of Variables and Their Values

In this chapter:

Add thousands separators

You get error messages. In Java, you can't use your country's standard thousands separators to represent numbers in the code.

Change 1000000.00 to 1_000_000.00

The code runs correctly. In Java, you can use underscores as thousands separators to represent numbers in the code.

amountInAccount = $50.22

You get an error message. In Java, you can't use currency symbols to represent numbers in the code.

Change all print statements to println statements

You have $
1000050.22
 in your account.

Now you have even more! You have 2000000.00 in your account.

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.");
    }
}

Number of anniversaries

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

Using JShell with / and +

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"

Predict the code's output with ++ and --

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

Using JShell with ++ and --

jshell> int i = 8
i ==> 8

jshell> i++
$9 ==> 8

jshell> i
i ==> 9

jshell> i
i ==> 9

jshell> i++
$12 ==> 9

jshell> i
i ==> 10

jshell> ++i
$14 ==> 11

jshell> i
i ==> 11

Predict the code's output with += and *=

42
45
22