Commit 549d9008 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch3] Code exercises from the end of the book (mostly the questions, not the answers)

parent 8501a341
Loading
Loading
Loading
Loading
+27 −0
Changes for src/main/java/ch3/Books.java: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

class Books {
  String title;
  String author;
}

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

    Books[] myBooks = new Books[3];
    int x = 0;
    myBooks[0].title = "The Grapes of Java";
    myBooks[1].title = "The Java Gatsby";
    myBooks[2].title = "The Java Cookbook";
    myBooks[0].author = "bob";
    myBooks[1].author = "sue";
    myBooks[2].author = "ian";

    while (x < 3) {
      System.out.print(myBooks[x].title);
      System.out.print(" by ");
      System.out.println(myBooks[x].author);
      x = x + 1;
    }
  }
}
 No newline at end of file
+27 −0
Changes for src/main/java/ch3/FiveMinuteMystery.java: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

public class FiveMinuteMystery {
  void solution1() {
    int x = 0;
    Contact[] contacts = new Contact[10];
    while (x < 10) {   // make 10 contact objects
      contacts[x] = new Contact();
      x = x + 1;
    }
    // do complicated Contact list updating with contacts
  }

  void solution2() {
    int x = 0;
    Contact contactRef;
    while ( x < 10 ) {   // make 10 contact objects
      contactRef = new Contact();
      x = x + 1;
    }
    // do complicated Contact list updating stuff with contactRef
  }

}

class Contact {
}
+23 −0
Changes for src/main/java/ch3/HeapQuiz.java: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

class HeapQuiz {
  int id = 0;

  public static void main(String[] args) {
    int x = 0;
    HeapQuiz[] hq = new HeapQuiz[5];
    while (x < 3) {
      hq[x] = new HeapQuiz();
      hq[x].id = x;
      x = x + 1;
    }
    hq[3] = hq[1];
    hq[4] = hq[1];
    hq[3] = null;
    hq[4] = hq[0];
    hq[0] = hq[3];
    hq[3] = hq[2];
    hq[2] = hq[0];
    // do stuff
  }
}  
+24 −0
Changes for src/main/java/ch3/Hobbits.java: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

class Hobbits {
  String name;

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

    while (z < 4) {
      z = z + 1;
      h[z] = new Hobbits();
      h[z].name = "bilbo";
      if (z == 1) {
        h[z].name = "frodo";
      }
      if (z == 2) {
        h[z].name = "sam";
      }
      System.out.print(h[z].name + " is a ");
      System.out.println("good Hobbit name");
    }
  }
}
 No newline at end of file
+24 −0
Changes for src/main/java/ch3/TestArrays.java: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

class TestArrays {
  public static void main(String[] args) {
    int[] index = new int[4];
    index[0] = 1;
    index[1] = 3;
    index[2] = 0;
    index[3] = 2;
    String[] islands = new String[4];
    islands[0] = "Bermuda";
    islands[1] = "Fiji";
    islands[2] = "Azores";
    islands[3] = "Cozumel";
    int y = 0;
    int ref;
    while (y < 4) {
      ref = index[y];
      System.out.print("island = ");
      System.out.println(islands[ref]);
      y = y + 1;
    }
  }
} 
 No newline at end of file
Loading