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

Chapter 10: Putting Variables and Methods Where They Belong

In this chapter:

Delete the Hankees.txt file

The program crashes. You get an error message indicating that the file hasn't been found.

Your first MensClothingItem class

public enum MensClothingKind {
  SHIRT, PANTS, JACKET, OVERCOAT, NECKTIE, SHOES
}

public class MensClothingItem {
  private MensClothingKind kind;
  private String name;

  public MensClothingItem(MensClothingKind kind, String name) {
    this.kind = kind;
    this.name = name;
  }

  public MensClothingKind getKind() {
    return kind;
  }

  public void setKind(MensClothingKind kind) {
    this.kind = kind;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;

@SuppressWarnings("serial")
public class ClothingFrame extends JFrame {

  public ClothingFrame() {
    MensClothingItem item;

    item = new MensClothingItem(MensClothingKind.SHIRT, "Design #7");
    addItemInfo(item);
    item = new MensClothingItem(MensClothingKind.PANTS, "Design #5");
    addItemInfo(item);
    item = new MensClothingItem(MensClothingKind.JACKET, "Design #3");
    addItemInfo(item);
    item = new MensClothingItem
                             (MensClothingKind.OVERCOAT, "Design #8");
    addItemInfo(item);
    item = new MensClothingItem(MensClothingKind.NECKTIE, "Design #9");
    addItemInfo(item);
    item = new MensClothingItem(MensClothingKind.SHOES, "Design #4");
    addItemInfo(item);

    setTitle("Men's Clothing");
    setLayout(new GridLayout(6, 2, 20, 3));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
  }

  void addItemInfo(MensClothingItem item) {
    add(new JLabel("  " + item.getKind()));
    add(new JLabel(item.getName()));
  }
}


public class Main {

  public static void main(String[] args) {
    new ClothingFrame();
  }

}

Your first PlayingCard class

public enum Suit {
  CLUBS, DIAMONDS, HEARTS, SPADES
}

public class PlayingCard {
  private Suit suit;
  private int number;

  public PlayingCard(Suit suit, int number) {
    this.suit = suit;
    this.number = number;
  }

  public String getString() {
    String numberString;
    switch (number) {
    case 11:
      numberString = "Jack";
      break;
    case 12:
      numberString = "Queen";
      break;
    case 13:
      numberString = "King";
      break;
    case 1:
      numberString = "Ace";
      break;
    default:
      numberString = Integer.toString(number);
      break;
    }

    return numberString + " of " + suit;
  }

  public Suit getSuit() {
    return suit;
  }

  public void setSuit(Suit suit) {
    this.suit = suit;
  }

  public int getNumber() {
    return number;
  }

  public void setNumber(int number) {
    this.number = number;
  }
}


public class Main {

  public static void main(String[] args) {
    PlayingCard card1 = new PlayingCard(Suit.SPADES, 13);
    PlayingCard card2 = new PlayingCard(Suit.HEARTS, 1);
    PlayingCard card3 = new PlayingCard(Suit.DIAMONDS, 3);

    System.out.println(card1.getString());
    System.out.println(card2.getString());
    System.out.println(card3.getString());
  }

}

A MensClothingItem subclass with designer, color and cost

import java.awt.Color;

public class MensClothingItemPlus extends MensClothingItem {
  public static String designer = "Dummies House of Fashion";
  private Color color;
  private double cost;

  public MensClothingItemPlus(MensClothingKind kind, String name,
                              Color color, double cost) {
    super(kind, name);
    this.color = color;
    this.cost = cost;
  }

  public Color getColor() {
    return color;
  }

  public void setColor(Color color) {
    this.color = color;
  }

  public double getCost() {
    return cost;
  }

  public void setCost(double cost) {
    this.cost = cost;
  }
}

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class ClothingFrame extends JFrame {

  public ClothingFrame() {
    MensClothingItemPlus item;

    add(new JLabel(MensClothingItemPlus.designer));
    add(new JLabel(" "));
    add(new JLabel(" "));
    add(new JLabel(" "));

    double totalCost = 0;

    item = new MensClothingItemPlus
        (MensClothingKind.SHIRT, "Design #7", Color.GRAY, 50.00);
    addItemInfo(item);
    totalCost += item.getCost();

    item = new MensClothingItemPlus
        (MensClothingKind.PANTS, "Design #5", Color.BLACK, 70.00);
    addItemInfo(item);
    totalCost += item.getCost();

    item = new MensClothingItemPlus
        (MensClothingKind.JACKET, "Design #3", Color.BLACK, 70.00);
    addItemInfo(item);
    totalCost += item.getCost();

    item = new MensClothingItemPlus
        (MensClothingKind.OVERCOAT, "Design #8", Color.GRAY, 100.00);
    addItemInfo(item);
    totalCost += item.getCost();

    item = new MensClothingItemPlus
        (MensClothingKind.NECKTIE, "Design #9", Color.BLUE, 30.00);
    addItemInfo(item);
    totalCost += item.getCost();

    item = new MensClothingItemPlus
        (MensClothingKind.SHOES, "Design #4", Color.DARK_GRAY, 80.00);
    addItemInfo(item);
    totalCost += item.getCost();

    add(new JLabel(" "));
    add(new JLabel(" "));
    add(new JLabel(" "));
    add(new JLabel(Double.toString(totalCost)));

    setTitle("Men's Clothing");
    setLayout(new GridLayout(8, 4, 20, 3));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
  }

  void addItemInfo(MensClothingItemPlus item) {
    add(new JLabel("  " + item.getKind()));
    add(new JLabel(item.getName()));
    add(new JLabel(item.getColor().toString()));
    add(new JLabel(Double.toString(item.getCost())));
  }
}

Count the number of playing cards

public class PlayingCardPlus extends PlayingCard {
  public static int count = 0;

  public PlayingCardPlus(Suit suit, int number) {
    super(suit, number);
    count++;
  }
}


public class Main {

  public static void main(String[] args) {
    PlayingCardPlus card1 = new PlayingCardPlus(Suit.SPADES, 13);
    PlayingCardPlus card2 = new PlayingCardPlus(Suit.HEARTS, 1);
    PlayingCardPlus card3 = new PlayingCardPlus(Suit.DIAMONDS, 3);

    System.out.println(card1.getString());
    System.out.println(card2.getString());
    System.out.println(card3.getString());

    System.out.println("Number of cards: " + PlayingCardPlus.count);
  }

}

What's the output of the MutableInteger code?

bigValue: 1000000
holder1: 42
holder2: 7

bigValue: 1000001
holder1: 43
holder2: 8

bigValue according to holder1: 1000002
bigValue according to holder2: 1000002

What's the output of the Main1 code?

George
Barry
The first println displays the static name variable belonging to the Main1 class; the second println displays the name variable that's local to the main method.

What's the output of the Main2 code?

George
Barry
George
The expression this.name refers to the name field (the variable that belongs to this instance of the Main2 class).

What's the output of the Main3 code?

Leonard
George
The first println displays the name variable that's local to the OtherClass constructor. The second println displays that static name field that belongs to the Main3 class.

What's the output of the Main4 code?

Leonard
George
The first println refers to the name variable that's local to the YetAnotherClass constructor. The second println refers to the name field belonging to the whoCreatedMe object (an instance of the Main4 class).