In: Computer Science
This question is about the java Object-Oriend program and give the code as follow requirment:
You are a software engineer who has to write the software for a
car transmission. The transmission has five gears numbered from 1
to 5 to move forward, one neutral gear position numbered 0 where
the car does not move, and one reverse gear numbered -1 to move
backward. The transmission has a clutch, and the driver of the car
can only change gear if the driver is currently pushing the pedal
for the clutch.
Write a Transmission class with the following UML
specification:
+------------------------------------------+
| Transmission |
+------------------------------------------+
| - clutchPedalPushed: boolean |
| - gear: int |
+------------------------------------------+
| + Transmission() |
| + isClutchPedalPushed(): boolean |
| + pushClutchPedal(): void |
| + releaseClutchPedal(): void |
| + getGear(): int |
| + upShift(): void |
| + downShift(): void |
| + skipGears(int gear): void |
| + testTransmission(): void |
+------------------------------------------+
(The "-" means it is private "+" means it is public)
(Just follow the upside method don't add the others)
where:
clutchPedalPushed is a private instance variable describing
whether the pedal for the clutch of the transmission is currently
pushed by the driver or not.
gear is a private instance variable describing the gear number in
which the transmission currently is.
Transmission is a public constructor that creates a Transmission
object. When a transmission is created, the pedal for its clutch is
not currently being pushed and the transmission is in the neutral
gear 0.
isClutchPedalPushed is a public method that returns as result a
boolean indicating whether the driver is currently pushing the
pedal for the clutch of the transmission or not.
pushClutchPedal is a public method that pushes the pedal for the
clutch of the transmission; if the pedal is already currently being
pushed then this method does nothing.
releaseClutchPedal is a public method that releases the pedal for
the clutch of the transmission; if the pedal is currently not being
pushed then this method does nothing.
getGear is a public method that returns the gear number in which
the transmission currently is.
upShift is a public method that shifts the transmission up by one
gear. The transmission can only shift up if the pedal for the
clutch of the transmission is currently being pushed by the driver.
If the pedal for the clutch of the transmission is not currently
being pushed then the upShift method must print a message to the
screen "Cannot shift up when the clutch is engaged" and the
transmission does not change gear. If the transmission is already
in gear 5 then the upShift method must print a message "Cannot
shift higher than 5th gear" and the transmission does not change
gear.
downShift is a public method that shifts the transmission down by
one gear. The transmission can only shift down if the pedal for the
clutch of the transmission is currently being pushed by the driver.
If the pedal for the clutch of the transmission is not currently
being pushed then the downShift method must print a message to the
screen "Cannot shift down when the clutch is engaged" and the
transmission does not change gear. If the transmission is already
in gear -1 then the downShift method must print a message "Cannot
shift lower than reverse gear" and the transmission does not change
gear.
skipGears is a public method that shifts the transmission
directly to the given gear.
o The transmission can only change gear if the pedal for the clutch
is currently being pushed. If the pedal for the clutch of the
transmission is not currently being pushed then the skipGears
method must print a message to the screen "Cannot shift to another
gear when the clutch is engaged" and the transmission does not
change gear.
o If the given gear number is strictly less than -1 or strictly
bigger than 5 then the skipGears method must print a message
"Cannot shift to gear XXX" (where XXX is replaced in the real
output by the given gear number) and the transmission does not
change gear.
o If the given gear number is the same as the current gear of the
transmission then the skipGears method must print a message "Cannot
shift to the same gear" and the transmission does not change
gear.
o If the car is currently moving forward and the driver tries to
shift to the reverse gear, or if the car is currently moving
backward and the driver tries to shift to one of the forward gears,
then the skipGears method must print a message "Cannot reverse
direction while moving" and the transmission does not change gear.
(This is to prevent the car from suddenly switching from moving
forward to moving backward, or from suddenly switching from moving
backward to moving forward, without going in between through the
neutral gear position and stopping.)
testTransmission is a public static method that tests all the
code in your Transmission class. Test all your methods from the
simplest one first to the most complicated one last.
Once you have written the Transmission class, you can test it by
adding the following code in a separate class:
public class Start {
public static void main(String[] args) {
Transmission.testTransmission();
}
}
This code calls the static testTransmission method of the
Transmission class, which should then run all your tests.
Here are a few extra instructions:
Give meaningful names to your variables so we can easily know
what each variable is used for in your program.
Put comments in your code (in English!) to explain WHAT your code
is doing and also to explain HOW your program is doing it.
Make sure all your code is properly indented (formatted). Your
code should be beautiful to read.
Failure to follow these instructions will result in you losing
poi
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Transmission.java
public class Transmission {
// attributes
private boolean clutchPedalPushed;
private int gear;
// constructor
public Transmission() {
clutchPedalPushed = false;
gear = 0;
}
// returns the value of clutchPedalPushed
public boolean isClutchPedalPushed() {
return clutchPedalPushed;
}
// sets clutchPedalPushed to true
public void pushClutchPedal() {
clutchPedalPushed = true;
}
// sets clutchPedalPushed to false
public void releaseClutchPedal() {
clutchPedalPushed = false;
}
// returns the gear
public int getGear() {
return gear;
}
// advances the gear if clutchPedalPushed is true and gear<5
public void upShift() {
if (!clutchPedalPushed) {
System.out.println("Cannot shift up when the clutch is engaged");
} else if (gear == 5) {
System.out.println("Cannot shift higher than 5th gear");
} else {
gear++;
}
}
// shift downs the gear if clutchPedalPushed is true and gear>-1
public void downShift() {
if (!clutchPedalPushed) {
System.out.println("Cannot shift down when the clutch is engaged");
} else if (gear == -1) {
System.out.println("Cannot shift lower than reverse gear");
} else {
gear--;
}
}
// shift to gear if clutchPedalPushed is true, gear is valid and not moving
// in opposite direction
public void skipGears(int gear) {
if (!clutchPedalPushed) {
System.out
.println("Cannot shift to another gear when the clutch is engaged");
} else if (gear < -1 || gear > 5) {
System.out.println("Cannot shift to gear " + gear);
} else if (gear == this.gear) {
System.out.println("Cannot shift to the same gear");
} else if ((gear > 0 && this.gear == -1)
|| (gear == -1 && this.gear > 0)) {
System.out.println("Cannot reverse direction while moving");
} else {
this.gear = gear;
}
}
//static method to test all methods
public static void testTransmission() {
//creating a Transmission, displaying initial status
Transmission t = new Transmission();
System.out.println("IsClutchPedalPushed: " + t.isClutchPedalPushed());
System.out.println("Gear: " + t.getGear());
//upshifting & downshifting without pushing gear
System.out.println("upShift():");
t.upShift();
System.out.println("downShift():");
t.downShift();
//pushing gear
System.out.println("pushClutchPedal()");
t.pushClutchPedal();
System.out.println("IsClutchPedalPushed: " + t.isClutchPedalPushed());
//testing downshifting
System.out.println("downShift()");
t.downShift();
System.out.println("Gear: " + t.getGear());
System.out.println("downShift()");
t.downShift();
//testing upshifting
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
System.out.println("upShift()");
t.upShift();
System.out.println("Gear: " + t.getGear());
//testing skipGears
System.out.println("skipGears(-1)");
t.skipGears(-1);
System.out.println("skipGears(2)");
t.skipGears(2);
System.out.println("Gear: " + t.getGear());
System.out.println("skipGears(22)");
t.skipGears(22);
}
}
/*OUTPUT of the Start program given by you*/
IsClutchPedalPushed: false
Gear: 0
upShift():
Cannot shift up when the clutch is engaged
downShift():
Cannot shift down when the clutch is engaged
pushClutchPedal()
IsClutchPedalPushed: true
downShift()
Gear: -1
downShift()
Cannot shift lower than reverse gear
upShift()
Gear: 0
upShift()
Gear: 1
upShift()
Gear: 2
upShift()
Gear: 3
upShift()
Gear: 4
upShift()
Gear: 5
upShift()
Cannot shift higher than 5th gear
Gear: 5
skipGears(-1)
Cannot reverse direction while moving
skipGears(2)
Gear: 2
skipGears(22)
Cannot shift to gear 22