Commit 4b9943cb authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch1] Examples from Ch1

parent 3418a3d1
Loading
Loading
Loading
Loading
+27 −0
Changes for src/main/java/ch1/BottleSong.java: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

public class BottleSong {
  public static void main(String[] args) {
    int bottlesNum = 10;
    String word = "bottles";

    while (bottlesNum > 0) {

      if (bottlesNum == 1) {
        word = "bottle"; // singular, as in ONE bottle.
      }

      System.out.println(bottlesNum + " green " + word + ", hanging on the wall");
      System.out.println(bottlesNum + " green " + word + ", hanging on the wall");
      System.out.println("And if one green bottle should accidentally fall,");
      bottlesNum = bottlesNum - 1;

      if (bottlesNum > 0) {
        System.out.println("There'll be " + bottlesNum +
                           " green " + word + ", hanging on the wall");
      } else {
        System.out.println("There'll be no green bottles, hanging on the wall");
      } // end else
    } // end while loop
  } // end main method
} // end class
 No newline at end of file
+11 −0
Changes for src/main/java/ch1/IfTest.java: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

class IfTest {
  public static void main (String[] args) {
    int x = 3;
    if (x == 3) {
      System.out.println("x must be 3");
    }
    System.out.println("This runs no matter what");
  }
}
 No newline at end of file
+14 −0
Changes for src/main/java/ch1/Loopy.java: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

public class Loopy {
  public static void main(String[] args) {
    int x = 1;
    System.out.println("Before the Loop");
    while (x < 4) {
      System.out.println("In the loop");
      System.out.println("Value of x is " + x);
      x = x + 1;
    }
    System.out.println("This is after the loop");
  }
}
 No newline at end of file
+10 −0
Changes for src/main/java/ch1/MyFirstApp.java: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

public class MyFirstApp {

   public static void main (String[] args) {
     System.out.println("I Rule!");
     System.out.println("The World");
   }

}
 No newline at end of file
+29 −0
Changes for src/main/java/ch1/PhraseOMatic.java: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

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

    // make three sets of words to choose from. Add your own!
    String[] wordListOne = {"agnostic", "opinionated", "voice activated", "haptically driven", "extensible", "reactive", "agent based", "functional", "AI enabled", "strongly typed"};

    String[] wordListTwo = {"loosely coupled", "six sigma", "asynchronous", "event driven", "pub-sub", "IoT", "cloud native", "service oriented", "containerized", "serverless", "microservices", "distributed ledger"};

    String[] wordListThree = {"framework", "library", "DSL", "REST API", "repository", "pipeline", "service mesh", "architecture", "perspective", "design", "orientation"};

    // find out how many words are in each list
    int oneLength = wordListOne.length;
    int twoLength = wordListTwo.length;
    int threeLength = wordListThree.length;

    // generate three random numbers
    int rand1 = (int) (Math.random() * oneLength);
    int rand2 = (int) (Math.random() * twoLength);
    int rand3 = (int) (Math.random() * threeLength);

    // now build a phrase
    String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];

    // print out the phrase
    System.out.println("What we need is a " + phrase);
  }
}  
 No newline at end of file