我附上了我在过去试图完成的过去论文中给出的指示图片。
我目前正在参加夏季考试前的大学预科考试。到目前为止,我已经完成了大部分问题的论文,但我目前仍然坚持这一部分。
我按照指示实现了接口,并且我还实现了一个名为directions的枚举。如果它显示“娱乐机器人类中的Walk and Talk方法的详细信息”,我不确定如何将用户的输入存储为枚举。到目前为止,我的代码如下所示。我将不胜感激任何帮助。非常感谢。
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;
private Directions direction;
//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;
}
public Directions getDirection() {
return direction;
}
public void setDirection(Directions direction) {
this.direction = direction;
}
//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.");
System.out.println("............................");
System.out.println("I have now got more energy!.");
}
}
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. \nThe robot has started entertaining.");
}
@Override
public void stop() {
System.out.println("I have stopped entertaining people.");
System.out.println("--------------------------------------------------");
}
@Override
public void doTask(Scanner input) {
play();
}
@Override
public void doTask() {
// TODO Auto-generated method stub
}
public void talk() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a phrase for me to repeat");
String phrase = input.nextLine();
input.nextLine();
System.out.println("You have asked me to say the following phrase : " + phrase);
}
public void walk() {
Scanner input = new Scanner (System.in);
System.out.println("Would you like me to walk for you?");
if (input.nextLine().equalsIgnoreCase("Y")) {
System.out.println("For how many paces");
int steps = input.nextInt();
System.out.println("Which direction do you want me to walk? (Enter a number between 1 and 4.) ");
System.out.println("1 - " + Directions.FORWARD);
System.out.println("2 - " + Directions.BACKWARD);
System.out.println("3 - " + Directions.LEFT);
System.out.println("4 - " + Directions.RIGHT);
}
}
public void play () {
Scanner input = new Scanner(System.in);
boolean valid = false;
int selection;
do {
System.out.println("How many times would you like to play?");
while (!input.hasNextInt()) {
System.out.println("That is not a number");
input.nextLine();
}
selection = input.nextInt();
valid = true;
} while(!valid);
for (int i = 1; i < selection + 1; i ++ ) {
if (getEnergy() >= energyRequired()) {
energyConsumption();
} else if (getEnergy() < energyRequired()) {
System.out.println("------------WHOOPS--------------.");
System.out.println("I do not have enough energy to play.");
regenerate();
}
}
input.close();
}
public String toString() {
return "---------------------------------\nI AM AN ENTERTAINMENT ROBOT \nThe details of the entertainment robot are below: \n" +
"Name : " + getName() + "\nWeight: " + getWeight() + "\nHeight: " + getHeight() + "\nManufacturer: " +
getManufacturer() + "\nPurpose: " + getPurpose() + "\n----------------------------";
}
}
package Program;
import java.util.Scanner;
public interface Talkable {
abstract void talk(Scanner input);
}
package Program;
import java.util.Scanner;
public interface Walkable {
abstract void walk(Scanner input);
}
package Program;
public enum Directions {
FORWARD,BACKWARD,LEFT,RIGHT;
}
答案 0 :(得分:0)
将方向键存储在枚举中,例如:
public enum Direction {
FORWARD(1), BACKWARD(2), LEFT(3), RIGHT(4);
final int num;
Direction(int num) {
this.num = num;
}
static Direction ofNumber(int num) {
for (Direction d : values())
if (d.num == num)
return d;
return null;
}
}
然后,当用户输入数字时,只需调用:
Direction selected = Direction.ofNumber(number);