Java 2 For Dummies


by Barry Burd

 Errata: 

General tip
Chapter 4
Chapter 12

Be sure to check the Bonus chapters on the CD-ROM for extra topics not covered in the paper portion of the book. The Bonus chapters appear in the Table of Contents, but topics in the Bonus chapters may not appear in the book's index.


In the section entitled "The increment and decrement operators" in Chapter 4, please note that the decrement operator is a double minus sign --. The decrement operator is not the single minus sign - that's used in many of this section's paragraphs.

For instance, the end of the first full paragraph of that section should read as follows:
"The increment operators use double plus signs (++), and the decrement operator uses double minus signs (--). To see how they work ..."


In MakeChange.java (Listing 4-7), change two occurrences of the word total to the word whatsLeft:
public class MakeChange
{
   public static void main(String args[])
   {
      int quarters, dimes, nickels, cents;
      int whatsLeft, total;

      total = 248;
      quarters = total/25;
      whatsLeft = total%25;

      dimes = whatsLeft/10;
      whatsLeft = whatsLeft%10;

      nickels = whatsLeft/5;
      whatsLeft = whatsLeft%5;

      cents = whatsLeft;

      System.out.println("From " + total + " cents get");
      System.out.println(quarters + " quarters");
      System.out.println(dimes + " dimes");
      System.out.println(nickels + " nickels");
      System.out.println(cents + " cents");
   }
}
When you make the change, a run of the program looks like this:

Thanks to David Wurster


In UseAssignmentOperators.java (Listing 4-8), swap the two statements that multiply numberOfBunnies by 2, and add numberExtra to numberOfBunnies:
public class UseAssignmentOperators
{
   public static void main(String args[])
   {
      int numberOfBunnies;
      int numberExtra;
      numberOfBunnies=27;
      numberExtra=53;

      numberOfBunnies += 1;
      System.out.println(numberOfBunnies);

      numberOfBunnies += 5;
      System.out.println(numberOfBunnies);

      numberOfBunnies *= 2;
      System.out.println(numberOfBunnies);

      numberOfBunnies += numberExtra;
      System.out.println(numberOfBunnies);

      System.out.println(numberOfBunnies -= 7);

      System.out.println(numberOfBunnies = 100);
   }
}

Thanks to Bo Isaksson


The program InventoryD.java in Listing 12-4 should have one additional statement near the end. The additional statement is highlighted in bold below:
import java.text.NumberFormat;

public class InventoryD
{
   public static void main(String args[])
   {
      String numBoxesIn;
      int numBoxes;
      double boxPrice = 3.25;
      NumberFormat currency =
         NumberFormat.getCurrencyInstance();

      System.out.print("How many boxes do we have? ");
      numBoxesIn = DummiesIO.getString();

      try
      {
         if (numBoxesIn.equals(""))
            throw new NoInputException();

         numBoxes = Integer.parseInt(numBoxesIn);
         if (numBoxes < 0)
            throw new NegativeNumberException();

         System.out.print("The value is ");
         System.out.println
            (currency.format(numBoxes*boxPrice));
      }

      catch (NegativeNumberException e)
      {
         System.out.print(numBoxesIn);
         System.out.println("? That's impossible!");
      }

      catch (NumberFormatException e)
      {
         System.out.println("That's not a number.");
      }

      catch (Exception e)
      {
         System.out.print("Something went wrong, ");
         System.out.print("but I'm clueless about what ");
         System.out.println("it actually was.");
      }
      System.out.println("That's that.");
   }
}


class NegativeNumberException extends Exception
{
}

class NoInputException extends NumberFormatException
{
}
Thanks to the Java students from Toys R Us

Visit the book's page at Hungry Minds.

If you have any comments or questions for Barry Burd, please send email to j2fd@BurdBrain.com. Thank you.