抽象クラスComputerと、それを実装した、PersonalComputer、TabletPC、およびSmartPhoneクラスを操作するプログラムがある。Computerクラス、PersonalComputerクラス、Problemex.javaを参考に、TalbetPCクラス、およびSmartPhoneクラスを完成させ、期待する実行結果と同じものが得られるようにしたい。
私が書いたコードはこちらです。
Java
1Computerクラス 2public abstract class Computer { 3 // コンピュータのタイプ 4 private String type; 5 6 // コンストラクタ 7 public Computer(String type) { 8 this.type = type; 9 } 10 11 // 入力処理 12 public abstract void input(); 13 14 // 出力処理 15 public abstract void output(); 16 17 // 通信処理 18 public void communication() { 19 System.out.println("インターネットで通信"); 20 } 21 22 // タイプの出力 23 public void showType() { 24 System.out.println("コンピュータの種類:" + type); 25 } 26 27} 28 29
Java
1personalcomputerクラス 2public class PersonalComputer extends Computer{ 3 // コンストラクタ 4 public PersonalComputer(){ 5 super("パーソナルコンピュータ"); 6 } 7 // 入力処理 8 public void input(){ 9 System.out.println("キーボード・マウスで入力"); 10 } 11 // 出力処理 12 public void output(){ 13 System.out.println("ディスプレイに出力"); 14 } 15} 16
Java
1TabletPCクラス 2public class TabletPC extends Computer { 3 4 public TabletPC() { 5 super("タブレットPC"); 6 } 7 // 8 // public void showType() { 9 // String type = "タブレットPC"; 10 // System.out.println("コンピューターの種類" + type); 11 // } 12 13 public void input() { 14 System.out.println("タッチパネルディスプレイをタップして操作"); 15 } 16 17 public void output() { 18 System.out.println("タッチパネルディスプレイに出力"); 19 } 20 21 public void communication() { 22 System.out.println("インターネットで通信"); 23 } 24 25} 26
Java
1SmartPhoneクラス 2public class SmartPhone extends Computer { 3 4 public SmartPhone() { 5 super("スマートフォン"); 6 } 7 8 public void ShowType() { 9 // String type = "スマートフォン"; 10 System.out.println("コンピューターの種類:" + "type"); 11 12 } 13 14 public void input() { 15 System.out.println("タッチパネルディスプレイをタップして操作"); 16 } 17 18 public void output() { 19 System.out.println("タッチパネルディスプレイに出力"); 20 } 21 22 public void communication() { 23 System.out.println("インターネットと電話回線で通信"); 24 } 25 26} 27 28
Java
1public class Problemex { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 Computer[] cp = new Computer[3]; 9 cp[0] = new PersonalComputer(); // パーソナルコンピュータ 10 cp[1] = new TabletPC(); // タブレットPC 11 cp[2] = new SmartPhone(); // 12 for (Computer c : cp) { 13 c.showType(); 14 c.input(); 15 c.output(); 16 c.communication(); 17 System.out.println("--------------------------------------"); 18 } 19 } 20} 21 22 23
期待される実行結果
コンピュータの種類:パーソナルコンピュータ
キーボード・マウスで入力
ディスプレイに出力
インターネットで通信
コンピュータの種類:タブレットPC
タッチパネルディスプレイをタップして操作
タッチパネルディスプレイに出力
インターネットで通信
コンピュータの種類:スマートフォン
タッチパネルディスプレイをタップして操作
タッチパネルディスプレイに出力
インターネットと電話回線で通信
実行結果は期待値と同じになるのですが SmartPhoneクラスと TabletPCクラスが何が間違っているのかわかりません。
大変申し訳ございませんがご教示お願いします。
回答4件
あなたの回答
tips
プレビュー