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 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 | |...
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...
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...
JAVA CODE FILL IN THE BLANKS you are to complete the skeleton program by completing *...
JAVA CODE FILL IN THE BLANKS you are to complete the skeleton program by completing * the To Do sections. I would suggest doing this a * little bit at a time and testing each step before you * go on to the next step. * *           Add whatever code is necessary to complete this assignment *********************************************************************/ //Add whatever code is necessary to complete this assignment public class Methods {    public static void main(String[] args)    {...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
Please can you draw a flow chart for the following code : Program code for Payroll,java:...
Please can you draw a flow chart for the following code : Program code for Payroll,java: public class Payroll { public Payroll(String name,int ID,double payRate) { this.name=name; this.ID=ID; this.payRate=payRate; } private String name; private double payRate,hrWorked; private int ID; public Payroll() { name="John Doe"; ID=9999; payRate=15.0; hrWorked=40; } public String getName() { return name; } public int getID() { return ID; } public void setPayRate(int payRate) { this.payRate=payRate; } public void setHrWorked(double hrWorked) { this.hrWorked=hrWorked; } public double getPayRate() {...
(JAVA PLS)You will create a program that will follow the queuing theory of the Barbershop Problem....
(JAVA PLS)You will create a program that will follow the queuing theory of the Barbershop Problem. In this you will use ques and random number generator to complete this project. You will create three queues Couch - the area where patron will wait just prior to getting their hair cut; Line - the area where patron will form a line just before reaching the couch; and Cashier - the area where patrons will line up to pay just before exiting...
In this program, you are modifying given code so that the class is object-oriented. 2. Write...
In this program, you are modifying given code so that the class is object-oriented. 2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.    3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.    b. The second text file contains the distances between the cities in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT