平成15年 春期 基本情報技術者 午後 問08
問08 Java次の Java プログラムの説明及びプログラムを読んで,設問に答えよ。 〔プログラムの説明〕 図形の面積を計算し,その結果を出力するプログラムである。 図形は,三角形,長方形及び正方形のいずれかであり,プログラムでは, 次の属性をもつ図形オブジェクトとして定める。 三角形:3辺の長さ 長方形:縦と横の2辺の長さ 正方形:1辺の長さ AreaTest (1) 三角形,長方形及び正方形のオブジェクトを生成し,配列 figures に設定する。 (2) 各図形の面積を求めて,結果を出力する。 ここで,各図形には,正しい図形となる数値が与えられているものとする。 Figure Triangle Rectangle Square
図 実行結果 public class AreaTest { public static void main(String args[]) { Figure[] figures = { new Triangle(2, 3, 3), new Rectangle(5, 8), new Square(5)}; for (int i = 0; i < figures.length; i++) { System.out.println(figures[i] + "area = " + figures[i].getArea()); } } } 〔プログラム 2〕 public abstract class Figure { public abstract double getArea(); } 〔プログラム 3〕 public class Triangle extends { double la; double lb; double lc; public Triangle(double la, double lb, double lc) { this.la = la; this.lb = lb; this.lc = lc; } public String toString() { return "Triangle : sides = " + la + ", " + lb + ", " + lc + " : "; } public double getArea() { double s = (la + lb + lc) / 2.0; return Math.sqrt(s * (s - la) * (s - lb) * (s - lc)); } } 〔プログラム 4〕 public class Rectangle extends { double height; double width; public Rectangle(double height, double width) { this.height = height; this.width = width; } public String toString() { return "Rectangle : height = " + height + ", width = " + width + " : "; } public double getArea() { return ; } } 〔プログラム 5〕 public class Square extends { public Square(double width) { ; } public String toString() { return "Square : width = " + width + " : "; } } 設問 プログラム中の に入れる正しい答えを, 解答群の中から選べ。解答は,重複して選んでもよい。
a,b,d に関する解答群 ア abstract イ Figure ウ getArea エ Rectangle オ Square カ super c に関する解答群 ア height イ height * height ウ height * width エ width オ width * width e に関する解答群 ア super(height) イ super(height, height) ウ super(width) エ super(width, height) オ super(width, width) カ this.height = height キ this.height = width ク this.width = height ケ this.width = width
[←前の問題] [次の問題→] [問題一覧表] [分野別] [基本情報技術者試験TOP ] | |