Question

In: Computer Science

This question is about the java Object-Oriend program and give the code as follow requirment: You...

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

Solutions

Expert Solution

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


Related Solutions

This is the question about the java problem, please give the detail comment and code of...
This is the question about the java problem, please give the detail comment and code of each class. Please write tests for all the code of all the classes Thank you Create a class Mammal with the following UML diagrams: +---------------------------------+ | Mammal | +---------------------------------+ | - name: String | +---------------------------------+ | + Mammal(String name) | | + getName(): String | | + isCookable(): boolean | | + testMammal(): void | (this method is static ) +---------------------------------+ The isCookable method...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Could you give some sample Java code of creating a program where users are prompt to...
Could you give some sample Java code of creating a program where users are prompt to determine how many exams they have taken then it is prompt that they have to enter their test scores and gives the sum of all their test scores? You should have to use looping
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
This problem is about java program and please show the detail comment and code in each...
This problem is about java program and please show the detail comment and code in each class. Thank you! Create four classes with the following UML diagrams: (The "-" means private and the testBankAccount() testMobilePhone() testChocolate() testStudent() all static +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ | MobilePhone     | +------------------------------------------------+ | -...
This the question about the java and please show the detail comment and code of each...
This the question about the java and please show the detail comment and code of each class.Thank you. And the "+" means "public" the "-" means "private" and testBankAccount(): void testMobilePhone(): void testChocolate(): void all are static Create four classes with the following UML diagrams: +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ |...
Java. You are creating a 'virtual pet' program. The pet object will have a number of...
Java. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of the pet class which will be a specific type of pet,...
You have written a JAVA program that creates Shoe object with three instance variables. You will...
You have written a JAVA program that creates Shoe object with three instance variables. You will need to use the exception you created to go along with it. So far you have been using my driver classes. Now it is time for you to create your own driver class that will implement an ArrayList of Shoe objects. This assignment uses the Shoe.java and ShoeException.java to create the driver ShoeStore.Java You will display a menu as follows: Add shoes Print all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT