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

Chapter 8: Saving Time and Money: Reusing Existing Code

In this chapter:

Your first PlaceToLive class

public class PlaceToLive {
  private String address;
  private int numberOfBedrooms;
  private double area;

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public int getNumberOfBedrooms() {
    return numberOfBedrooms;
  }

  public void setNumberOfBedrooms(int numberOfBedrooms) {
    this.numberOfBedrooms = numberOfBedrooms;
  }

  public double getArea() {
    return area;
  }

  public void setArea(double area) {
    this.area = area;
  }
}

import java.util.Scanner;

public class DisplayThePlaces {

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

    PlaceToLive place1 = new PlaceToLive();
    place1.setAddress("111 Main Street");
    place1.setArea(600);
    place1.setNumberOfBedrooms(2);
    System.out.print("Cost of " + place1.getAddress() + ": ");
    double cost1 = keyboard.nextDouble();

    PlaceToLive place2 = new PlaceToLive();
    place2.setAddress("222 Java Road");
    place2.setArea(1000);
    place2.setNumberOfBedrooms(1);
    System.out.print("Cost of " + place2.getAddress() + ": ");
    double cost2 = keyboard.nextDouble();

    System.out.println(place1.getAddress());
    System.out.println
      ("  Cost per unit area: " + cost1 / place1.getArea());
    System.out.println
      ("  Cost per bedroom:   " + cost1/ place1.getNumberOfBedrooms());

    System.out.println(place2.getAddress());
    System.out.println
      ("  Cost per unit area: " + cost2 / place2.getArea());
    System.out.println
      ("  Cost per bedroom:   " + cost2 / place2.getNumberOfBedrooms());

    keyboard.close();
  }
}

Use the PlaceToLive class with a DummiesFrame

public class PlaceToLive {
  private String address;
  private int numberOfBedrooms;
  private double area;

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public int getNumberOfBedrooms() {
    return numberOfBedrooms;
  }

  public void setNumberOfBedrooms(int numberOfBedrooms) {
    this.numberOfBedrooms = numberOfBedrooms;
  }

  public double getArea() {
    return area;
  }

  public void setArea(double area) {
    this.area = area;
  }
}


import com.allmycode.dummiesframe.DummiesFrame;

public class Main {

    public static void main(String args[]) {
        DummiesFrame frame = new DummiesFrame("Place to Live");
        frame.addRow("Address");
        frame.addRow("Area");
        frame.addRow("Number of Bedrooms");
        frame.addRow("Cost");
        frame.setButtonText("Display");
        frame.go();
    }

    public static String calculate(String address,
                                   double area,
                                   int numberOfBedrooms,
                                   double cost) {

        PlaceToLive place = new PlaceToLive();
        place.setAddress(address);
        place.setArea(area);
        place.setNumberOfBedrooms(numberOfBedrooms);

        return address + "; " +
               "Cost per unit area: " + (cost / place.getArea()) + "; " +
               "Cost per bedroom: " +
                                     (cost / place.getNumberOfBedrooms());
    }
}

Get PlaceToLive information from a disk file

public class PlaceToLive {
  private String address;
  private int numberOfBedrooms;
  private double area;

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public int getNumberOfBedrooms() {
    return numberOfBedrooms;
  }

  public void setNumberOfBedrooms(int numberOfBedrooms) {
    this.numberOfBedrooms = numberOfBedrooms;
  }

  public double getArea() {
    return area;
  }

  public void setArea(double area) {
    this.area = area;
  }
}


import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class DisplayThePlaces {

  public static void main(String[] args) throws IOException {
    Scanner diskScanner = new Scanner(new File("places.txt"));
    Scanner keyboard = new Scanner(System.in);

    PlaceToLive place1 = new PlaceToLive();
    place1.setAddress(diskScanner.nextLine());
    place1.setArea(diskScanner.nextDouble());
    place1.setNumberOfBedrooms(diskScanner.nextInt());
    System.out.print("Cost of " + place1.getAddress() + ": ");
    double cost1 = keyboard.nextDouble();

    diskScanner.nextLine();

    PlaceToLive place2 = new PlaceToLive();
    place2.setAddress(diskScanner.nextLine());
    place2.setArea(diskScanner.nextDouble());
    place2.setNumberOfBedrooms(diskScanner.nextInt());
    System.out.print("Cost of " + place2.getAddress() + ": ");
    double cost2 = keyboard.nextDouble();

    System.out.println(place1.getAddress());
    System.out.println
      ("  Cost per unit area: " + cost1 / place1.getArea());
    System.out.println
      ("  Cost per bedroom:   " + cost1/ place1.getNumberOfBedrooms());

    System.out.println(place2.getAddress());
    System.out.println
      ("  Cost per unit area: " + cost2 / place2.getArea());
    System.out.println
      ("  Cost per bedroom:   " + cost2 / place2.getNumberOfBedrooms());

    diskScanner.close();
    keyboard.close();
  }
}
Here's a sample places.txt file:
111 Main Street
1000
2
222 Java Row
1200
3

Create House and Apartment subclasses

public class PlaceToLive {
  private String address;
  private int numberOfBedrooms;
  private double area;

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public int getNumberOfBedrooms() {
    return numberOfBedrooms;
  }

  public void setNumberOfBedrooms(int numberOfBedrooms) {
    this.numberOfBedrooms = numberOfBedrooms;
  }

