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

Chapter 6: Using the Building Blocks: Variables, Values, and Types

In this chapter:

Number crunching

class SnitSoft {

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

        amount = 155.21;
        amount = amount + 66.19;

        System.out.print("We will bill $");
        System.out.print(amount);
        System.out.println(" to your credit card.");
    }
}

Varying a variable

class SnitSoft {

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

        cash = 5.95;
        cash = cash + 25.00;

        System.out.print("We will bill $");
        System.out.print(cash);
        System.out.println(" to your credit card.");
    }
}

Using underscores

class SnitSoft {

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

        amount = 5.95;
        amount = amount + 1_000_000.00;

        System.out.print("We will bill $");
        System.out.print(amount);
        System.out.println(" to your credit card.");
    }
}

More information, please

class SnitSoft {

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

        amount = 5.95;
        
        System.out.print("The flash drive costs $");
        System.out.print(amount);
        System.out.println(".");
        
        amount = amount + 25.00;

        System.out.print("Shipping and handling costs $");
        System.out.print(25.00);
        System.out.println(".");
        
        System.out.print("We will bill $");
        System.out.print(amount);
        System.out.println(" to your credit card.");
    }
}
When you run this code, the output is
The flash drive costs $5.95.
Shipping and handling costs $25.0.
We will bill $30.95 to your credit card.
If you want the second line to end with 25.00 instead of 25.0, see Chapter 18.

Tip the parking attendant

import java.util.Scanner;

class Parking {

   public static void main(String args[]) {
      Scanner keyboard = new Scanner(System.in);
      double amount;

      System.out.print("What's the price to park your car? ");
      amount = keyboard.nextDouble();
      amount = amount + 2.00;

      System.out.print("You will pay $");
      System.out.print(amount);
      System.out.println(" in total.");

      keyboard.close();
   }
}

Double price

import java.util.Scanner;

class VersatileSnitSoft {

   public static void main(String args[]) {
      Scanner keyboard = new Scanner(System.in);
      double amount;

      System.out.print("What's the price of a flash drive? ");
      amount = keyboard.nextDouble();
      amount = amount + amount;

      System.out.print("We will bill $");
      System.out.print(amount);
      System.out.println(" to your credit card.");

      keyboard.close();
   }
}

Fun with JShell

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

jshell> double bananaCalories = 100.0
bananaCalories ==> 100.0

jshell> double appleCalories = 95.0
appleCalories ==> 95.0

jshell> double dietSodaCalories = 0.0
dietSodaCalories ==> 0.0

jshell> double cheeseburgerCalories = 500.0
cheeseburgerCalories ==> 500.0

jshell> bananaCalories + appleCalories +
   ...> dietSodaCalories + cheeseburgerCalories
$5 ==> 695.0

jshell> /exit
|  Goodbye