次の課題 1~7 の を埋めてプログラムを完成させよ 1. 整数型の配列に格納されたデータの総和を計算し, その結果を出力するプログラムである このプログラムの処理手順を次に示す 1 配列の格納するデータの個数 n (n>0) を入力する 2n の大きさで配列を確保する 3 配列に n 個分のデータを格納する 4 配列の総和を求める 5 総和を出力する import java.io.*; public class Prog090114_1{ public static void main(string[] args) throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(buf.readLine()); // 入力を標準入力より取得 int [] a = 1 int[n]; int sum = 0; for (int i = 0; i < n; i++){ // 入力データのカウント a[i] = Integer.parseInt(buf. 2 ()); for (int i = 0; i < n; i++){ // 表示する文字数のカウントと表示 3 +=a[i]; System.out.println(" 総和 = "+sum); 2. 図形の面積を計算し, その結果を出力するプログラムである 図形は, 長方形及び正方形のいずれかであり, 図形オブジェクトは, 次のような属性をもつ 長方形 : 縦と横の長さ正方形 : 一辺の長さ このプログラムは, 次の一つのインタフェースと四つのクラスで構成される (1)Figure 図形のインタフェースであり, 面積を計算してその結果を返す抽象メソッド getarea を宣言している (3)Rectangle 長方形のクラスであり, データを文字列で返すメソッド tostring と長方形の面積を計算してその結果を返すメソッド getarea を実装している (4)Square 正方形のクラスであり, データを文字列で返すメソッド tostring を実装している (5)Prog090114_2 メソッド main をもつクラスであり, 次の処理を行う 1 三角形, 長方形及び正方形のデータを生成し, 配列 figures に設定する 2 面積を求めて, 結果を出力する プログラムの実行結果を次に示す 実行結果 Rectangle : width = 5.0, height = 8.0 : area = 40.0 Square : width = 5.0 : area = 25.0 interface Figure{ public double getarea(); class Rectangle implements Figure{ double length1; double length2; 1
public Rectangle(double length1, double length2){ this.length1 = length1; this.length2 = length2; public String tostring(){ return "\n Rectangle : width = " + length1 + ", height = " + length2 + " : " ; public double 4 (){ return length1 * length2 ; class Square extends 5 { public Square(double length1){ 6 (length1, length1); public String tostring(){ return "\n Square : width = " + length1 + " : "; public class Prog090114_2{ public static void main(string args[]){ Rectangle r1 = new Rectangle(5, 8); Square s1 = new Square(5); 7 [] figures = {r1, s1; for (int i = 0; i < 8.length; i++){ System.out.println(figures[i] + " area = " + figures[i].getarea()); 3. 2 人のユーザが1つの口座を共有し, 各ユーザは共有している口座に対して 10 円を 10 回づつ預金し, その最終額を出力するプログラムである class Kouza{ private int zandaka = 0; public synchronized void add(int yen){ int tmp = zandaka; System.out.println(Thread.currentThread().getName() + ": 現在額 =" + tmp + ": 加算額 =" + yen + ": 合計額 =" + (tmp+yen)); tmp += yen; 9 = tmp; public int 10 (){ return zandaka; class User extends 11 { Kouza acnt; public User(String name, Kouza a){ super(name); acnt = a; public void run(){ for(int i = 0; i < 10; i++){ acnt. 12 (10); public class Prog090114_3{ public static void main(string[] args){ 2
Kouza kouza = new Kouza(); User user1 = new User("th1", kouza); User user2 = new User("th2", kouza); user1. 13 (); user2. 14 (); try{ user1.join(); user2.join(); catch(interruptedexception e){ e.printstacktrace(); System.out.println(" 最終額 =" + kouza.getzandaka()); 2011/01/19 OOP 演習追加課題 4. 線描画を paint() メソッド内で行なうプログラムである import java.applet.applet; import java.awt.graphics; /* <applet code = "Prog090114_4.class" width = 300 height = 200></applet> */ public class Prog090114_4 extends 15 { public void paint( 16 g){ g. 17 (10, 10, 50, 100); 5. ボタンが押されたときに ボタンに表示されている start が [stop] に変わるプログラムである import java.applet.applet; import java.awt. 18 ; import java.awt.event. 19 ; import java.awt.event.actionevent; /* <applet code = "Prog090114_5.class" width = 300 height = 200></applet> */ public class Prog090114_5 extends 20 implements ActionListener{ Button bt; public void 21 (){ bt = new Button("start"); 22 (bt); bt. 23 (this); public void 24 (ActionEvent ae){ bt. 25 ("stop"); 3
6. 乗り物に関するデータを設定して その内容を出力するプログラムである 26~35 の空欄を適切に埋めよ 2011/01/19 OOP 演習追加課題 interface Vehicle{ void display(); class Car 26 Vehicle{ 27 int num; 28 double gas; public Car(int n, double g){ num = n; gas = g; public void 29 { System.out.println("number = " + num + ", gas = " + gas); class Plane 30 Vehicle{ 31 int flight; public Plane(int f){ flight = f; public void 32 { System.out.println("flight = " + flight); public class VehicleTest{ public static void main(string[] args){ 33 [] vc = new Vehicle[2]; vc[0] = 34 Car(1234, 50.5); vc[1] = 35 Plane(246); for(int i = 0; i < vc.length; i++){ vc[i].display(); /* number = 1234, gas = 50.5 flight = 246 */ 4
7.Animal クラスを継承するプログラムである /*PetShop.java*/ import java.io.*; // 抽象クラス abstract class Animal{ long cryday=system.currenttimemillis();// 鳴いた時刻を記録 public long getcryday() { return cryday; public abstract void 36 ();// 抽象メソッド // 犬クラス class Dog extends Animal{ // メソッドの上書き public void voice(){ System.out.println(" ワンワン "); // 新メソッドの追加 public void voicee(){ System.out.println("Bow-Wow"); // 猫クラス class Cat extends Animal{ public void voice(){ System.out.println(" ニャンニャン "); public void voicee(){ System.out.println("Mew"); // 鶏クラス class Cook extends Animal{ public void voice(){ System.out.println(" コケコッコ-"); public void voicee(){ System.out.println("Cock-A-Doodle-Doo"); // 動作確認のクラス public class 37 { public static void main(string[] args) throws IOException { while(true){ System.out.println(" 聞きたい動物の声を選んでください "); System.out.println(" 犬 =1, 猫 =2, 鶏 =3"); BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); int in; in = Integer.parseInt(buf. 38 ()); if(in!=1 && in!=2 && in!=3){ System.out.println(" 何も鳴きませんでした "); break; 5
System.out.println(" 国籍を選んでください "); System.out.println(" 外国 =f, 日本 =j"); BufferedReader bs = new BufferedReader(new InputStreamReader(System.in)); String str; str =bs. 38 (); char cha = str.charat(0); // 動物を参照する変数 39 an1,an2,an3; // インスタンス作成する an1=new Dog(); an2=new Cat(); an3=new Cook(); if (in == 1 ){ if (cha == 'j'){ System.out.println(" 犬の鳴いた時刻 :"+an1. 40 ()); an1.voice(); else if (cha == 'f'){ System.out.println(" 犬の鳴いた時刻 :"+an1. 40 ()); ((Dog)an1).voiceE(); else{ System.out.println(" 犬は鳴きませんでした "); break; if (in == 2 ){ if (cha == 'j'){ System.out.println(" 猫の鳴いた時刻 :"+an2. 40 ()); an2.voice(); else if (cha == 'f'){ System.out.println(" 猫の鳴いた時刻 :"+an2. 40 ()); ((Cat)an2).voiceE(); else{ System.out.println(" 猫は鳴きませんでした "); break; if (in == 3 ){ if (cha == 'j'){ System.out.println(" 鶏の鳴いた時刻 :"+an3. 40 ()); an3.voice(); else if (cha == 'f'){ System.out.println(" 鶏の鳴いた時刻 :"+an3. 40 *()); ((Cook)an3).voiceE(); else{ System.out.println(" 鶏は鳴きませんでした "); break; 注 ) システム時刻までの経過時間を取得するには System クラスの currenttimemillis メソッドを使います 6
1 21 2 22 3 23 4 24 5 25 6 26 7 27 8 28 9 29 10 30 11 31 12 32 13 33 14 34 15 35 16 36 17 37 18 38 19 39 20 40 7
解答群 ア ActionListener イ actionperformed ウ add エ addactionlistener オ Applet カ Button キ drawline ク Figure ケ figures コ getarea サ getzandaka シ Graphics ス init セ new ソ readline タ Rectangle チ setlabel ツ start テ sum ト super ナ Thread ニ zandaka ヌ voice ネ Animal ノ PetShop ハ getcryday ヒ voicee フ Dog ヘ Cat ホ Cock マ implements ミ interface ム readline メ parseint 8