My project is composition, which a Truck HAS-A Engine which can be called using parameters into the console:
(note, engineStatus represent two arguments which would determine the parameter, IE, One or the other would be inputted; On/Off or True/False)
PrimaTruck truck = new PrimaTruck(150, OUColour.RED, (engineStatus));
I can't think how to get my second class to get the engineStatus() and insert variables properly into the constructor for use with the console command code above. My main error is that it cannot find the engineStatus variable.
I also acknowledge that my code is wrong for the Engine Boolean expressions and can't figure out a good alternative to give engineStatus() an On or Off variable dependant on if engineStart() and engineStop() are On/Off, thus giving a value that can be entered in the PrimaTrucks() constructor parameters.
PrimaTruck.Java
import ou.*;
/**
* Write a description of class PrimaTruck here.
*/
public class PrimaTruck{
// instance variables - replace the example below with your own
private int topSpeed;
private OUColour colour;
public Engine engineStatus;
/**
* Constructor for objects of class PrimaTruck
* Initialise instance variables
*/
public PrimaTruck(int topSpeed, OUColour colour, Engine engineStatus){
this.topSpeed = topSpeed;
this.colour = colour;
this.engineStatus = engineStatus;
}
public void setColour(OUColour aColour){
this.colour = aColour;
}
public OUColour getColour(){
return colour;
}
public int getTopSpeed(){
return topSpeed;
}
}
Engine.Java
/**
* Write a description of class Engine here.
*/
public class Engine{
// instance variables
public Boolean engineStart;
public Boolean engineStop;
public String engineStatus;
private Boolean status;
/**
* Constructor for objects of class Engine
*/
public Engine(String engineStatus){
// initialise instance variables
this.engineStart = engineStart;
this.engineStop = engineStop;
this.engineStatus = engineStatus;
this.status = status;
}
public Boolean status(){
if (engineStart == true){
status = engineStart;
}else if (engineStop = true){;
status = engineStop;
}
return this.status;
}
public Boolean getEngineStart(){
return engineStart;
}
public void setEngineStart(){
this.engineStop = false;
this.engineStart = true;
}
public Boolean getEngineStop(){
return engineStop;
}
public void setEngineStop(){
this.engineStart = false;
this.engineStop = true;
}
public void setEngineStatus(String aStatus){
this.engineStatus = aStatus;
}
public String getEngineStatus(){
return engineStatus;
}
}
Any feedback on coding or pointers on how to implement will be appreciated.
A lot of the code in the Engine class seems redundant. I'm making lot of changes to the code. See whether this is what you want.
public class Engine{
// instance variables
public boolean engineStart;
public boolean engineStop;
public boolean engineStatus;
/**
* Constructor for objects of class Engine
*@param1 - true if engine is started
*@param2 - true if engine is stopped
*@param3 - true if engine is on, else false
*/
public Engine(boolean engineStart, boolean engineStop, boolean engineStatus){
// initialise instance variables
this.engineStart = engineStart;
this.engineStop = engineStop;
this.engineStatus = engineStatus;
}
public boolean getEngineStart(){
return engineStart;
}
public void setEngineStart(){
this.engineStop = false;
this.engineStart = true;
setEngineStatus(true);
}
public boolean getEngineStop(){
return engineStop;
}
public void setEngineStop(){
this.engineStart = false;
this.engineStop = true;
setEngineStatus(false);
}
public void setEngineStatus(boolean engineStatus){
this.engineStatus = engineStatus;
}
public String getEngineStatus(){
return engineStatus;
}
}
This Engine class has only 3 boolean variables: engineStart, engineStop, engineStatus. If engineStart = true, then engineStatus = true. Else engineStatus = false.
I hope this is the functionality you are trying to achieve. Now you need to pass in 3 boolean variables while creating an Engine object.
Now in the truck class make the following changes:
public class PrimaTruck{
// instance variables - replace the example below with your own
private int topSpeed;
private OUColour colour;
public boolean engineStatus;
/**
* Constructor for objects of class PrimaTruck
* Initialise instance variables
*/
public PrimaTruck(int topSpeed, OUColour colour, Engine engine){
this.topSpeed = topSpeed;
this.colour = colour;
this.engineStatus = engine.getEngineStatus();
}
If this is not what you were looking for, feel free to ask in comments.