オブジェクト指向の基礎計算問題
次のコードを実行したとき、出力結果として正しいものはどれですか? class Rectangle { private int width; private int height; Rectangle(int w, int h) { this.width = w; this.height = h; } int calculateArea() { return width * height; } } public class Main { public static void main(String[] args) { Rectangle rect = new Rectangle(12, 8); System.out.println(rect.calculateArea()); } }
A.20
✗ 幅と高さの合計です。面積は幅と高さの積で計算します。
B.80
✗ 高さのみに12を乗じた値です。正しくは幅12と高さ8の積です。
C.96← 正解
✓ 正解です。幅12 × 高さ8 = 96が面積となります。
D.40
✗ 幅と高さの平均に10を乗じた値です。面積計算として誤りです。