Commit 5342a099 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch3] Solutions to the code exercises

parent 549d9008
Loading
Loading
Loading
Loading
+3 −1
Changes for src/main/java/ch3/Books.java: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -7,9 +7,11 @@ class Books {

class BooksTestDrive {
  public static void main(String[] args) {

    Books[] myBooks = new Books[3];
    int x = 0;
    myBooks[0] = new Books();
    myBooks[1] = new Books();
    myBooks[2] = new Books();
    myBooks[0].title = "The Grapes of Java";
    myBooks[1].title = "The Java Gatsby";
    myBooks[2].title = "The Java Cookbook";
+2 −3
Changes for src/main/java/ch3/Hobbits.java: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -5,9 +5,8 @@ class Hobbits {

  public static void main(String[] args) {
    Hobbits[] h = new Hobbits[3];
    int z = 0;

    while (z < 4) {
    int z = -1;
    while (z < 2) {
      z = z + 1;
      h[z] = new Hobbits();
      h[z].name = "bilbo";
+28 −25
Changes for src/main/java/ch3/Triangle.java: 28 added lines, 25 removed lines.
Original line number Diff line number Diff line
package ch3;

class Triangle {
//  double area;
//  int height;
//  int length;
//  public static void main(String [] args) {
//    ____________
//    _______________________
//    while ( __________ ) {
//      __________________________
//      ________.height = (x + 1) * 2;
//      ________.length = x + 4;
//      __________________________
//      System.out.print("triangle "+x+", area");
//      System.out.println(" = " + _______.area);
//      ________________
//    }
//    ______________
//    x = 27;
//    Triangle t5 = ta[2];
//    ta[2].area = 343;
//    System.out.print("y = " + y);
//    System.out.println(", t5 area = "+ t5.area);
//  }
//  void setArea() {
//    ____________ = (height * length) / 2;
//  }
  double area;
  int height;
  int length;

  public static void main(String[] args) {
    int x = 0;
    Triangle[] ta = new Triangle[4];
    while (x < 4) {
      ta[x] = new Triangle();
      ta[x].height = (x + 1) * 2;
      ta[x].length = x + 4;
      ta[x].setArea();
      System.out.print("triangle " + x + ", area");
      System.out.println(" = " + ta[x].area);
      x = x + 1;
    }
    int y = x;
    x = 27;
    Triangle t5 = ta[2];
    ta[2].area = 343;
    System.out.print("y = " + y);
    System.out.println(", t5 area = " + t5.area);
  }

  void setArea() {
    area = (height * length) / 2;
  }
}


@@ -33,6 +35,7 @@ class Triangle {
//  double area;
//  int height;
//  int length;
//
//  public static void main(String[] args) {
//    ____________
//    _______________________