Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is a blueJ method will three class,The ChatBot class is a concrete subclass; it should inherit both Robot and Chatty.

It has a single constructor that takes two parameters: a name and a number. It instantiates
both friends and chatRecords fields with an empty list.

The name of the chatbot should be set to the given name.
The level of the chatbot should be set to the given number if it is within the range
[LEVEL_MIN, LEVEL_MAX]. If the given number is less than LEVEL_MIN, the level is set
to LEVEL_MIN; if the number is greater than LEVEL_MAX, the level is set to LEVEL_MAX.

In the ChatBot class i get an error on this line public class ChatBot extends Robot implements Chatty which says ChatBot is not abstract and does not override abstract method hasAI() in Robot. How do i fix this error

What I have tried:

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

}
Posted
Updated 22-Mar-23 8:48am

1 solution

The error message means pretty much exactly what it says.
Your ChatBot class extends the abstract class Robot. Robot has an abstract method called hasAI(). In order to extend Robot your ChatBox class MUST define hasAI() to override the abstract method from Robot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900