+1 (315) 557-6473 

Program To Build Classes by Inheriting from Other Classes, Using Java Programming Language Assignment Solutions.


Instructions

Objective
Write a java assignment program to build classes by inheriting from other classes, in programming language.

Requirements and Specifications

Overview:
  1. Define a base class named Robot, used to define the capabilities of various types of robots.
  2.  Derive classes Drone, Vacuum, and MovieRobot each of which describe a specific type of robot, and which redefine certain Robot behaviors appropriately.
  3.  Derive more specific MovieRobot classes called HAL9000, ModelB9, and WALL_E which each have different capabilities.
  4. Download and use a tester module to ensure that your programs are correct. The programs can also be tested using Dr Java's interactive window.
  5. Prepare the assignment for submission and submit it.
This project is an individual effort; the Honor Code applies.
Robot Class
The term "robot" (at least in popular usage) can refer to a very broad category of things, and yet there is a common image that we often have in mind when discussing robots in general: machines that interact with the world. Usually, we categorize different types of robots based on their capabilities. Let's create a general class that identifies some common capabilities that different robots might have, with the possibility that new capabilities might be added in the future. The Robot class we are about to write will be a general class which we can use to describe any type of robot - we won't worry about the specifics yet, and instead we'll use subclasses to describe those.
Our Robot class will include the following:
  • A constructor, with the parameters int serialNumber, boolean flies, boolean autonomous, boolean teleoperated. The serialNumber is a number which uniquely identifies the robot, while the remaining parameters all represent different capabilities a robot can have: if flies is true, the robot can move through the air, autonomous is true when the robot can act on its own without a human operator, and teleoperated indicates whether a human can operate the robot manually.
  •  A void method called setCapabilities for setting the capabilities after object creation with the parameter list boolean flies, boolean autonomous, boolean teleoperated.
  • A getter called getSerialNumber for the serialNumber. We do not need a setter because we do not expect to change the serial number after it is set (it may even make sense to declare the method and the corresponding field final).
  •  Methods canFly, isAutonomous, and isTeleoperated, each of which take no parameters and return boolean. These method indicate which capabilities the robot supports. They may be redefined in subclasses, but for now, each of these methods should return whatever the capabilities were set to.
  • A getCapabilities method that takes no parameters and which returns a String containing each of the capabilities of the robot separated by spaces. For example, if canFly returns true, isAutonomous returns false, and isTeleoperated returns true, then getCapabilities should return the string "canFly isTeleoperated" (order and case are important). If all three returned true the string should be "canFly isAutonomous isTeleoperated". If none of them return true, the empty string should be returned ("").NOTE: in order for this method to work correctly for subclasses, you should use the previously defined canFly, isAutonomous, and isTeleoperated methods rather than access any data members directly.
  •  A toString method which returns a String in the format "ID: , Capabilities: " (replacing with actual serial number and list of capabilities).
Drone Class
Our simplest type of robot will represent a drone. Even though many different types of drones exist, they all have the same basic functionality, at least for the purposes of this project. All drones can fly, are teleoperated and not autonomous. To implement this subclass, we need to define the following:
  • Extend the Robot class.
  • An overloaded constructor with just a single parameter: int serialNumber.
  • An overridden canFly method which always returns true.
  • An overridden isAutonomous method which always returns false.
  • An overridden isTeleoperated method which always returns true.
NOTE: this class does not define or override the getCapabilities method, but it is still inherited by this subclass, and it should still give the correct output when on instances of this subclass. If you've followed the note above, this will be the case.
Vacuum Class
Our next type of robot is the humble vacuum, a consumer product which autonomously vacuums your house. All vacuums have the same basic capabilities: they can't fly or be teleoperated, but they are autonomous, and they also have the ability to clean. To implement this subclass, we need to define the following:
  • Extend the Robot class.
  • An overloaded constructor with just a single parameter: int serialNumber.
  • A new method, canClean which takes no arguments and returns true.
  • An overridden getCapabilities method which returns a string in the same format as the parent class Robot, but with the additional canClean capability at the end.
  • Overridden versions of each of three boolean methods from the parent class, as was done for the Drone class.
