2016-04-01 12 views
0

私は現在、夏の私の試験まで大学の模擬試験を修了しています。以下は私が今までに完成したコードです: 私は割り当ての部分の一つと苦労しています。 「テスター2では、startRobotというメソッドを作成します。このメソッドは、多態的メソッドになります。このメソッドは、RobotオブジェクトとScannerオブジェクトのオブジェクトを受け取ります。このメソッドの目的は、ロボットを起動することですロボットにタスクメソッド(ここではdoTaskメソッドの2つのバージョンを実行する必要があります)を実行し、ロボットを停止します。Java多態法

基本的に多態的なメソッドを作成し、最初にEntertainmentRobotをそれに渡して、次に2回目の呼び出しをHumanStudyRobotとするように要求します。テスターでこれを設定する方法がわからないので、コードを書くときにエラーが出るだけです。私は多形性の方法/多形性にも慣れていません。

ご協力いただければ幸いです。

package Program; 

import java.util.Scanner; 

public abstract class Robot { 

    //instance variables 
    protected double EnergyUnitsRequired; 
    protected double height; 
    protected String manufacturer; 
    protected String name; 
    protected String purpose; 
    protected double weight; 
    protected double energy; 

    //constructor 
    public Robot(String name, double height, double weight, String manufacturer) { 
     super(); 
     this.EnergyUnitsRequired = 0.25; 
     this.height = height; 
     this.manufacturer = manufacturer; 
     this.name = name; 
     this.purpose = "The robot's purpose needs to be provided"; 
     this.weight = weight; 
     this.energy = 0.0; 
    } 

    //accessors & mutators 
    public double getEnergyUnitsRequired() { 
     return EnergyUnitsRequired; 
    } 

    public double getHeight() { 
     return height; 
    } 

