Coffee Break: ちょっと一休み

Step 2 のコマンドの翻訳で疲れきっているので
ちょっとお休み。
ごくろうさま。
本格的に Step 3 が始まるまでに、JDKをインストールして使ってみてね!
サンプルプログラムは。
お得意の

--- HelloWorld.java ----
/*
 * Comment, comment, comment.
 */

class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, world.");
  }
}
          

これではちょっと簡単すぎるので。クラス継承のサンプルも兼ねて

/* File: GraphicsProg.java */
 
abstract class Shape {
  protected int x;
  protected int y;
 
  protected Shape(int x, int y) {
    this.x = x;
    this.y = y;
  }
 
  public abstract void draw();
}
 
class Rectangle extends Shape {
  private int width;
  private int height;
 
  public Rectangle(int x, int y,
                   int width, int height) {
    super(x, y);
    this.width  = width;
    this.height = height;
  }
 
  public void draw() {
    System.out.println("Rectangle: " + x + ", " + y +
                       ", " + width + ", " + height);
  }
}
 
class Circle extends Shape {
  private int radius;

  public Circle(int x, int y, int radius) {
    super(x, y);
    this.radius = radius;
  }
 
  public void draw() {
    System.out.println("Circle:    " + x + ", " + y +
                       ", " + radius);
  }
}
 
public class GraphicsProg {
  public static void main(String args[]) {
    Shape s1 = new Rectangle(0, 5, 100, 200);
    Shape s2 = new Circle(20, 30, 100);
 
    s1.draw();
    s2.draw();
  }
}

これで、コマンドが一通り使えるでしょう。これって何処かで見たことあるプログラムでしょ。馴染みがあった方がわかりやすいので、ObjectCenter に付いているサンプルプログラムを GUI 使わないように Java で作り替えてみたんだ。
では頑張ってね!質問があればいつもの通り E-Mail でね!
はあい。ちょっと使ってみます。
エディタは vi ね。emacs 禁止。
ええ〜
うそ。

コンパイル方法:
javac を使って上のプログラムをコンパイル。

# javac HelloWorld.java
# javac GraphicsProg.java

実行してみよう:

# java HelloWorld
Hello, world.
# java GraphicsProg
Rectangle: 0, 5, 100, 200
Circle:    20, 30, 100

コンパイルした後で、どんなファイルが作られるのかも良くみてみてね!