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

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

In this chapter:

Number crunching

public 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

public 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

public 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

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

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

public 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

Defined field double bananaCalories = 100.0

Defined field double appleCalories = 95.0

Defined field double dietSodaCalories = 0.0

Defined field double cheeseburgerCalories = 500.0

bananaCalories + appleCalories +
                dietSodaCalories + cheeseburgerCalories = 695.0

Adding and removing JShell code

  1. Type height = 5.6
    ERROR: cannot find symbol
      symbol:   variable height
      location: class 
    Rejected height = 5.6
  2. Type double height
    ERROR: cannot find symbol
      symbol:   variable height
      location: class 
    Rejected height = 5.6
    
    Defined field double height
       
  3. Press Ctrl+Enter or Cmd+Enter again
    double height = 5.6
    
    field double height
       
  4. Erase double height
    double height = 5.6
       
  5. Look for the Trash Can icon
  6. Click the Trash Can icon
  7. Press Ctrl+Enter or Cmd+Enter again
    ERROR: cannot find symbol
      symbol:   variable height
      location: class 
    Rejected height = 5.6