Question
JAVA This PoD, builds off the classes that you created this week. You may copy your code from the previous PODs For todays problem, you
JAVA
This PoD, builds off the classes that you created this week. You may copy your code from the previous PODs For todays problem, you will add two methods to the class called RaceTrack: topSpeedCar, averageSpeed.
Here is the updated RaceTrack Class:
Attributes:
- name (String) //this holds track name single word
- Car [] cars //an array of Cars
- maxNum (int) //this is the maximum number of cars a race track can have
- currNum (int) //this is the current number of cars on the race - track
and the following methods:
- Constructor (sets the name and maximum number of cars from defined values) ANDinitializes the car array to the max number of cars AND sets the currNum to 0
- Get and set methods.
- toString method returns the name of the race track and number of cars on the race track
- addCar (Car) method adds the passed in car to the array if there is room (if there is no room, then it is not added. Dont forget to update the current number of cars if you successfully add a car. It returns true or false based on whether a car was added or not.
- printAllCars which prints the name of the race track, followed by each car on the race track on a new line (you can use the cars own toString method to print the information)
- topSpeedCar goes through the array of Cars and returns the Car with the fastest topSpeed (if more than one car has the same topSpeed, it will return the last car in the array with that top speed)
- averageSpeed() this method goes through all the cars in the array, tracking their current speed and returns the average current speed of all the cars on the racetrack.
Then create a RaceTrackDemo class to create and use Car objects. The demo class contains the main method. Use a Scanner object to create:
- one RaceTrack object by reading in the name of the race track and maximum number of cars
- Then create three Car objects by reading in the make and topSpeed of the three cars
- Add the three cars to the race track
- Increase the speed of the first car by 120 km
- Increase the speed of the second car by 130 km
- Increase the speed of the third car by 110 km
- Print out the topSpeedCar
- Print out the averageSpeed of the race
Suppose you had an array of Cars called cars with 5 car objects. If you wanted to the know the make of the car object at position 2 in the array, there are two ways you could do this:
- Create a temporary car object and then access that cars data by using the temporary car object. E.g.,
Car temp = cars [2]; //returns the car object stored at index 2
and stores it in temp
String make = temp.getMake(); //temp calls it's getMake method
and returns a String
- Directly access and use the Car object held in the array without needing a temporary car object. Eg., //the Car object stored at index 2 calls its getMake method and returns a String
String make = cars[2].getMake();
Details
Input
The race track name, then the max number of cars, then the make, model and model for 3 different cars: {name of race track} {max cars allowed on the track} {make of car1} {top speed of car1} {make of car2} {top speed of car2} {make of car3}
Example Input: KawarthaRaceTrack 8 BMW 200 Audi 240 Porsche 250
Output
Example Output: Car with top speed: Porsche Top Speed: 250 Average speed: 120.0
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