Car class

    sing Inheritance

    (based on Module 5 material)

    1) Modify the Module 5 Car class to provide a start() method that overrides the Vehicle class start() method and a speedUp() method that overloads the Vehicle class speedup() method.

    The initial code for the Car and Vehicle classes, and the TestCar2 driver class from Module 5 is shown below.

    /** Vehicle class This class is used to create Vehicle objects. */ public class Vehicle { // Vehicle class instance fields private boolean moving; // whether or not the vehicle is currently moving private double speed; // speed in mph of the vehicle private char bearing; // direction vehicle headed (‘N’,’E’,’S’, or ‘W’) public Vehicle() // Vehicle class no-arg constructor { moving = false; // assume not moving speed = 0.0; // not moving bearing = ‘N’; // assume ‘N’orth System.out.println(“Created a vehicle (no-arg)”); }public Vehicle (double initialSpeed) // Vehicle 1-arg constructor { bearing = ‘W’; speed = initialSpeed; if (speed > 0.0) { moving = true; }System.out.println(“Created a vehicle (1-arg)”);

    } public Vehicle (double initialSpeed, char initialBearing) // Vehicle 2-arg constructor { bearing = initialBearing; speed = initialSpeed; if (speed > 0.0) { moving = true; }System.out.println(“Created a vehicle (2-arg)”); } public void start(double initialSpeed, char initialBearing) { moving = true; if (initialSpeed >= 5.0 && initialSpeed <= 20.0) { speed = initialSpeed; // valid expected range }else if (initialSpeed >= 0.0 && initialSpeed < 5.0) { speed = 5.0; // minimum }else if (initialSpeed < 0.0) { speed = 0.0; // assume no movement moving = false; }else if (initialSpeed > 20.0) { speed = 20.0; // maximum allowed }switch(initialBearing) { case ‘N’: bearing = initialBearing; break; case ‘E’: bearing = initialBearing; break; case ‘S’: bearing = initialBearing; break; case ‘W’:bearing = initialBearing; default: System.out.println(“invalid bearing ” + initialBearing + ” set to N”); // additional user notification bearing = ‘N’; } }public double getSpeed() // get and return current speed in mph { return speed; }public void setSpeed(double newSpeed) // set new speed in mph { speed = newSpeed; }public char getBearing() // get and return current bearing { return bearing; }public void speedUp(double mphSteps, int numSteps) { int counter = 0; while (counter < numSteps) { speed += mphSteps; System.out.println(“counter= ” + counter + “, ” + this.toString()); counter++; } }public String toString() { return “From toString(): speed= ” + getSpeed() + ” mph and bearing= ” + getBearing(); } }

    /** Car class This class is used to create Car objects and inherits from the Vehicle class. */ public class Car extends Vehicle { // Car class instance fields private String color; // color of the Car private int doors; // number of doors of the Car private double hp; // engine horsepower public Car(String carColor, int numDoors, double horsePower, double startingSpeed) // 4-arg constructor { super(startingSpeed); color = carColor; doors = numDoors; hp = horsePower; System.out.println(“Created a car”); }public String getColor() { return color; }public int getDoors() { return doors; }public double getHp() { return hp; }public String toString() { return “From Car toString(): color= ” + getColor() + ” doors= ” + getDoors() + ” hp= ” + getHp() + ” speed= ” + getSpeed() + ” mph and bearing= ” + getBearing();

    } }

    /**TestCar2 class This class is used to create a Car, start it up, and then speed it up. */ public class TestCar2 { public static void main(String[] args) { // create, start, and speed up a Car Car myCar2 = new Car(“blue”, 4, 300., 10.0); // blue car, 4 doors, 300. hp // w/ initial speed of 10 mph System.out.println(myCar2.toString()); myCar2.speedUp(5.0, 2); } }

    The Car class’s overriding start() method must start a car in a southerly direction at zero speed. The Car class’s overloaded speedup() method should just accept the additional miles per hour that the car should start moving from its current speed. Modify the TestCar2 driver class to form a new TestCar3 driver class that creates a myCar3 instance and uses single start() and single speedup() method calls to use the Car class’s start() and speedup() methods only to accelerate it by 50 mph.

    Be sure that you include the course, the program number, your name, and the date in your program header. Include additional comments as necessary and maintain consistent indentation for good programming style as shown in the text. Be sure that you capture all of your output for this program’s execution for full credit!

    2) You may use any Java Integrated Development Environment (IDE) you choose to develop your source code, compile and link your code, and execute your program (jGRASP is recommended). Or you may use the Windows Command Prompt.

    You are to submit the following deliverables in a single Microsoft Word file in this order, and clearly labeled.

    a) A screen snapshot of your Java source code (all file(s) shown separately) displayed in the IDE or Windows editor showing a successful compilation if possible (only the beginning of the source file(s) are necessary

    b) A listing of your entire source code file(s).

    c) A screen snapshot of all of your program’s outputs for the specified values in Step #1. Failure to show any of them will result in lost points.

    3) Your instructor may compile and run your program to verify its correctness.

    4) You will be evaluated on (in order of importance):

    a) Following directions and inclusion and packaging of all deliverables in Step #2.

    b) Correct execution of your program (this includes proper compilation). This also includes getting the correct answers!

    c) Proper commenting of your Java code and indentation (as specified in the text).

    d) Neatness in packaging of your deliverables (to include putting the items in Step #2 in the correct order).

                                                                                                                                      Order Now