繁体   English   中英

带有枚举实现的 Java 方法

[英]Java Methods with enum Implementation

我被要求做什么

我附上了一张我目前正在尝试完成的过去论文中给出的说明的图片。

我目前正在完成大学模拟考试,为我的夏季考试做准备。 到目前为止,我已经完成了大部分的试卷,但是我目前被困在这部分上。

我已经按照指示实现了接口,并且我还实现了一个名为方向的枚举。 它说“娱乐机器人类中的 Walk 和 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;
}

在枚举本身中存储一个方向键,例如:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM