我目前正在参加夏季考试前的大学预科考试。以下是我目前完成的代码: 我正在努力完成任务的其中一个部分。它要求我做的是:"在tester02中,创建一个名为startRobot的方法,它将是一个多态方法。此方法将接受Robot类型的对象和Scanner对象。该方法的目的是启动机器人,让机器人进行任务方法(你必须在这里运行两个版本的doTask方法),然后停止机器人。"
所以基本上它要求我创建多态方法并调用它两次,第一次将EntertainmentRobot
传递给它,然后第二次传递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 :(得分:1)
第一次观察:您的startRobot
方法签名无效,请将其更改为
public void startRobot(Robot robot, Scanner input){
}
第二次观察:将方法声明移到main方法之外。
第三次观察:使用机器人和扫描仪参数从main方法调用startRobot
。
startRobot(EntertainmentRobot1, /*your scanner*/);
startRobot(HumanStudyRobot1, /*your scanner*/);
由于这两个类都扩展了Robot
类 - 它们可以传递给startRobot
方法。有关此主题的进一步阅读,请参阅Oracle Documentation。
答案 1 :(得分:0)
这里的代码没有错。既然你不了解多态原则,我认为你需要对此进行研究; 3个机器人课程
扩展FROM my-php-base-image
类是一个抽象类,子类必须重写
超类抽象方法。在您的情况下,抽象方法是
Robot
由于所有机器人类都是子类,因此您可以执行某些操作 像这样
public abstract void start();
public abstract void stop();
public abstract void doTask();
public abstract void doTask(Scanner input);
答案 2 :(得分:0)
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`
}
}