MovieRobot Class
In addition to robots that exist in the real world, there is a rich (if not technically accurate) history of robots in film. The capabilities of these robots vary widely from film to film, so unlike the previous two subclasses we won't "hard code" the capabilities, but we will introduce a new capability that most movie robots have: speech. Invariably, whenever a MovieRobot is capable of speech it will have a catchphrase, so we'll store that in our class as well. Lastly, since there are no examples of generic MovieRobots - only robots from specific movies - we'll make MovieRobot an abstract class.
  • Extend the Robot class.
  • An overloaded constructor with the parameters int serialNumber, boolean flies, boolean autonomous, boolean teleoperated, String catchphrase. Note: you can use the parent class' constructor to set the other capabilities first, and then set the robots catchphrase.
  •  An overloaded four argument constructor which by default sets the catchphrase to null. Note: if you followed the previous note, you can chain constructors to accomplish this in one line.
  • Add a new abstract method, canSpeak which takes no arguments and returns a boolean. The subclasses of MovieRobot will need to make sure this method returns true when they have a non-null catchphrase.
  • An overridden getCapabilities method which returns a string in the same format as the parent class Robot, but with the additional canSpeak capability at the end (if true).
  •  An overridden toString method which returns a string in the same format as the parent class Robot, and if the robot can speak appends a space followed by the catchphrase surrounded by quotation marks. (If the robot cannot speak, nothing should be appended)
MovieRobot Subclasses
Once we've made a subclass, we can keep building off of it by making new classes which derive from the subclass. We'll create three subclasses of MovieRobots from iconic films (all three of these classes should be concrete):
  • A HAL9000 subclass which extends MovieRobot, which flies and has the catchphrase "I can't let you do that Dave."
  • A ModelB9 subclass which extends MovieRobot, which has the catchphrase "Danger, Will Robinson!" and the additional capability flail.
  • A WALL_E subclass which extends MovieRobot, which cannot speak, but can also clean.
For each of these subclasses, make sure you override and add methods where appropriate, as you did in the previous subclasses. The easiest way to make sure the catchphrase for each subclass is correct is by overriding (not overloading) the MovieRobot constructor.
Source Code
ROBOT
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Robot {
    private int serialNumber;
    private boolean flies;
    private boolean autonomous;
    private boolean teleoperated;
    public Robot(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated) {
        this.serialNumber = serialNumber;
        setCapabilities(flies, autonomous, teleoperated);
    }
    public void setCapabilities(boolean flies, boolean autonomous, boolean teleoperated) {
        this.flies = flies;
        this.autonomous = autonomous;
        this.teleoperated = teleoperated;
    }
    public int getSerialNumber() {
        return serialNumber;
    }
    public boolean canFly() {
        return flies;
    }
    public boolean isAutonomous() {
        return autonomous;
    }
    public boolean isTeleoperated() {
        return teleoperated;
    }
    public String getCapabilities() {
        List capabilities = new ArrayList<>();
        if (canFly()) {
            capabilities.add("canFly");
        }
        if (isAutonomous()) {
            capabilities.add("isAutonomous");
        }
        if (isTeleoperated()) {
            capabilities.add("isTeleoperated");
        }
        if (capabilities.isEmpty()) {
            return "";
        }
        return String.join(" ", capabilities);
    }
    @Override
    public String toString() {
        return "ID: " + serialNumber + ", Capabilities: " + getCapabilities();
    }
}
DRONE
public class Drone extends Robot {
    public Drone(int serialNumber) {
        super(serialNumber, true, false, true);
    }
}
VACUUM
import java.util.ArrayList;
import java.util.List;
public class Vacuum extends Robot {
    public Vacuum(int serialNumber) {
        super(serialNumber, false, true, false);
    }
    public boolean canClean() {
        return true;
    }
    public String getCapabilities() {
        String s = super.getCapabilities();
        if (!s.isEmpty()) {
            s += " ";
        }
        return s + "canClean";
    }
}
MOVIE ROBOT
public abstract class MovieRobot extends Robot {
    private String catchPhrase;
    public MovieRobot(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated) {
        this(serialNumber, flies, autonomous, teleoperated, null);
    }
    public MovieRobot(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated, String catchPhrase) {
        super(serialNumber, flies, autonomous, teleoperated);
        this.catchPhrase = catchPhrase;
    }
    public abstract boolean canSpeak();
    public boolean canFlail() {
        return false;
    }
    @Override
    public String getCapabilities() {
        String s = super.getCapabilities();
        if (canSpeak()) {
            if (!s.isEmpty()) {
                s += " ";
            }
            s += "canSpeak";
        }
        return s;
    }
    @Override
    public String toString() {
        String s = super.toString();
        if (canFlail()) {
            s += " canFlail";
        }
        s += (canSpeak() ? " \"" + catchPhrase + "\"" : "");
        return s;
    }
}