<演習1>の解答例
 
class Origami extends Paper
{
  String col; // 色

  void setColor( String c ){ // 色を設定する
    col = new String( c );
  }
  void showColor(){ // 色情報を表示
    System.out.println(" 色 = " + col );
  }
}

class Paper
{
  double width;  // 幅(cm)
  double height; // 高さ(cm)

  public void setSize( double w, double h ){ // サイズを設定する
    width = w;
    height = h;
  }
  public void showSize(){ // サイズを表示する
    System.out.println(" (幅,高さ) = " + width + ", " + height );
  }
}

class Ensyu1301
{
  public static void main(String[] args)
  {
   
    // 紙クラスのオブジェクト
    System.out.println("\n- 紙 ----");
    Paper pp = new Paper();
    pp.setSize( 21.0, 29.7 );
    pp.showSize();

    // おりがみクラスのオブジェクト
    System.out.println("\n- おりがみ ----");
    Origami og = new Origami();
    og.setSize( 15.0, 15.0 );
    og.showSize();
    og.setColor("赤色");
    og.showColor();
  }
}

 
 
<演習2>の解答例
 
class Oyu extends Water // お湯クラス
{
  int temp = 100; // 温度

  // 量と温度を指定するコンストラクタ
  public Oyu( double v, int t ){
    super( v ); // スーパークラスのコンストラクタを呼ぶ
    temp = t;
  }
  void showTemp(){ // 温度を表示
    System.out.println(" 温度 = " + temp + " 度" );
  }
}
class Water // 水クラス
{
  double vol = 0.0; // 体積(cc)

  public Water( double v ){ // 体積を指定するコンストラクタ
    vol = v;
  }
  public void showVol(){ // 体積を表示
    System.out.println(" 体積 = " + vol + " cc" );
  }
}
class Ensyu1302
{
  public static void main(String[] args)
  {
    // お湯クラスのオブジェクト
    System.out.println("- お湯 ----");
    Oyu oy = new Oyu( 180.0, 85 );
    oy.showTemp();
    oy.showVol();
  }
}

 
 
<演習3>の解答例
 
class Sub_A extends Super_A // サブクラス(子クラス)
{
  public Sub_A(){ // 引数なしコンストラクタ
    System.out.println("コンスト at Sub_A");
    num = num + 25;  // numに25を加算する
  }
}
class Super_A // スーパークラス(親クラス)
{
  int num;

  public Super_A(){ // 引数なしコンストラクタ
    System.out.println("コンスト at Super_A");
    num = 10;  // numに10を代入する
  }
  public void show(){
    System.out.println(" > num = " + num + "  at Super_A" );
  }

}
class Ensyu1303
{
  public static void main(String[] args)
  {
    // Super_Aクラスのオブジェクト
    System.out.println("\n- Super_Aのオブジェクト ----");
    Super_A oya = new Super_A();
    oya.show();

    // Sub_Aクラスのオブジェクト
    System.out.println("\n- Sub_Aのオブジェクト ----");
    Sub_A ko = new Sub_A();
    ko.show();
  }
}

 
 
<演習4>の解答例
 
class Kaisuu extends Ziyuu // 回数券クラス
{
  public Kaisuu(){ // 引数なしコンストラクタ
    price -= 360; // 1枚あたりのprice
  }
  void showGoukei(){ // 合計を表示
    System.out.println(" 6枚綴りの価格 = " + price*6 + "円");
  }
}
class Ziyuu // 自由席クラス
{
  int price; // 料金

  public Ziyuu(){ // 引数なしコンストラクタ
    price = 5500; // 新大阪〜岡山 自由席料金
  }
  public void showPrice(){ // 料金を表示
    System.out.println(" 料金 = " + price + " 円" );
  }
}
class Ensyu1304
{
  public static void main(String[] args)
  {
    // 自由席クラスのオブジェクト
    System.out.println("- 自由席 ----");
    Ziyuu zi = new Ziyuu();
    zi.showPrice();

    // 回数券クラスのオブジェクト
    System.out.println("- 回数券 ----");
    Kaisuu ka = new Kaisuu();
    ka.showPrice();
    ka.showGoukei();
  }
}

 
 
<演習5>の解答例
 
class Premium extends Normal
{
  String disp;

  public Premium( String d ){ // コンストラクタ
    disp = new String( d ); // ディスプレイの解像度
    mem = mem*2; // メモリは2倍
    hdd = hdd*4; // HDDは4倍
  }
  public void showSpecP(){
    System.out.print(", ディスプレイ:" + disp + "\n");
  }
}
class Normal // 普通仕様クラス
{
  int mem; // メモリ
  int hdd; // ハードディスク

  public Normal(){
    mem = 8; // メモリ(GB)
    hdd = 256; // ストレージ(GB)
  }
  public void showSpecN(){
    System.out.print( " メモリ:" + mem + "GB, HDD:" + hdd + "GB");
  }
}
class Ensyu1305
{
  public static void main(String[] args)
  {
    // ノーマル仕様PCクラスのオブジェクト
    System.out.println("\n- ノーマル仕様PC ----");
    Normal no = new Normal();
    no.showSpecN();

    // プレミアム仕様PCクラスのオブジェクト
    System.out.println("\n\n- プレミアム仕様PC ----");
    Premium pr = new Premium("4K");
    pr.showSpecN();
    pr.showSpecP();
  }
}