  public double getArea() {
    return area;
  }

  public void setArea(double area) {
    this.area = area;
  }
}

public class House extends PlaceToLive {
  private double mortgage;
  private double propertyTax;

  public double getMortgage() {
    return mortgage;
  }

  public void setMortgage(double mortgage) {
    this.mortgage = mortgage;
  }

  public double getPropertyTax() {
    return propertyTax;
  }

  public void setPropertyTax(double propertyTax) {
    this.propertyTax = propertyTax;
  }

  public double getTotalMonthlyPayment() {
    return mortgage + propertyTax / 12;
  }
}

public class Apartment extends PlaceToLive {
  private double rent;

  public double getRent() {
    return rent;
  }

  public void setRent(double rent) {
    this.rent = rent;
  }
}


public class DisplayThePlaces {

  public static void main(String[] args) {
    House place1 = new House();
    place1.setAddress("111 Main Street");
    place1.setArea(600);
    place1.setNumberOfBedrooms(2);
    place1.setMortgage(1000.00);
    place1.setPropertyTax(600);
    double cost1 = place1.getTotalMonthlyPayment();

    Apartment place2 = new Apartment();
    place2.setAddress("222 Java Road");
    place2.setArea(1000);
    place2.setNumberOfBedrooms(1);
    place2.setRent(1500);
    double cost2 = place2.getRent();

    System.out.println(place1.getAddress());
    System.out.println
      ("  Cost per unit area: " + cost1 / place1.getArea());
    System.out.println
      ("  Cost per bedroom:   " + cost1/ place1.getNumberOfBedrooms());

    System.out.println(place2.getAddress());
    System.out.println
      ("  Cost per unit area: " + cost2 / place2.getArea());
    System.out.println
      ("  Cost per bedroom:   " + cost2 / place2.getNumberOfBedrooms());
  }
}

Create subclasses ProfitMakingOrganization and NonProfitOrganization

public class Organization_2 {
  private String name;
  private double revenue;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    if (!name.equals("")) {
      this.name = name;
    }
  }

  public double getRevenue() {
    return revenue;
  }

  public void setRevenue(double revenue) {
    if (revenue >= 0) {
      this.revenue = revenue;
    }
  }
}

public class ProfitMakingOrganization extends Organization_2 {

  public double getTax() {
    return getRevenue() * 0.10;
  }
}

public class NonProfitOrganization extends Organization_2 {

  public double getTax() {
    return getRevenue() * 0.02;
  }
}


public class Main {

  public static void main(String[] args) {
    ProfitMakingOrganization org1 =
                              new ProfitMakingOrganization();
    org1.setName("XYZ Company");
    org1.setRevenue(1000000.00);

    NonProfitOrganization org2 = new NonProfitOrganization();
    org2.setName("XYZ University");
    org2.setRevenue(100000.00);

    System.out.print(org1.getName());
    System.out.print(" has annual revenue ");
    System.out.print(org1.getRevenue());
    System.out.print(" and pays ");
    System.out.print(org1.getTax());
    System.out.println(" in taxes.");

    System.out.print(org2.getName());
    System.out.print(" has annual revenue ");
    System.out.print(org2.getRevenue());
    System.out.print(" and pays ");
    System.out.print(org2.getTax());
    System.out.println(" in taxes.");
  }

}

Subclass ApartmentWithFees

public class ApartmentWithFees extends Apartment{
  private double fees;

  public double getFees() {
    return fees;
  }

  public void setFees(double fees) {
    this.fees = fees;
  }

  public double getTotalMonthlyPayment() {
    return getRent() + fees / 3;
  }
}


public class DisplayThePlaces {

  public static void main(String[] args) {
    House place1 = new House();
    place1.setAddress("111 Main Street");
    place1.setArea(600);
    place1.setNumberOfBedrooms(2);
    place1.setMortgage(1000.00);
    place1.setPropertyTax(600);
    double cost1 = place1.getTotalMonthlyPayment();

    Apartment place2 = new Apartment();
    place2.setAddress("222 Java Road");
    place2.setArea(1000);
    place2.setNumberOfBedrooms(1);
    place2.setRent(1500);
    double cost2 = place2.getRent();

    ApartmentWithFees place3 = new ApartmentWithFees();
    place3.setAddress("333 Front Street");
    place3.setArea(1500);
    place3.setNumberOfBedrooms(2);
    place3.setRent(700);
    place3.setFees(300);
    double cost3 = place3.getTotalMonthlyPayment();

    System.out.println(place1.getAddress());
    System.out.println("  Montly cost: " + cost1);

    System.out.println(place2.getAddress());
    System.out.println("  Montly cost: " + cost2);

    System.out.println(place3.getAddress());
    System.out.println("  Montly cost: " + cost3);
  }
}

What's the output of the MyThing code?

The output is
in MySUBThing, value is 7
In MyOTHERThing, value is 44
So if you have the following declarations:
MyThing myThing, myThing2;

myThing = new MySubThing();
myThing2 = new MyOtherThing();
and you declare display methods in the MyThing, MySubThing, and MyOtherThing classes, Java calls the display methods that you declared in the MySubThing and MyOtherThing classes. Even though you've declared the myThing and myThing2 variables to be of type MyThing, Java doesn't call the display method that's declared in the MyThing class.