带有枚举实现的Java方法
我已经附上了一张我在这篇过去的论文中给出的说明的图片,我目前正试图完成 我目前正在完成一项大学实践考试,准备夏季考试。到目前为止,我已经完成了大部分的问题文件,但我目前停留在这一部分 我已经按照说明实现了接口,并且还实现了一个名为directions的枚举。其中提到“娱乐机器人类中行走和交谈方法的详细信息”,我不确定如何将用户的输入存储为枚举。我的代码如下所示。如果能在这方面得到任何帮助,我将不胜感激。非常感谢带有枚举实现的Java方法,java,methods,input,enums,Java,Methods,Input,Enums,我已经附上了一张我在这篇过去的论文中给出的说明的图片,我目前正试图完成 我目前正在完成一项大学实践考试,准备夏季考试。到目前为止,我已经完成了大部分的问题文件,但我目前停留在这一部分 我已经按照说明实现了接口,并且还实现了一个名为directions的枚举。其中提到“娱乐机器人类中行走和交谈方法的详细信息”,我不确定如何将用户的输入存储为枚举。我的代码如下所示。如果能在这方面得到任何帮助,我将不胜感激。非常感谢 package Program; import java.util.Scanner
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;
}
打包程序;
导入java.util.Scanner;
公共抽象类机器人{
//实例变量
受保护的双能源系统;
保护双高;
保护管柱制造商;
受保护的字符串名称;
保护串用途;
保护双重重量;
双能量保护;
私人指示;
//建造师
公共机器人(字符串名称、双高、双重、字符串制造商){
超级();
该值为0.25;
高度=高度;
这个。制造商=制造商;
this.name=名称;
this.purpose=“需要提供机器人的用途”;
重量=重量;
该能量=0.0;
}
//存取器和变异器
public double getEnergyUnitsRequired(){
返回所需的能量;
}
公众双倍身高(){
返回高度;
}
公共字符串getManufacturer(){
退货制造商;
}
公共字符串getName(){
返回名称;
}
公共字符串getPurpose(){
返回目的;
}
公共双getWeight(){
返回重量;
}
公共能源({
返回能量;
}
公共无效设置energyUnitsRequired(双energyUnitsRequired){
EnergyUnits Required=EnergyUnits Required;
}
公共空间设置高度(双倍高度){
高度=高度;
}
公共无效设置制造商(字符串制造商){
这个。制造商=制造商;
}
公共void集合名(字符串名){
this.name=名称;
}
公共用途(字符串用途){
这个目的=目的;
}
公共空隙设定重量(双倍重量){
重量=重量;
}
公共方向{
返回方向;
}
公共空间设置方向(方向){
这个方向=方向;
}
//方法
公共摘要void start();
公共抽象无效停止();
公共抽象void doTask();
公共摘要无效点任务(扫描仪输入);
公共能源消耗(){
println(“机器人:“+getName()+”以“+getEnergy()+”开头”);
双优先级能量=getEnergy();
energy=energy-energyRequired();//变量energyRequired应从该方法下面的energyRequired方法返回。
println(“我的能量消耗量如下:”+(先验能量-能量)+“单位”);
System.out.println(“我的能量现在是:“+能量+”单位”);
}
公共双能源需求(){
双能源需求=(能源需求*重量);
返回所需能量;
}
公共无效重新生成(){
能量=getEnergy()+(getWeight()*2);
System.out.println(“正在为机器人产生更多的能量”);
系统输出打印项次(…………);
System.out.println(“我现在有更多的能量了!”);
}
}
一揽子计划;
导入java.util.Scanner;
公共类HumanStudyRobot扩展了Robot{
//实例变量
公共HumanStudyRobot(字符串名称、双倍高度、双倍重量、字符串制造商){
super(名称、高度、重量、制造商);
该能量=30.0;
}
@凌驾
公开作废开始(){
System.out.println(“这是一个人类研究机器人”);
System.out.println(“机器人已经开始学习了。”);
}
@凌驾
公共停车场(){
System.out.println(“机器人已完成学习”);
}
@凌驾
公共void doTask(){
研究();
}
@凌驾
公共void点任务(扫描仪输入){
//TODO自动生成的方法存根
}
公共空间研究(){
如果(能量>=energyRequired()){
能量消耗();
}
其他的
如果(能量<能量需求()){
System.out.println(“机器人没有足够的能量。”);
再生();
系统输出打印项次(“……”);
System.out.println(“机器人已完成再生”);
}
}
公共字符串toString(){
return“我是人类研究机器人:\n娱乐机器人的详细信息如下:\n”
+名称:“+getName()+”\n重量:“+getWeight()+”\n重量:
+getHeight()+“\n制造商:“+getManufacturer()+”\n目的:
+getPurpose();
}
}
一揽子计划;
导入java.util.Scanner;
导入org.omg.Messaging.SyncScopeHelper;
公共类娱乐机器人扩展机器人{
//建造师
公共娱乐机器人(字符串名称、双高、双重、字符串制造商){
super(名称、高度、重量、制造商);
该能量=10.0;
该值为0.75;
}
@凌驾
公开作废开始(){
System.out.println(“这是一个娱乐机器人。\n机器人已经开始娱乐了。”);
}
@凌驾
公共停车场(){
System.out.println(“我不再招待别人了。”);
System.out.println(“-------------------------------------------------------------”;
}
@凌驾
公共void点任务(扫描仪输入){
玩
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);