Question
Building a virtual Car gear shifter with associated velocity plus directions You are building a car that will shift gear (automatically) based on the velocity
Building a virtual Car gear shifter with associated velocity plus directions
You are building a car that will shift gear (automatically) based on the velocity ranges of the following table: (the numbers are integers, low/high are in mph but dont worry about the unit for this question)
You can start with a base class Vehicle, and then create a Car class that inherits from this base class Vehicle. You can then create another class which is a specific type of car that inherits from the Car class. You can choose any hot car that you like. Mine is a MiniCooperS convertible. Please build these classes in their separate files. There should be 4 files..
For your car class, you want to be able to steer directions (turn in 0 to 80 degrees) and change velocity (accelerate(+), decelerate(-)). The gears change based on the velocity range of the table above. After every velocity change, your program needs to report the gear shift values, and the vehicle movement parameters (speed, direction) cumulatively. After every direction change, your program needs to report the cumulative changes of the direction as well.
Your output display should look similar to the following. You can download a main sample (Main.java) from eLearn to start.
Increase speed by 35 from 0 => new velocity is 35
Car.ShiftGear(): shift from 1 -> gear: 4
Vehicle.move(): Moving at 35 in direction 0
Vehicle.steer(): turn from 0 to 40 degrees
Vehicle.steer(): turn from 40 to 55 degrees
Vehicle.steer(): turn from 55 to 50 degrees
Increase speed by 25 from 35 => new velocity is 60
Car.ShiftGear(): shift from 4 -> gear: 5
Vehicle.move(): Moving at 60 in direction 50
Vehicle.steer(): turn from 50 to 75 degrees
Reduce speed by-20 from 60 => new velocity is 40
Car.ShiftGear(): shift from 5 -> gear: 4
Vehicle.move(): Moving at 40 in direction 75
Increase speed by 5 from 40 => new velocity is 45
Car.ShiftGear(): shift from 4 -> gear: 5
Vehicle.move(): Moving at 45 in direction 75
Vehicle.steer(): turn from 75 to 87 degrees
Reduce speed by-8 from 45 => new velocity is 37
Car.ShiftGear(): shift from 5 -> gear: 4
Vehicle.move(): Moving at 37 in direction 87
Car Specfications:
name = MiniCooperS ,
size = 3L ,
# of wheels =4,
doors=2,
isManual = true,
Engine Size= 3L
roadServiceMonths = 12
For your design (before writing any code), you should decide on what are the appropriate fields (aka states, data, attributes) and methods (behaviors) for each of the classes. You can always change it later to optimize your design. Dont try to be perfect in the first time. (80/20 rules) your design should be flexible that you can modify easily.
From the above result printouts, it appears that you will need: Name, # of Wheels, doors, isManual (or automatic), Engine size, roadServiceMonths, Current Gear value, method to change gear, Current velocity, method to change velocity (accelerate, decelerate), method to steer() to change and update directions, method to move() to change and update velocity. Remember, the direction and velocity setting are cumulative (it updates from the previous value to become a current value). You will have to decide on which class is more appropriate to have the above states and methods. All your states should be private. You will need to have getters to get the private data (eg getName() method returns the car name via an object.getName() )
The following are the suggested arrangement for all the class. Please try your DESIGN based on the above information and compare to the suggestions below. You may have a better design.
Dont forget to use super(.) to initialize the super class and toString() to help in displaying results.
Your Vehicle class should have name, engine size, velocity, direction(0-360). It knows how to steer (direction) and move (velocity, direction). Your Car class (extends from Vehicle) should have names, engine size, number of wheels, number of doors, number of gears, and manual or automatic transmission. Notice Vehicle has the name and size fields as well. You will need to use super() along the class hierarchy tree to initialize various variables.
You need to have your specific car class (eg MiniCooperS) extends from Car (class)) to do the gear change operation. In this class (eg MiniCooperS), you should have a readServiceMonths (int) field. You need to use the super(..) in your constructor to initialize some of the parameters of the parent class. I put the gear changing codes in the Car class.
Generate an UML diagram (via ObjectAid ) for your design and codes.
NOTE: Be sure the comment your codes (Focus on what and why, NOT how). Codes without comments will automatically get 75% deduction.
Main.Java:
package Assignment2;
public class Main {
public static void main(String[] args) {
final int roadServiceMonth = 12; MiniCooperS mini = new MiniCooperS(roadServiceMonth); // steer() is defined in Vehicle // start car and accelerate from 0 to 30 mpp // show gear setting, and move values(speed,direction) (total speed, direction) mini.accelerate(35); // change direction (steer) 3 times (45, 10, -5) mini.steer(40); mini.steer(15); mini.steer(-5);
// increase speed by 20 ; show gear setting, move values mini.accelerate(25); // change direction (20) ; mini.steer(25); // change speed by (-20, 5); mini.accelerate(-20); mini.accelerate(5); // change direction (10) : Reduce speed (-10) mini.steer(12); mini.accelerate(-8); System.out.println(mini); // toString() in mini will display the stuff } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started