Report#3

体積・表面積公式クラスの作成{〜12/11(Mon)}
(1)3種以上の体積を求めるクラスを作成し、具体的な値を入力しプログラムを実
   行せよ。
(2)2種以上の表面積を求めるクラスを作成し、同様に実行せよ。 
(3)例題を参考に、階乗計算を再帰プログラムにより作成し、for文による階乗計
   算との違いを考察せよ。

(1)3種以上の体積を求めるクラスを作成し、具体的な値を入力しプログ ラムを実行せよ。


ソースプログラム

class VolumeInfo {
    double length;
    double side;
    double height;
    double radius;

VolumeInfo(double length,double side,double height,double radius){
    this.length = length;
    this.side = side;
    this.height = height;
    this.radius = radius;
   }
}
public class volume {
    public static void main( String args[] ) {
    VolumeInfo Hexahedron = new VolumeInfo(12.1,3.4,4.5,0);
    double answer1 =
    Hexahedron.length*Hexahedron.side*Hexahedron.height;
    System.out.println("Volume Hexahedron =" + answer1);
    VolumeInfo Globe = new VolumeInfo(0,0,0,5);
    double answer2 = Math.PI*Math.pow(Globe.radius,3)*4/3;
    System.out.println("Volume Globe =" + answer2);
    VolumeInfo Column = new VolumeInfo(0,0,12.5,8.3);
    double answer3 = Math.PI*Math.pow(Column.radius,2)*Column.height;
    System.out.println("Volume Column =" + answer3);
    }
}



実行結果

Volume Hexahedron =185.13
Volume Globe =523.5987755982989
Volume Column =2705.303973822511

考察


(1)クラスVolumeInfo内にコンストラクタを用意する。
(2)コンストラクタでは体積を求めるための情報である「縦」「横」「高さ」
「半径」の
   double型の引数を4つ受け取る。
(3)thisキーワードを使用してインスタンス変数を参照。
(4)volumeクラスでは、new演算子を使ってVolumeImfoクラスのインスタンス
を作成。
(5)オブジェクトへの参照はローカル変数 Hexahedron  Globe  Column に代
入する。
(6)上から順に直方体、球、円柱の体積を求めて、println()を使い体積の値
を出力する。



(2)2種以上の表面積を求めるクラスを作成し、同様に実行せよ。


ソースプログラム

class SurfaceInfo {
    double length;
    double side;
    double height;
    double radius;

    SurfaceInfo(double length,double side,double height,double radius){
    this.length = length;
    this.side = side;
    this.height = height;
    this.radius = radius;
    }
}
public class surface {
    public static void main( String args[] ) {
    SurfaceInfo Globe = new SurfaceInfo(0,0,0,6);
    double answer1 = 4*Math.PI*Math.pow(Globe.radius,2);
    System.out.println("Surface Globe =" + answer1);
    SurfaceInfo Column = new SurfaceInfo(0,0,18.5,6.8);
    double answer2 = Math.PI*Math.pow(Column.radius,2)+
        Column.height*2*Math.PI*Column.radius;
	System.out.println("Surface Column =" + answer2);
    }
}

実行結果

Surface Globe =452.3893421169302
Surface Column =935.691955945184

考察


 (1)クラスSurfaseInfo内にコンストラクタを用意する。
(2)コンストラクタでは表面積を求めるための情報である「縦」「横」「高さ」
「半径」の
   double型の引数を4つ受け取る。
(3)thisキーワードを使用してインスタンス変数を参照。
(4)volumeクラスでは、new演算子を使ってVolumeImfoクラスのインスタンス
を作成。
(5)オブジェクトへの参照はローカル変数 Globe  Column に代入する。
(6)上から順に球、円柱の表面積を求めて、println()を使い表面積の値を出
力する。

(3)例題を参考に、階乗計算を再帰プログラムにより作成し、for文による 階乗計算との違いを考察せよ。


ソースプログラム

import java.io.*;

class Recursive {
    public static void main(String args[]) throws Exception {

        BufferedReader in = new BufferedReader(new
        InputStreamReader(System.in)\);

        System.out.print("input natural number: ");
        int num = (new Integer(in.readLine())).intValue();

        System.out.print("再帰呼び出しによる階乗計算 => ");
        System.out.println(num + "!=" + detail(num));

    }

    static int detail(int n) {
        if(n == 0) return(1);
        return detail(n-1)*n;
    }
}



実行結果

input natural number: 5
再帰呼び出しによる階乗計算 => 5!=120

考察


(1)detail(int n)メソッド内でfor文と同じ動きをするようした。
(2)もし、nが0なら1を返す。これは階乗のf(0)=1と同じ処理。
(3)detail(n-1)でn=1になった時から自分を読む再帰が始まる。
(4)例えば、n=3の時、detail(n-1)にきた時にdetailブロックの最初に戻り処
理 をする。要するに順番に2*3,1*3,0*3の処理をする。
(5)再帰プログラムは階乗計算のfor文の処理をfor文を用いずにできる。


感想


もう少しで冬休みだー!がんばるぞー!!