06020
1年後期>report3


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

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


1-1.プログラム
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class rectangular {
    float r_len, r_wid, r_hei;
    float rec (float r_len, float r_wid,float r_hei) {
        return r_len * r_wid * r_hei;
    }
}

class sphere {
    double s_rad;
    double sph (double s_rad) {
        return 4/3 * 3.14 * s_rad * s_rad * s_rad;
    }
}

class cylinder {
    double c_rad, c_hei;
    double cyl (double c_rad, double c_hei) {
        return 3.14 * c_rad * c_rad * c_hei;
    }
}

class report3_1 {
    public static void main (String args[]) {
        double r_volume;
        rectangular obj1= new rectangular();
        float r_length = 3;
        float r_width = 4;
        float r_height = 2;
        r_volume = obj1.rec(r_length, r_width, r_height);
        System.out.print("縦 " + r_length + ", 横 " + r_width + ", 高さ " + r_height);
        System.out.println(", の直方体の体積は " + r_volume);

        double s_volume;
        sphere obj2 = new sphere();
        double s_radius = 3;
        s_volume = obj2.sph(s_radius);
        System.out.print("半径 " + s_radius);
        System.out.println(", の球の体積は " + s_volume);

        double c_volume;
        cylinder obj3 = new cylinder();
        double c_radius = 2;
        double c_height = 8;
        c_volume = obj3.cyl(c_radius,c_height);
        System.out.print("半径 " + c_radius +", 高さ " + c_height);
        System.out.println(", の円柱の体積は " + c_volume);
    }
}


1-2.実行結果
縦 3.0, 横 4.0, 高さ 2.0, の直方体の体積は 24.0
半径 3.0, の球の体積は 84.78
半径 2.0, 高さ 8.0, の円柱の体積は 100.48


1-3.考察
1〜6行目は直方体の体積を求めるクラスである。設定された縦,横,高さの値をreturnの位置で計算し体積を求める。
体積を求める公式は「縦 * 横 * 高さ」である。

8〜13行目は球の体積を求めるクラスである。設定された球の半径の値から体積を計算する。
公式は「4/3 * 3.14 * 半径の3乗」である。

15〜20行目は円柱の体積を求めるクラスである。設定された半径と高さの値から体積を計算する。
公式は「3.14 * 半径の2乗 * 高さ」である。

22〜48行目はmainメソッドのあるクラスである。それぞれの図形の長さを設定し体積を表示する。
25行目ではnew演算子を使ってobj1を作っている。
26〜28行目で長さの値を実際に設定している。
29行目ではobj1のrecメソッドを呼び出し、先に設定した値を入力し直方体の体積を求め、出力している。
33〜38行目も上と同様にobj2のsphメソッドを呼び、球の体積を求めている。
40〜46行目も同様な動作で円柱の体積を求めている。




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

2-1.プログラム
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class surface_rec {
    float r_len, r_wid, r_hei;
    float rec (float r_len, float r_wid,float r_hei) {
        return 2 * ( r_len * r_wid  +  r_len * r_hei  +  r_wid * r_hei );
    }
}

class surface_sph {
    double s_rad;
    double sph (double s_rad) {
        return 4 * 3.14 * s_rad * s_rad;
    }
}


class report3_2 {
    public static void main (String args[]) {
        double r_volume;
        surface_rec obj1= new surface_rec();
        float r_length = 3;
        float r_width = 4;
        float r_height = 2;
        r_volume = obj1.rec(r_length, r_width, r_height);
        System.out.print("縦 " + r_length + ", 横 " + r_width + ", 高さ " + r_height);
        System.out.println(", の直方体の表面積は " + r_volume);

        double s_volume;
        surface_sph obj2 = new surface_sph();
        double s_radius = 3;
        s_volume = obj2.sph(s_radius);
        System.out.print("半径 " + s_radius);
        System.out.println(", の球の表面積は " + s_volume);

    }
}

2-2.実行結果
縦 3.0, 横 4.0, 高さ 2.0, の直方体の表面積は 52.0
半径 3.0, の球の表面積は 113.03999999999999


2-3.考察
1〜6行目は直方体の表面積を求めるクラスである。設定された縦,横,高さの値をreturnの位置で計算し表面積を求める。
表面積を求める公式は「2 * (縦*横 + 縦*高さ + 横*高さ)」である。

8〜13行目は球の表面積を求めるクラスである。設定された球の半径の値から表面積を計算する。
公式は「4 * 3.14 * 半径の2乗」である。

16〜35行目はmainメソッドのあるクラスである。それぞれの図形の長さを設定し表面積を表示する。
19行目ではnew演算子を使ってobj1を作っている。
20〜22行目で長さの値を実際に設定している。
23行目ではobj1のrecメソッドを呼び出し、先に設定した値を入力し直方体の表面積を求め、出力している。 27〜32行目も上と同様にobj2のsphメソッドを呼び、球の表面積を求めている。

基本的に公式が違うだけでやっていることは体積を求めるプログラムとなんら変わりは無い。




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

3-1.プログラム
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*; 
class report3_3 { 
  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("for文を使った階乗計算      => "); 
    System.out.println(num + "! =" + factfor(num)); 
    System.out.print("再帰呼び出しによる階乗計算 => "); 
    System.out.println(num + "! =" + factrec(num)); 
  } 
  /* for 文を使った階乗の計算 */ 
  static int factfor(int number) { 
    int factrial = 1; 
    for (int i = 1; i <= number; i++) { 
      factrial = i * factrial; 
    } 
    return(factrial); 
  } 
  /* 再帰による階乗の計算 */ 
  static int factrec(int number) { 
    if ( number == 0 ) return(1);
    return( number*factrec(number-1) ); 
  } 
} 


3-2.実行結果
input natural number: 5
for文を使った階乗計算      => 5! =120
再帰呼び出しによる階乗計算 => 5! =120


3-3.考察
for文を使った階乗の計算では、変数iを指定し、入力された値の数だけ繰り返す。
factrial = 2 * 1
factrial = 3 * 2
factrial = 4 * 6
factrial = 5 * 24
factrial = 120

再帰による階乗の計算では、入力された値から1ずつ引いていき、22行目のif文が実行されるまで21〜24が繰り返し呼び出される。
factrec(5)= 5 * factrec(4)
factrec(4)= 4 * factrec(3)
factrec(3)= 3 * factrec(2)
factrec(2)= 2 * factrec(1)
factrec(1)= 1 * factrec(0)
if ( 0 == 0 ) return(1)
よって、5*4*3*2*1*1 = 120となる。