Commit 7f4de954 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch2] All exercises

parent 06fb2a2a
Loading
Loading
Loading
Loading
+27 −0
Changes for src/main/java/ch2/DrumKit.java: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch2;

class DrumKit {
  boolean topHat = true;
  boolean snare = true;

  void playTopHat() {
    System.out.println("ding ding da-ding");
  }

  void playSnare() {
    System.out.println("bang bang ba-bang");
  }
}

class DrumKitTestDrive {
  public static void main(String[] args) {
    DrumKit d = new DrumKit();
    d.playSnare();
    d.snare = false;
    d.playTopHat();

    if (d.snare == true) {
      d.playSnare();
    }
  }
}
 No newline at end of file
+33 −0
Changes for src/main/java/ch2/EchoTestDrive.java: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch2;

public class EchoTestDrive {
  public static void main(String[] args) {
    Echo e1 = new Echo();
    Echo e2 = new Echo();   // correct answer
//       - or -
//    Echo e2 = e1;    // bonus "24” answer
    int x = 0;
    while (x < 4) {
      e1.hello();
      e1.count = e1.count + 1;
      if (x == 3) {
        e2.count = e2.count + 1;
      }
      if (x > 0) {
        e2.count = e2.count + e1.count;
      }
      x = x + 1;
    }
    System.out.println(e2.count);
  }
}

class Echo {
  int count = 0;

  void hello() {
    System.out.println("helloooo... ");
  }
}