Commit 2c4b4020 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch6] Chapter 6 code, including initial (buggy) version of the Startup code

parent 3bed6b28
Loading
Loading
Loading
Loading
+4 −0
Changes for src/main/java/ch6/Egg.java: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch6;

public class Egg {
}
+25 −0
Changes for src/main/java/ch6/Snippets.java: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch6;

import java.util.ArrayList;

public class Snippets {
  void arrayList() {
    ArrayList<Egg> myList = new ArrayList<Egg>();

    Egg egg1 = new Egg();
    myList.add(egg1);

    Egg egg2 = new Egg();
    myList.add(egg2);

    int theSize = myList.size();

    boolean isIn = myList.contains(egg1);

    int idx = myList.indexOf(egg2);

    boolean empty = myList.isEmpty();

    myList.remove(egg1);
  }
}
+26 −0
Changes for src/main/java/ch6/Startup.java: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch6;

class Startup {
  private int[] locationCells;
  private int numOfHits = 0;

  public void setLocationCells(int[] locs) {
    locationCells = locs;
  }

  public String checkYourself(int guess) {
    String result = "miss";
    for (int cell : locationCells) {
      if (guess == cell) {
        result = "hit";
        numOfHits++;
        break;
      } // end if
    } // end for
    if (numOfHits == locationCells.length) {
      result = "kill";
    } // end if
    System.out.println(result);
    return result;
  } // end method
} // close class
 No newline at end of file