Reputation: 429
I am currently finishing a University practice exam in the run up to my exam in the summer. Below is the code which I have completed so far: I am struggling with one of the parts of the assignment. What it asks me to do is: "In the tester02, create a method called startRobot, which will be a polymorphic method. This method will accept an object of type Robot and a Scanner object. The purpose of the method is to start the robot, get the robot to undertake a task method(you must run the two versions of the doTask method here) and then to stop the robot."
so basically it asks me to create the polymorphic method and call it twice, first time passing the EntertainmentRobot
into it and then the second time HumanStudyRobot
. I am not sure how to set this up in the tester as I am just receiving errors when trying to write the code. I'm not really familiar with polymorphic methods / polymorphism either.
Any help at all would be much appreciated!
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;
//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;
}
//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.");
}
}
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. \n The robot has started entertaining.");
}
@Override
public void stop() {
System.out.println("The Entertainment RObot has finsihed entertaining");
}
@Override
public void doTask() {
}
@Override
public void doTask(Scanner input) {
play();
}
public void talk() {
}
public void play () {
System.out.println("How many times would you like to play?");
if (getEnergy() >= energyRequired() ) {
energyConsumption();
}
else
if (getEnergy() < energyRequired()) {
System.out.println("The robot does not have sufficient energy to play.");
regenerate();
System.out.println("The robot is regenerating");
System.out.println(".........................");
System.out.println("The robot has finished regenerating!");
}
}
public String toString() {
return "\nI AM AN ENTERTAINMENT ROBOT \nThe details of the entertainment robot are below: \n" +
"Name : " + getName() + "\nHeight: " + getHeight() + "\nWeight: " + getWeight() + "\nManufacturer: " +
getManufacturer() + "\nPurpose: " + getPurpose();
}
}
package Program;
import java.util.Scanner;
public class Tester02 {
public static void main(String[] args) {
HumanStudyRobot HumanStudyRobot1 = new HumanStudyRobot("HRP", 1.5, 58.0, "Kawada Industries");
HumanStudyRobot1.setPurpose("Study into human movement and perform a wide range of tasks.");
/*
System.out.println(HumanStudyRobot1.toString());
HumanStudyRobot1.start();
HumanStudyRobot1.study();
HumanStudyRobot1.stop();*/
public void startRobot(Robot, Scanner input){
}
EntertainmentRobot EntertainmentRobot1 = new EntertainmentRobot("QRIO", 0.6, 7.3, "SONY");
EntertainmentRobot1.setPurpose("To live with you, make life fun and make you happy.");
System.out.println(HumanStudyRobot1.toString());
System.out.println(EntertainmentRobot1.toString());
}
}
Upvotes: 0
Views: 545
Reputation: 47
im almost certain im on the same course as you, as im doing the exact same question, anyway after looking at what the people above have said, I got this working by doing this:
public static void main(String[] args) {
EntertainmentRobot jim = new EntertainmentRobot(0.6, "SONY", "QRIO", "To live with you, make life fun and make you happy", 7.3);
HumanStudyRobot jeff = new HumanStudyRobot(1.5, "Kawada Industries", "HRP", "Study into human movement and perfrom a wide range of tasks", 58.0);
//System.out.println(jim);
//System.out.println(jeff);
//EntertainmentRobot jim = robot();
Scanner input = new Scanner(System.in);
startRobot(jim, input);
startRobot(jeff, input);
}
public static void startRobot(Robot robot, Scanner input) {
/*
* POLYMORPHIC METHOD
* Accept an object of type robot
* Scanner object
* Start the robot
* Get robot to undertake a task
* stop the robot
*/
robot.start();
robot.doTask();
robot.stop();`enter code here`
}
}
Upvotes: 0
Reputation: 2969
There's nothing wrong with the code here. Since you don't understand polymorphism principles I think you need to work on that; 3 robot classes
extend Robot
class which is an abstract class, subclasses must override
super classes abstract methods. In your case the abstract methods are
public abstract void start();
public abstract void stop();
public abstract void doTask();
public abstract void doTask(Scanner input);
Since all robot classes are subclasses you can do something like this
Robot humanrobot = new HumanStudyRobot("parameters...");
Robot entertainment = new EntertainmentRobot("parameters");
public void startRobot(Robot robot, Scanner input){
robot.dotask();
}
startRobot(humanrobot, scanner);
startRobot(entertainment, scanner);
Upvotes: 0
Reputation: 2418
First observation: your startRobot
method signature is invalid, change it to
public void startRobot(Robot robot, Scanner input){
}
Second observation: move the method declaration outside of the main method.
Third observation: call startRobot
from the main method with your robot and scanner parameters.
startRobot(EntertainmentRobot1, /*your scanner*/);
startRobot(HumanStudyRobot1, /*your scanner*/);
Since both classes extend the Robot
class - they can be passed to the startRobot
method. For further reading on this topic, consult the Oracle Documentation.
Upvotes: 1