Part 1 Writing Exercise 10 pts Include answers to the follow
Part 1: Writing Exercise: (10 pts) Include answers to the following questions in a comment block after your header comment block A) Do a little discovery learning and research the basic idea of a Class. Compare and contrast a Class and a Struct (5pts) B) Read Part 2... If you were going to implement this assignment with Object Orientation ... how would that change the Car? (5pts) Part 2: Programming (40 pts) This project is more of a simulator than an input->output program. You may choose to add some input to the program though. Specifications (20 points) Text Based Car Race! Skills: Basic Description: Input/Output Arrays Loops Structs You are going to create a simple car race program using the ideas of Structs and functions to make several racing cars. These cars will “compete” to get to the end of the line first. The hard part will be creating and using the Car struct... but ultimately if you can make an array of integers and loop through that array increasing each integer ... you can make this work too. Specifications: Define a struct called \"Car\" with the following member variables: Total Odometer Miles Speed in miles per hour Driver Name Sponsor Car Number You should also include the following functions: A function to make progress down the course A function to convert your speed from MPH to MPS A function to “pretty print” your car’s information including Number, Sponsor, Driver and Progress A function to re-randomize speed Include any useful functions you might need to accomplish the goal The total odometer miles should be initialized to zero, and speed initialized to a random value between 60 and 120. Create a list of 20 vehicles with driver and sponsor names. You should not re-use a driver name or sponsor name (note – worry about this specification last, if you can make the race work w/o this, that’s better than getting hung up on this algorithm) Your main program should simulate the progress of the vehicles in the race. You should accomplish this with a loop. Every 5 iterations of the loop (should be considered 1 minute of time), the vehicles pick a new random speed. Every iteration through the loop their odometer miles are updated: odometer_miles = odometer_miles + speed*time Since speed is in miles per hour, time should be in hours as well (1 minute is 1/60th of an hour). Remember for the sake of this assignment 1 minute = 5 loop iterations. The first car to reach the race’s limit (100 miles is a good number) should be declared the winner by printing the driver name and sponsor name. You should set this as a variable – consider asking the user for the value as well. +5 Extra Credit – Use correct object orientation instead of structs and functions. +3 Extra Credit – Output your racing results to a text-file as well as the screen. Here are some String arrays that you can use to extract Driver and Sponsors: string driverNameList = {\"William Swopes\", \"Margurite Miland\", \"Alva Liska\", \"Bruna Breen\", \"Luciana Deshotel\", \"Jeannetta Nesbitt\", \"Donny Ledger\", \"Modesto Tennant\", \"Imelda Hassler\", \"Karma Agar\", \"Janis Wisneski\", \"Micha Dillow\", \"Mattie Boulden\", \"Ethelyn Boswell\", \"Erica Corvin\", \"Marion Holliday\", \"Laticia Repka\", \"Desirae Guarino\", \"Ines Wallach\", \"Deloris Kimbler\", \"Elliot Shatley\", \"Millicent Koller\", \"Bess Medellin\", \"Marcy Lydick\", \"Garnet Mccabe\", \"Donna Tannehill\", \"Dusti Devillier\", \"Leland Slemp\", \"Keiko Dolph\", \"Maybell Berggren\", \"Jeni Crew\", \"Christi Birdwell\", \"Una Sprague\", \"Sheba Mirza\", \"Alanna Wawrzyniak\", \"Francina Rippel\", \"Fermin Layne\", \"Taren Stetson\", \"Serina Reveles\", \"Maegan Arvizo\", \"Tajuana Behringer\", \"Tamara Havel\", \"Anya Gemmill\", \"Mel Bustle\", \"Tandy Nash\", \"Hue Stefan\", \"Reina Streett\", \"Glennie Kist\", \"Latricia Li\", \"Juliette Bureau\"}; string listOfSponsors = {\"Abercrombie & Fitch Co.\", \"ABM Industries Incorporated\", \"Ace Hardware Corporation\", \"ACT Manufacturing Inc.\", \"Acterna Corp.\", \"Adams Resources & Energy, Inc.\", \"ADC Telecommunications, Inc.\", \"Adelphia Communications Corporation\", \"Administaff, Inc.\", \"Adobe Systems Incorporated\", \"Adolph Coors Company\", \"Advance Auto Parts, Inc.\", \"Advanced Micro Devices, Inc.\", \"AdvancePCS, Inc.\", \"Advantica Restaurant Group, Inc.\", \"The AES Corporation\", \"Aetna Inc.\", \"Affiliated Computer Services, Inc.\", \"AFLAC Incorporated\", \"AGCO Corporation\", \"Agilent Technologies, Inc.\", \"Agway Inc.\", \"Apartment Investment and Management Company\", \"Air Products and Chemicals, Inc.\", \"Airborne, Inc.\", \"Airgas, Inc.\", \"AK Steel Holding Corporation\", \"Alaska Air Group, Inc.\", \"Alberto-Culver Company\", \"Albertson\'s, Inc.\", \"Alcoa Inc.\", \"Alleghany Corporation\", \"Allegheny Energy, Inc.\", \"Allegheny Technologies Incorporated\", \"Allergan, Inc.\", \"ALLETE, Inc.\", \"Alliant Energy Corporation\", \"Allied Waste Industries, Inc.\", \"Allmerica Financial Corporation\", \"The Allstate Corporation\", \"ALLTEL Corporation\", \"The Alpine Group, Inc.\", \"Amazon.com, Inc.\", \"AMC Entertainment Inc.\", \"American Power Conversion Corporation\", \"Amerada Hess Corporation\", \"AMERCO\", \"Ameren Corporation\", \"America West Holdings Corporation\", \"American Axle & Manufacturing Holdings, Inc.\", \"American Eagle Outfitters, Inc.\", \"American Electric Power Company, Inc.\"}; Breaking it down Create a struct called RaceCar or simply Car with the listed member variables Create an array of 20 Cars each with a Driver and Sponsor, initialized as seen above. o Create logic to prevent a duplicate name, but once again – don’t focus on this, get the race to work first Create a function to report the progress of the cars. Create a loop to make the cars progress towards the goal! Make sure you frequently report the progress of the cars. Declare a winner by finding the first car to cross the finish line! o You may consider writing a sort for your array to make this easier.
Solution
Car.java
import java.util.Random;
public class Car {
private double totalOdometerMiles;
private int speed;
private String driverName;
private String sponser;
private int carNumber;
public Car(String driverName, String sponser, int carNumber) {
this.speed = new Random().nextInt(61) + 60;
this.driverName = driverName;
this.sponser = sponser;
this.carNumber = carNumber;
}
public double getTotalOdometerMiles() {
return totalOdometerMiles;
}
public void setTotalOdometerMiles(double totalOdometerMiles) {
this.totalOdometerMiles = totalOdometerMiles;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getSponser() {
return sponser;
}
public void setSponser(String sponser) {
this.sponser = sponser;
}
public int getCarNumber() {
return carNumber;
}
public void setCarNumber(int carNumber) {
this.carNumber = carNumber;
}
public void makeProgress(){
this.totalOdometerMiles += (this.speed * (1 / 60.0));
}
public double convertToMPS(){
return this.speed / 3600.0;
}
public void reRandomizeSpeed(){
this.speed = new Random().nextInt(61) + 60;
}
public void print(){
System.out.println(\"==================================================\");
System.out.println(\"Car Number: \" + this.carNumber);
System.out.println(\"Driver Name: \" + this.driverName);
System.out.println(\"Sponser Name: \" + this.sponser);
System.out.println(\"Progress: \" + this.totalOdometerMiles);
System.out.println(\"==================================================\");
}
}
______________________________________________________________________________
RaceUtility.java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RaceUtility
public static String[] driverNameList = { \"William Swopes\",
\"Margurite Miland\", \"Alva Liska\", \"Bruna Breen\", \"Luciana Deshotel\", \"Jeannetta Nesbitt\", \"Donny Ledger\", \"Modesto Tennant\", \"Imelda Hassler\", \"Karma Agar\", \"Janis Wisneski\", \"Micha Dillow\", \"Mattie Boulden\", \"Ethelyn Boswell\", \"Erica Corvin\", \"Marion Holliday\", \"Laticia Repka\", \"Desirae Guarino\", \"Ines Wallach\", \"Deloris Kimbler\", \"Elliot Shatley\", \"Millicent Koller\", \"Bess Medellin\", \"Marcy Lydick\", \"Garnet Mccabe\", \"Donna Tannehill\", \"Dusti Devillier\", \"Leland Slemp\", \"Keiko Dolph\", \"Maybell Berggren\", \"Jeni Crew\", \"Christi Birdwell\", \"Una Sprague\", \"Sheba Mirza\", \"Alanna Wawrzyniak\", \"Francina Rippel\", \"Fermin Layne\", \"Taren Stetson\", \"Serina Reveles\", \"Maegan Arvizo\", \"Tajuana Behringer\", \"Tamara Havel\", \"Anya Gemmill\", \"Mel Bustle\", \"Tandy Nash\", \"Hue Stefan\", \"Reina Streett\", \"Glennie Kist\", \"Latricia Li\", \"Juliette Bureau\" };
public static String[] listOfSponsors = { \"Abercrombie & Fitch Co.\", \"ABM Industries Incorporated\", \"Ace Hardware Corporation\", \"ACT Manufacturing Inc.\", \"Acterna Corp.\", \"Adams Resources & Energy, Inc.\", \"ADC Telecommunications, Inc.\", \"Adelphia Communications Corporation\", \"Administaff, Inc.\", \"Adobe Systems Incorporated\", \"Adolph Coors Company\", \"Advance Auto Parts, Inc.\", \"Advanced Micro Devices, Inc.\", \"AdvancePCS, Inc.\", \"Advantica Restaurant Group, Inc.\", \"The AES Corporation\", \"Aetna Inc.\", \"Affiliated Computer Services, Inc.\", \"AFLAC Incorporated\", \"AGCO Corporation\", \"Agilent Technologies, Inc.\", \"Agway Inc.\", \"Apartment Investment and Management Company\", \"Air Products and Chemicals, Inc.\", \"Airborne, Inc.\", \"Airgas, Inc.\", \"AK Steel Holding Corporation\", \"Alaska Air Group, Inc.\", \"Alberto-Culver Company\", \"Albertson\'s, Inc.\", \"Alcoa Inc.\", \"Alleghany Corporation\", \"Allegheny Energy, Inc.\", \"Allegheny Technologies Incorporated\", \"Allergan, Inc.\", \"ALLETE, Inc.\", \"Alliant Energy Corporation\", \"Allied Waste Industries, Inc.\", \"Allmerica Financial Corporation\", \"The Allstate Corporation\", \"ALLTEL Corporation\", \"The Alpine Group, Inc.\", \"Amazon.com, Inc.\", \"AMC Entertainment Inc.\", \"American Power Conversion Corporation\", \"Amerada Hess Corporation\", \"AMERCO\", \"Ameren Corporation\", \"America West Holdings Corporation\", \"American Axle & Manufacturing Holdings, Inc.\", \"American Eagle Outfitters, Inc.\", \"American Electric Power Company, Inc.\" };
private static List<String> driversTaken = new ArrayList<String>();
public static String getRandomDriverName(){
String name = \"\";
while(true){
int randInt = new Random().nextInt(driverNameList.length);
name = driverNameList[randInt];
if(!driversTaken.contains(name)){
driversTaken.add(name);
break;
}
}
return name;
}
public static String getRandomCompany(){
int randInt = new Random().nextInt(listOfSponsors.length);
return listOfSponsors[randInt];
}
public static List<Car> getCars(int size){
List<Car> cars = new ArrayList<Car>(size);
for(int i = 0; i < size; ++i){
cars.add(new Car(getRandomDriverName(), getRandomCompany(), i + 1));
}
return cars;
}
public static void displyCarProgress(List<Car> cars){
for(int i = 0; i < cars.size(); ++i){
cars.get(i).print();
}
}
public static int getWinner(List<Car> cars, double distance){
int index = -1;
double maxDistance = distance;
for(int i = 0; i < cars.size(); ++i){
if(cars.get(i).getTotalOdometerMiles() >= maxDistance){
maxDistance = cars.get(i).getTotalOdometerMiles();
index = i;
}
}
return index;
}
}
________________________________________________________________________________
Race.java:
import java.util.ArrayList;
import java.util.List;
public class Race {
public static void main(String[] args){
List<Car> cars = RaceUtility.getCars(20);
int itr = 0;
while(true){
if(++itr == 5){
itr = 0;
}
else{
continue;
}
for(int i = 0; i < cars.size(); ++i){
cars.get(i).makeProgress();
cars.get(i).reRandomizeSpeed();
}
RaceUtility.displyCarProgress(cars);
int winner = RaceUtility.getWinner(cars, 100);
if(winner != -1){
System.out.println(cars.get(winner).getDriverName() + \" is the winner\");
cars.get(winner).print();
break;
}
}
}
}
_____________________________________________________________________________
FEW LINES OF OUTPUT:
Car Number: 1
Driver Name: Sheba Mirza
Sponser Name: AdvancePCS, Inc.
Progress: 95.46666666666664
==================================================
==================================================
Car Number: 2
Driver Name: Keiko Dolph
Sponser Name: Amerada Hess Corporation
Progress: 92.21666666666664
==================================================
==================================================
Car Number: 3
Driver Name: Bruna Breen
Sponser Name: Allied Waste Industries, Inc.
Progress: 95.21666666666664
==================================================
==================================================
Car Number: 4
Driver Name: Janis Wisneski
Sponser Name: AMERCO
Progress: 97.2
==================================================
==================================================
Car Number: 5
Driver Name: Tamara Havel
Sponser Name: Amazon.com, Inc.
Progress: 95.31666666666668
==================================================
==================================================