    public String getManufacturer() { 
     return manufacturer; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getPurpose() { 
     return purpose; 
    } 

    public double getWeight() { 
     return weight; 
    } 

    public double getEnergy() { 
     return energy; 
    } 

    public void setEnergyUnitsRequired(double energyUnitsRequired) { 
     EnergyUnitsRequired = energyUnitsRequired; 
    } 

    public void setHeight(double height) { 
     this.height = height; 
    } 

    public void setManufacturer(String manufacturer) { 
     this.manufacturer = manufacturer; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setPurpose(String purpose) { 
     this.purpose = purpose; 
    } 

    public void setWeight(double weight) { 
     this.weight = weight; 
    } 

    //methods 
    public abstract void start(); 
    public abstract void stop(); 
    public abstract void doTask(); 
    public abstract void doTask(Scanner input); 

    public void energyConsumption() { 
     System.out.println("The robot: " + getName() + " has: " + getEnergy() + " to begin with."); 
     double priorEnergy = getEnergy(); 
     energy = energy - energyRequired(); //the variable energyRequired should be returned from the energyRequired method below this method. 
     System.out.println("My energy has depleted by the following amount: " + (priorEnergy - energy) + " units."); 
     System.out.println("My energy is now at: " + energy + " units."); 
    } 

    public double energyRequired() { 
     double energyRequired = (EnergyUnitsRequired * weight); 
     return energyRequired; 
    } 

    public void regenerate() { 
     energy = getEnergy() + (getWeight() * 2); 
     System.out.println("More energy is being generated for the robot."); 
    } 
} 

package Program; 

import java.util.Scanner; 

public class HumanStudyRobot extends Robot { 

    //instance variables 

    public HumanStudyRobot(String name, double height, double weight, String manufacturer) { 
     super(name, height, weight, manufacturer); 
     this.energy = 30.0; 
    } 

    @Override 
    public void start() { 
     System.out.println("This is a Human Study Robot"); 
     System.out.println("The robot has started studying."); 
    } 

    @Override 
    public void stop() { 
     System.out.println("The robot has finished studying."); 
    } 

    @Override 
    public void doTask() { 
    study(); 

    } 

    @Override 
    public void doTask(Scanner input) { 
     // TODO Auto-generated method stub 

    } 

    public void study() { 
    if (energy >= energyRequired()) { 
     energyConsumption(); 
    } 
    else 
     if (energy < energyRequired()) { 
      System.out.println("The robot does not have sufficient energy."); 
      regenerate(); 
      System.out.println("................"); 
      System.out.println("The robot has finished regenerating"); 
     } 
    } 


    public String toString() { 
     return "I AM A HUMAN STUDY ROBOT : \nThe details of the entertainment robot are below:\n" 
       + "Name : " + getName() + "\nWeight: " + getWeight() + "\nHeight: " 
       + getHeight() + "\nManufacturer : " + getManufacturer() + "\nPurpose : " 
       + getPurpose(); 
    } 



} 

package Program; 

import java.util.Scanner; 

import org.omg.Messaging.SyncScopeHelper; 

public class EntertainmentRobot extends Robot { 

    //constructor 
    public EntertainmentRobot(String name, double height, double weight, String manufacturer) { 
     super(name, height, weight, manufacturer); 
     this.energy = 10.0; 
     this.EnergyUnitsRequired = 0.75; 
    } 

    @Override 
    public void start() { 
     System.out.println("This is an Entertainment Robot. \n The robot has started entertaining."); 
    } 

    @Override 
    public void stop() { 
     System.out.println("The Entertainment RObot has finsihed entertaining"); 
    } 

    @Override 
    public void doTask() { 

    } 

    @Override 
    public void doTask(Scanner input) { 
     play(); 
    } 

    public void talk() { 

    } 

    public void play() { 
     System.out.println("How many times would you like to play?"); 
     if (getEnergy() >= energyRequired()) { 
      energyConsumption(); 
     } 
     else 
      if (getEnergy() < energyRequired()) { 
       System.out.println("The robot does not have sufficient energy to play."); 
       regenerate(); 
       System.out.println("The robot is regenerating"); 
       System.out.println("........................."); 
       System.out.println("The robot has finished regenerating!"); 
      } 
    } 

    public String toString() { 
     return "\nI AM AN ENTERTAINMENT ROBOT \nThe details of the entertainment robot are below: \n" + 
       "Name : " + getName() + "\nHeight: " + getHeight() + "\nWeight: " + getWeight() + "\nManufacturer: " + 
       getManufacturer() + "\nPurpose: " + getPurpose(); 
    } 

} 


package Program; 

import java.util.Scanner; 

public class Tester02 { 

    public static void main(String[] args) { 

     HumanStudyRobot HumanStudyRobot1 = new HumanStudyRobot("HRP", 1.5, 58.0, "Kawada Industries"); 
     HumanStudyRobot1.setPurpose("Study into human movement and perform a wide range of tasks."); 
/*  
     System.out.println(HumanStudyRobot1.toString()); 

     HumanStudyRobot1.start(); 
     HumanStudyRobot1.study(); 
     HumanStudyRobot1.stop();*/ 

     public void startRobot(Robot, Scanner input){ 

     } 


     EntertainmentRobot EntertainmentRobot1 = new EntertainmentRobot("QRIO", 0.6, 7.3, "SONY"); 
     EntertainmentRobot1.setPurpose("To live with you, make life fun and make you happy."); 

     System.out.println(HumanStudyRobot1.toString()); 
     System.out.println(EntertainmentRobot1.toString()); 
    } 

} 
+0

ロボットをパラメータとして要求された方法を作成するだけです。その後、その関数を2回呼び出すと、最初に1つのタイプのロボットを渡し、もう1つをもう一方に渡します。次に、そのメソッド内でRobotの共通インターフェース/基本クラスを使用するだけです。そして、ロボットの各サブクラスは、あらゆる種類のロボットをパラメータとして取るということを考えれば、あなたのメソッドによって管理されることが保証されます。 – ManoDestra

+0

親クラスでメソッドを作成し、子クラスでそれをオーバーライドする必要があります。 –

+0

また、多態性に慣れていないのは不思議ですが、 'start'、' stop'、 'doTask'メソッドで実装しても問題ありません。おそらく忘れているのは、オブジェクトをインスタンス化する方法です。あなたは 'robot entertainmentRobot1 = new EntertainmentRobot(...);' –

答えて

1

まず観察:あなたのstartRobotメソッドの署名が無効である、

public void startRobot(Robot robot, Scanner input){ 

} 

第2の観察のように変更します。メインメソッドの外でメソッド宣言を移動します。

第3の観察:ロボットとスキャナのパラメータでメインメソッドからstartRobotを呼び出します。

startRobot(EntertainmentRobot1, /*your scanner*/); 
startRobot(HumanStudyRobot1, /*your scanner*/); 

両方のクラスはRobotクラスを拡張しているので - 彼らはstartRobotメソッドに渡すことができます。このトピックの詳細については、Oracle Documentationを参照してください。

0

ここでコードに問題はありません。あなたは多形性の原理を理解していないので、私はあなたがそれに取り組む必要があると思います。 3ロボットクラス Robotクラスを抽象クラスに拡張し、サブクラスは スーパークラス抽象メソッドをオーバーライドする必要があります。あなたのケースでは抽象メソッドは、すべてのロボットクラスがサブクラスであるため、IMは正確にやってあ​​なたは、あなたと同じコース上のこの

Robot humanrobot = new HumanStudyRobot("parameters..."); 
Robot entertainment = new EntertainmentRobot("parameters"); 

public void startRobot(Robot robot, Scanner input){ 
robot.dotask(); 
} 

startRobot(humanrobot, scanner); 
startRobot(entertainment, scanner); 
+0

そこのstartRobotからScannerパラメータがありません:) – ManoDestra

+0

申し訳ありません!今修正されました – Priyamal

0

イムほぼ確実イムのようなもの を行うことができます

public abstract void start(); 
public abstract void stop(); 
public abstract void doTask(); 
public abstract void doTask(Scanner input); 

です

public static void main(String[] args) { 
    EntertainmentRobot jim = new EntertainmentRobot(0.6, "SONY", "QRIO", "To live with you, make life fun and make you happy", 7.3); 
    HumanStudyRobot jeff = new HumanStudyRobot(1.5, "Kawada Industries", "HRP", "Study into human movement and perfrom a wide range of tasks", 58.0); 
    //System.out.println(jim); 
    //System.out.println(jeff); 
    //EntertainmentRobot jim = robot(); 
    Scanner input = new Scanner(System.in); 
    startRobot(jim, input); 
    startRobot(jeff, input); 

} 

public static void startRobot(Robot robot, Scanner input) { 
    /* 
    * POLYMORPHIC METHOD 
    * Accept an object of type robot 
    * Scanner object 
    * Start the robot 
    * Get robot to undertake a task 
    * stop the robot 
    */ 
    robot.start(); 
    robot.doTask(); 
    robot.stop();`enter code here` 

} 

}

:同じ質問、とにかく以上の人が言ったことを見た後、私はこれを行うことによって、この作業を得ました