This program is for C++, if the code works and is right I will rate right away!
ID: 3777205 • Letter: T
Question
This program is for C++, if the code works and is right I will rate right away! This is very confusing and would appreciate any help I can get. Thank you!
This project is more of a simulator than an input->output program. You may choose to add some input to the program though.
Skills:
Input/Output Arrays
Loop
Structs
Description:
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.
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."};
Explanation / Answer
Solution:
//CarInfo.java
// import required packages
import java.util.Random;
// class fro car imnformation
public class CarInfo {
private double cartotalOdometerMiles;
private int carspeed;
private String cardriverName;
private String carsponser;
private int crNumber;
// constructor
public CarInfo(String cardriverName, String carsponser, int crNumber)
{
this.carspeed = new Random().nextInt(61) + 60;
this.cardriverName = cardriverName;
this.carsponser = carsponser;
this.crNumber = crNumber;
}
// method to get odometer details
public double getcarTotalOdometerMiles() {
return cartotalOdometerMiles;
}
// method to set odometer details
public void setcarTotalOdometerMiles(double cartotalOdometerMiles) {
this.cartotalOdometerMiles = cartotalOdometerMiles;
}
// method to get car speed
public int getcarSpeed() {
return carspeed;
}
// method to set car speed
public void setcarSpeed(int carspeed) {
this.carspeed = carspeed;
}
// method to get car driver name
public String getcarDriverName() {
return cardriverName;
}
// method to set car driver name
public void setcarDriverName(String cardriverName) {
this.cardriverName = cardriverName;
}
// method to get car sponser
public String getcarSponser() {
return carsponser;
}
// method to set car sponser
public void setcarSponser(String carsponser) {
this.carsponser = carsponser;
}
// method to get car number
public int getCrNumber() {
return crNumber;
}
// method to set car number
public void setCrNumber(int crNumber) {
this.crNumber = crNumber;
}
// method to make progress
public void makecarProgress(){
this.cartotalOdometerMiles += (this.carspeed * (1 / 60.0));
}
// method to convert MPS
public double carconvertToMPS(){
return this.carspeed / 3600.0;
}
// method to randomize car speed
public void carreRandomizeSpeed(){
this.carspeed = new Random().nextInt(61) + 60;
}
// method to print car details
public void carprint(){
System.out.println("==================================================");
System.out.println("CarInfo Number: " + this.crNumber);
System.out.println("Driver Name: " + this.cardriverName);
System.out.println("Sponser Name: " + this.carsponser);
System.out.println("Progress: " + this.cartotalOdometerMiles);
System.out.println("==================================================");
}
}
_____________________________________________________________________________________________
//carRaceUtility.java
// import required packages
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
// class for race utilities
public class carRaceUtility {
// declare the string array
public static String[] cardriverNameList = { "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[] carlistOfSponsors = { "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." };
// create a list for car taken by driver
private static List<String> cardriversTaken = new ArrayList<String>();
// method for select ranom driver
public static String getcarRandomDriverName(){
String Dname = "";
while(true){
int randInt = new Random().nextInt(cardriverNameList.length);
Dname = cardriverNameList[randInt];
if(!cardriversTaken.contains(Dname)){
cardriversTaken.add(Dname);
break;
}
}
return Dname;
}
// method for select ranom car company
public static String getRandomcarCompany(){
int randInt = new Random().nextInt(carlistOfSponsors.length);
return carlistOfSponsors[randInt];
}
// method to get car details
public static List<CarInfo> getCarsdetail(int size){
List<CarInfo> carsinfo = new ArrayList<CarInfo>(size);
for(int lp = 0; lp < size; ++lp){
carsinfo.add(new CarInfo(getcarRandomDriverName(), getRandomcarCompany(), lp + 1));
}
return carsinfo;
}
// method to display car details
public static void displyProgressofcar(List<CarInfo> carsinfo){
for(int lp = 0; lp < carsinfo.size(); ++lp){
carsinfo.get(lp).carprint();
}
}
// method to get winner details
public static int getWinnerdetail(List<CarInfo> carsinfo, double distance){
int index = -1;
double maxDistance = distance;
for(int lp = 0; lp < carsinfo.size(); ++lp){
if(carsinfo.get(lp).getcarTotalOdometerMiles() >= maxDistance){
maxDistance = carsinfo.get(lp).getcarTotalOdometerMiles();
index = lp;
}
}
return index;
}
}
//carRacecls.java:
import java.util.ArrayList;
import java.util.List;
// class for maintain the car race
public class carRacecls {
// main method to test other class and methods
public static void main(String[] args){
List<CarInfo> carsinfo = carRaceUtility.getCarsdetail(20);
int itrtr = 0;
while(true){
if(++itrtr == 5){
itrtr = 0;
}
else{
continue;
}
for(int lp = 0; lp < carsinfo.size(); ++lp){
carsinfo.get(lp).makecarProgress();
carsinfo.get(lp).carreRandomizeSpeed();
}
carRaceUtility.displyProgressofcar(carsinfo);
int gmwinner = carRaceUtility.getWinnerdetail(carsinfo, 100);
if(gmwinner != -1){
System.out.println(carsinfo.get(gmwinner).getcarDriverName() + " is the winner");
carsinfo.get(gmwinner).carprint();
break;
}
}
}
}
Result:
Car Number: 1
Driver Name: Sheba Mirza
Sponser Name: Amerada Hess Corporation
Progress: 95.46666666666664
==================================================
==================================================
Car Number: 2
Driver Name: Keiko Dolph
Sponser Name: AdvancePCS, Inc.
Progress: 92.21666666666664
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.