【解決方法】エラーメッセージを解決する方法、クラスは抽象的ではなく、メソッドをオーバーライドしません…


これは blueJ メソッドの 3 つのクラスです。ChatBot クラスは具体的なサブクラスです。 Robot と Chatty の両方を継承する必要があります。

名前と数値の 2 つのパラメーターを取る単一のコンストラクターがあります。 インスタンス化します
空のリストを持つ friends フィールドと chatRecords フィールドの両方。

チャットボットの名前は、指定された名前に設定する必要があります。
チャットボットのレベルは、範囲内であれば指定された数値に設定する必要があります
[LEVEL_MIN, LEVEL_MAX]. 指定された数値が LEVEL_MIN 未満の場合、レベルが設定されます
LEVEL_MINに; 数値が LEVEL_MAX より大きい場合、レベルは LEVEL_MAX に設定されます。

ChatBot クラスでは、この行でエラーが発生しました。 このエラーを修正するにはどうすればよいですか

私が試したこと:

Java
<pre>import java.util.ArrayList;
import java.util.List;

public class ChatBot extends Robot implements Chatty{

    // AI level of the chatbot
    private int level;

    // A list of friends of the chatbot
    private final List<ChatBot> friends;

    // A list of chats made by the chatbot
    private final List<String> chatRecords;

    /**
     * Create a chatbot
     *
     * @param name A given name
     * @param level A a given level
     */
    public ChatBot(String name, int level) {
        super(name);
        this.level = Math.min(Math.max(level, LEVEL_MIN), LEVEL_MAX);
        this.friends = new ArrayList<>();
        this.chatRecords = new ArrayList<>();
        if (level < LEVEL_MIN) {
            this.level = LEVEL_MIN;
        } else if (level > LEVEL_MAX) {
            this.level = LEVEL_MAX;
        } else {
            this.level = level;
        }
    }

    /**
     * Add a chatbot to the friends list
     *
     * @param bot A given chatbot
     */
    public void addFriend(ChatBot bot) {
        friends.add(bot);
    }



    //////////////////////////////////////////////////////////////////////////////////
    ///////// PLEASE DO NOT CHANGE CODE BELOW THIS LINE ////////////////////////

    /**
     * Get the chatbot's level
     *
     * @return The level of the Chatbot
     */
    public int getLevel() {

        return level;
    }

    /**
     * Get the chatbot's friends
     *
     * @return The list of friends of the Chatbot
     */
    public List<ChatBot> getFriends() {

        return friends;
    }

    /**
     * Get the chat records
     *
     * @return The list of chats made by the chatbot
     */
    public List<String> getChatRecords() {

        return chatRecords;
    }

    /**
     * Add a chat by the chatbot in chatRecord
     * <p>
     * Each chat record is prefixed
     * by "Q:" for a question
     * or "A:" for an answer
     * e.g. if the chatbot asks a question "How are you?",
     * the string "Q:How are you?" will be added in the chatRecords
     *
     * @param type A given chat type, either 'Q' or 'A'
     * @param chat A given chat
     */
    public void addChatRecord(char type, String chat) {

        if (type == 'Q' || type == 'A') {
            chatRecords.add(type + ":" + chat);
        }
        else {
            System.out.println("Wrong type.");
        }
    }

    /**
     * @param obj A given object
     * @return true if obj is a Chatbot with the same name and level,
     * false otherwise
     */
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ChatBot bot)) {
            return false;
        }
        return super.equals(bot) && level == bot.getLevel();
    }
}/**
 * The Robot class represents a robot.
 * It holds relevant details of a robot.
 *
 * @author Yang He
 * @version 2023
 */
public abstract class Robot {

    // the robot's name
    private final String name;

    /**
     * Create a new robot with a given name
     *
     * @param name A given name
     */
    public Robot(String name) {

        this.name = name;
    }

    /**
     * Get the robot's name
     *
     * @return The robot's name
     */
    public String getName() {

        return name;
    }

    /**
     * Check if the robot has AI
     *
     * @return true if the robot has AI, false otherwise
     */
    public abstract boolean hasAI();

    /**
     * @param obj A given object
     * @return true if obj is a Robot with the same name as the robot,
     * false otherwise
     */
    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }

        if (!(obj instanceof Robot)) {
            return false;
        }

        var bot = (Robot) obj;
        return name.equals(bot.getName());
    }
}
import java.util.HashMap;
import java.util.Map;

/**
 * The Chatty interface
 *
 * @author Yang He
 * @version 2023
 */
public interface Chatty {

    // Chat levels
    int LEVEL_MIN = 1;
    int LEVEL_MAX = 3;

    // Question & answer repertoires
    Map<String,String> QA = new HashMap<>(Map.of(

                "Hello, how are you?", "I'm great, thanks.",
                "What are you?", "I am a chatbot",
                "Do you have a hobby?", "I like chatting with you.",
                "Can you tell a joke?", "You are very funny!",
                "What is the capital of UK?", "London",
                "Do you like Java?", "Yes of course.",
                "Are you a robot?", "Yes I'm a robot, but I'm a smart one!",
                "Do you get smarter?", "I hope so."
            ));

    /**
     * Ask a question
     *
     * @return the question
     */
    String question();

    /**
     * Answer a given question by a given robot
     *
     * @param question A given question
     * @return An answer
     */
    String answer(String question);

}

解決策 1

エラーメッセージは、それが言っていることをほぼ正確に意味します。
ChatBot クラスは、抽象クラス Robot を拡張します。 ロボットには hasAI() という抽象メソッドがあります。 Robot を拡張するには、ChatBox クラスで hasAI() を定義して、Robot の抽象メソッドをオーバーライドする必要があります。

コメント

タイトルとURLをコピーしました