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

Chapter 7: The Inside scOOP

In this chapter:

Getting Organized

public class Organization {
  String name;
  double revenue;
  boolean isForProfit;
}

public class Main {

  public static void main(String[] args) {
    var org1 = new Organization();
    org1.name = "XYZ Company";
    org1.revenue = 1000000.00;
    org1.isForProfit = true;

    var org2 = new Organization();
    org2.name = "XYZ University";
    org2.revenue = 100000.00;
    org2.isForProfit = false;

    System.out.print(org1.name);
    System.out.print(" has annual revenue ");
    System.out.print(org1.revenue);
    System.out.print(" and is ");
    System.out.print(org1.isForProfit ? "" : "not ");
    System.out.println("for profit.");

    System.out.print(org2.name);
    System.out.print(" has annual revenue ");
    System.out.print(org2.revenue);
    System.out.print(" and is ");
    System.out.print(org2.isForProfit ? "" : "not ");
    System.out.println("for profit.");
  }

}

Yummy Foods

public class FoodProduct {
  String type;
  double weight;
  int servings;
  int calories;
}

public class Main {

  public static void main(String[] args) {
    var product1 = new FoodProduct();
    product1.type = "Peach Slices";
    product1.weight = 500;
    product1.servings = 4;
    product1.calories = 70;

    var product2 = new FoodProduct();
    product2.type = "Bread";
    product2.weight = 500;
    product2.servings = 10;
    product2.calories = 150;

    System.out.print(product1.type);
    System.out.print(" has weight ");
    System.out.print(product1.weight);
    System.out.print(" grams, has ");
    System.out.print(product1.servings);
    System.out.print(" servings, and has ");
    System.out.print(product1.calories);
    System.out.println(" calories.");

    System.out.print(product2.type);
    System.out.print(" has weight ");
    System.out.print(product2.weight);
    System.out.print(" grams, has ");
    System.out.print(product2.servings);
    System.out.print(" servings, and has ");
    System.out.print(product2.calories);
    System.out.println(" calories.");

  }

}

Show Off

public class Organization {
  String name;
  double revenue;
  boolean isForProfit;

  public void display() {
    System.out.print(name);
    System.out.print(" has annual revenue ");
    System.out.print(revenue);
    System.out.print(" and is ");
    System.out.print(isForProfit ? "" : "not ");
    System.out.println("for profit.");
  }
}


public class Main {

  public static void main(String[] args) {
    var org1 = new Organization();
    org1.name = "XYZ Company";
    org1.revenue = 1000000.00;
    org1.isForProfit = true;

    Organization org2 = new Organization();
    org2.name = "XYZ University";
    org2.revenue = 100000.00;
    org2.isForProfit = false;

    org1.display();
    org2.display();
  }

}


public class FoodProduct {
  String type;
  double weight;
  int servings;
  int calories;

  public void display() {
    System.out.print(type);
    System.out.print(" has weight ");
    System.out.print(weight);
    System.out.print(" grams, has ");
    System.out.print(servings);
    System.out.print(" servings, and has ");
    System.out.print(calories);
    System.out.println(" calories.");
  }
}


public class Main {

  public static void main(String[] args) {
    var product1 = new FoodProduct();
    product1.type = "Peach Slices";
    product1.weight = 500;
    product1.servings = 4;
    product1.calories = 70;

    var product2 = new FoodProduct();
    product2.type = "Bread";
    product2.weight = 500;
    product2.servings = 10;
    product2.calories = 150;

    product1.display();
    product2.display();
  }

}

Tax Day

public class Organization {
  String name;
  double revenue;
  boolean isForProfit;

  public void display() {
    System.out.print(name);
    System.out.print(" has annual revenue ");
    System.out.print(revenue);
    System.out.print(", is ");
    System.out.print(isForProfit ? "" : "not ");
    System.out.print("for profit, and pays ");
    System.out.print(tax());
    System.out.println(" tax.");
  }

  public double tax() {
    if (isForProfit) {
      return revenue * 0.10;
    } else {
      return revenue * 0.02;
    }
  }
}

public class Main {

  public static void main(String[] args) {
    var org1 = new Organization();
    org1.name = "XYZ Company";
    org1.revenue = 1000000.00;
    org1.isForProfit = true;

    Organization org2 = new Organization();
    org2.name = "XYZ University";
    org2.revenue = 100000.00;
    org2.isForProfit = false;

    org1.display();
    org2.display();
  }

}

Cost of Consumption

public class FoodProduct {
  String type;
  double weight;
  int servings;
  int calories;
  double cost;

  public void display() {
    System.out.print(type);
    System.out.print(" has weight ");
    System.out.print(weight);
    System.out.print(" grams, has ");
    System.out.print(servings);
    System.out.print(" servings, and has ");
    System.out.print(calories);
    System.out.println(" calories.");
    System.out.println("  The cost per 100 grams is " + costPer100Grams() + ".");
    System.out.println("  The cost per serving is " + costPerServing() + ".");
    System.out.println("  The total calories is " + totalCalories() + ".");
  }

  public double costPer100Grams() {
    return cost / (weight / 100);
  }

  public double costPerServing() {
    return cost / servings;
  }

  public int totalCalories() {
    return calories * servings;
  }
}



public class Main {

  public static void main(String[] args) {
    var product1 = new FoodProduct();
    product1.type = "Peach Slices";
    product1.weight = 500;
    product1.servings = 4;
    product1.calories = 70;
    product1.cost = 5.00;

    var product2 = new FoodProduct();
    product2.type = "Bread";
    product2.weight = 500;
    product2.servings = 10;
    product2.calories = 150;
    product2.cost = 2.50;

    product1.display();
    product2.display();
  }

}

Fill in the Blanks

import static java.lang.System.out;

public class Main {

    public static void main(String[] args) {
        out.printf("%s%d%s", ">>", 7, "<<\n");
        out.printf("%s%10d%s", ">>", 7, "<<\n");
        out.printf("%s%-10d%s", ">>", 7, "<<\n");
        out.printf("%s%010d%s", ">>", 7, "<<\n");
        out.printf("%s%+d%s", ">>", 7, "<<\n");
        out.printf("%s%+d%s", ">>", -7, "<<\n");
        out.printf("%s%(d%s", ">>", -7, "<<\n");
        out.printf("%s%10.5f%s", ">>", 7.0, "<<\n");
        out.printf("%s%S%s", ">>", "Hello", "<<\n");
        out.printf("%s%c%s", ">>", 'x', "<<\n");
        out.printf("%s%C%s", ">>", 'x', "<<\n");
    }
}

Private Eye

public class Organization {
  private String name;
  private double revenue;
  private boolean isForProfit;

  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 boolean isForProfit() {
    return isForProfit;
  }

  public void setForProfit(boolean isForProfit) {
    this.isForProfit = isForProfit;
  }
}


public class Main {

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

    Organization org2 = new Organization();
    org2.setName("XYZ University");
    org2.setRevenue(100000.00);
    org2.setForProfit(false);

    System.out.print(org1.getName());
    System.out.print(" has annual revenue ");
    System.out.print(org1.getRevenue());
    System.out.print(" and is ");
    System.out.print(org1.isForProfit() ? "" : "not ");
    System.out.println("for profit.");

    System.out.print(org2.getName());
    System.out.print(" has annual revenue ");
    System.out.print(org2.getRevenue());
    System.out.print(" and is ");
    System.out.print(org2.isForProfit() ? "" : "not ");
    System.out.println("for profit.");
  }

}


public class FoodProduct {
  private String type;
  private double weight;
  private int servings;
  private int calories;

  public String getType() {
    return type;
  }

  public void setType(String type) {
    if (!type.equals("")) {
      this.type = type;
    }
  }

  public double getWeight() {
    return weight;
  }

  public void setWeight(double weight) {
    if (weight > 0) {
      this.weight = weight;
    }
  }

  public int getServings() {
    return servings;
  }

  public void setServings(int servings) {
    if (servings > 0) {
      this.servings = servings;
    }
  }

  public int getCalories() {
    return calories;
  }

  public void setCalories(int calories) {
    if (calories >= 0) {
      this.calories = calories;
    }
  }
}


public class Main {

  public static void main(String[] args) {
    var product1 = new FoodProduct();
    product1.setType("Peach Slices");
    product1.setWeight(500);
    product1.setServings(4);
    product1.setCalories(70);

    var product2 = new FoodProduct();
    product2.setType("Bread");
    product2.setWeight(500);
    product2.setServings(10);
    product2.setCalories(150);

    System.out.print(product1.getType());
    System.out.print(" has weight ");
    System.out.print(product1.getWeight());
    System.out.print(" grams, has ");
    System.out.print(product1.getServings());
    System.out.print(" servings, and has ");
    System.out.print(product1.getCalories());
    System.out.println(" calories.");

    System.out.print(product2.getType());
    System.out.print(" has weight ");
    System.out.print(product2.getWeight());
    System.out.print(" grams, has ");
    System.out.print(product2.getServings());
    System.out.print(" servings, and has ");
    System.out.print(product2.getCalories());
    System.out.println(" calories.");

  }

}

Window Shopping

public class Organization {
  String name;
  double revenue;
  boolean isForProfit;
}

import com.allmycode.dummiesframe.DummiesFrame;

public class Main {

    public static void main(String[] args) {
        var frame = new DummiesFrame("Organization Info");
        frame.addRow("Name");
        frame.addRow("Annual Revenue");
        frame.addRow("For Profit? (Y/N)");
        frame.setButtonText("Display");
        frame.go();
    }

    public static String calculate(String name, double revenue,
                                                String isForProfit) {
        Organization org = new Organization();
        org.name = name;
        org.revenue = revenue;

        double tax;
        if (isForProfit.equals("Y")) {
            tax = revenue * 0.10;
        } else {
            tax = revenue * 0.02;
        }

        return "Tax: " + tax;
    }
}

GUI Chop Suey

public class FoodProduct {
  String type;
  double weight;
  double cost;
  int servings;
  int calories;
}


import com.allmycode.dummiesframe.DummiesFrame;

public class Main {

    public static void main(String[] args) {
        var frame = new DummiesFrame("Product");
        frame.addRow("Type");
        frame.addRow("Weight");
        frame.addRow("Cost");
        frame.addRow("Servings");
        frame.addRow("Calories per serving");
        frame.setButtonText("Display");
        frame.go();
    }

    public static String calculate(String type,
                                   double weight,
                                   double cost,
                                   int servings,
                                   int calories) {
        var product = new FoodProduct();

        product.type = type;
        product.weight = weight;
        product.cost = cost;
        product.servings = servings;
        product.calories = calories;
        return "Cost per 100 grams: " + (cost / (weight / 100)) + "; " +
               "Cost per serving: " + (cost / servings) + "; " +
               "Total calories: " + (calories * servings);

    }
}