- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在完成大学模拟考试,准备夏季考试。下面是我到目前为止已经完成的代码:我正在努力完成作业的一部分。它要求我做的是:“在 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());
}
}
最佳答案
第一次观察:您的 startRobot
方法签名无效,请将其更改为
public void startRobot(Robot robot, Scanner input){
}
第二个观察:将方法声明移到主方法之外。
第三个观察结果:使用机器人和扫描仪参数从主方法调用 startRobot
。
startRobot(EntertainmentRobot1, /*your scanner*/);
startRobot(HumanStudyRobot1, /*your scanner*/);
由于这两个类都扩展了 Robot
类 - 它们可以传递给 startRobot
方法。有关此主题的更多阅读,请参阅 Oracle Documentation .
关于Java多态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362956/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!