Commit f273f022 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch1] All code samples from chapter one book added.

parent db1c530c
Loading
Loading
Loading
Loading
+31 −0
Changes for src/main/java/ch1/DooBee.java: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

public class DooBee {
  public static void main(String[] args) {
    int x = 1;
    while (x < 3) {
      System.out.print("Doo");
      System.out.print("Bee");
      x = x + 1;
    }
    if (x == 3) {
      System.out.print("Do");
    }
  }
}

//% java DooBee
//DooBeeDooBeeDo


//  public static void main (String[] args) {
//    int x = 1;
//    while (x < _____ ) {
//      System.out._________("Doo");
//      System.out._________("Bee");
//      x = x + 1;
//    }
//    if (x == ______  ) {
//      System.out.print("Do");
//    }
//  }
+56 −0
Changes for src/main/java/ch1/FirstJavaClass.java: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

import java.io.FileNotFoundException;

public class FirstJavaClass {
  private static int x;

  public static void main(String[] args) {
    int size = 27;
    String name = "Fido";
    Dog myDog = new Dog(name, size);
    x = size - 5;
    if (x < 15) myDog.bark(8);

    while (x > 3) {
      myDog.play();
    }

    int[] numList = {2, 4, 6, 8};
    System.out.print("Hello");
    System.out.print("Dog: " + name);
    String num = "8";
    int z = Integer.parseInt(num);

    try {
      readTheFile("myFile.txt");
    }
    catch (FileNotFoundException ex) {
      System.out.print("File not found.");
    }

  }

  private static void readTheFile(String s) throws FileNotFoundException {

  }

}

class Dog {
  private final String name;
  private final int size;

  public Dog(String name, int size) {
    this.name = name;
    this.size = size;
  }

  public void bark(int i) {

  }

  public void play() {

  }
}
+13 −0
Changes for src/main/java/ch1/IfTest2.java: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

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

import java.awt.*;
import java.awt.event.*;

class Party {
  public void buildInvite() {
    Frame f = new Frame();
    Label l = new Label("Party at Tim's");
    Button b = new Button("You bet");
    Button c = new Button("Shoot me");
    Panel p = new Panel();
    p.add(l);
  }   // more code here...
}
 No newline at end of file
+48 −0
Changes for src/main/java/ch1/Snippets.java: 48 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch1;

public class Snippets {
  void statements() {
    int x = 3;
    String name = "Dirk";
    x = x * 17;
    System.out.print("x is " + x);
    double d = Math.random();
    // this is a comment
  }

  void looping(int x) {
    while (x > 12) {
      x = x - 1;
    }

    for (int i = 0; i < 10; i = i + 1) {
      System.out.print("i is now " + i);
    }
  }

  void conditions(int x, String name) {
    if (x == 10) {
      System.out.print("x must be 10");
    } else {
      System.out.print("x isn't 10");
    }
    if ((x < 3) & (name.equals("Dirk"))) {
      System.out.println("Gently");
    }
    System.out.print("this line runs no matter what");
  }

  void booleanTests() {
    int x = 4; // assign 4 to x
    while (x > 3) {
      // loop code will run because
      // x is greater than 3
      x = x - 1; // or we’d loop forever
    }
    int z = 27; //
    while (z == 17) {
      // loop code will not run because
      // z is not equal to 17
    }
  }
}