Commit 8501a341 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch3] Code samples from throughout chapter, plus a quick test to see which...

[Ch3] Code samples from throughout chapter, plus a quick test to see which special words in Java actually can be used as variable names.
parent 7f4de954
Loading
Loading
Loading
Loading
+46 −0
Changes for src/main/java/ch3/Dog.java: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

class Dog {
  String name;

  public static void main(String[] args) {
    // make a Dog object and access it
    Dog dog1 = new Dog();
    dog1.bark();
    dog1.name = "Bart";

    // now make a Dog array
    Dog[] myDogs = new Dog[3];
    // and put some dogs in it
    myDogs[0] = new Dog();
    myDogs[1] = new Dog();
    myDogs[2] = dog1;

    // now access the Dogs using the array
    // references
    myDogs[0].name = "Fred";
    myDogs[1].name = "Marge";

    // Hmmmm... what is myDogs[2] name?
    System.out.print("last dog’s name is ");
    System.out.println(myDogs[2].name);

    // now loop through the array
    // and tell all dogs to bark
    int x = 0;
    while (x < myDogs.length) {
      myDogs[x].bark();
      x = x + 1;
    }
  }

  public void bark() {
    System.out.println(name + " says Ruff!");
  }

  public void eat() {
  }

  public void chaseCat() {
  }
}
 No newline at end of file
+108 −0
Changes for src/main/java/ch3/ExampleDeclarations.java: 108 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

public class ExampleDeclarations {
  void declarations() {
    int x;
    x = 234;
    byte b = 89;
    boolean isFun = true;
    double d = 3456.98;
    char c = 'f';
    int z = x;
    boolean isPunkRock;
    isPunkRock = false;
    boolean powerOn;
    powerOn = isFun;
    long big = 3456789L;
    float f = 32.5f;
  }

  void declareLiterals() {
    int x = 234;


    int size = 32;
    char initial = 'j';
    double d = 456.709;
    boolean isLearning;
    isLearning = true;
    int y = x + 456;

  }

  void invalidNames() {
//    int _;
//    int abstract;
//    int assert;
//    int boolean;
//    int break;
//    int byte;
//    int case;
//    int catch;
//    int char;
//    int class;
//    int const;
//    int continue;
//    int default;
//    int do;
//    int   double;
//    int else;
//    int enum;
    int exports;
//    int extends;
//    int final;
//    int finally;
//    int float;
//    int for;
//    int goto;
//    int if;
//    int implements;
//    int import;
//    int instanceof;
//    int int;
//    int interface;
//    int long;
    int module;
//    int native;
//    int new;
//    int         non-sealed;
//    int null;
    int open;
    int opens;
//    int package;
    int permits;
//    int private;
//    int protected;
    int provides;
//    int public;
    int record;
    int requires;
//    int return;
    int sealed;
//    int short;
//    int static;
//    int strictfp;
//    int super;
//    int switch;
//    int synchronized;
//    int this;
//    int throw;
//    int throws;
    int to;
//    int transient;
    int transitive;
//    int try;
    int uses;
    int var;
//    int void;
//    int volatile;
//    int while;
    int with;
    int yield;
//    int false;
//    int true;

  }


}
+18 −0
Changes for src/main/java/ch3/Sharpen.java: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch3;

public class Sharpen {
  void sharpen() {
//    int x = 34.5;
//    boolean boo = x;
    int g = 17;
    int y = g;
    y = y + 10;
    short s;
//    s = y;
    byte b = 3;
    byte v = b;
    short n = 12;
//    v = n;
//    byte k = 128;
  }
}