Question

In: Computer Science

Project 1 - OO Programming Create a program that simulates cars racing and allows users to...

Project 1 - OO Programming

Create a program that simulates cars racing and allows users to bet on the races ( start users with $100 )

In main, prompt the user to enter details needed to create 2 instances of the Car class you create.

Ask the user to bet on which car will win the race ( use the overridden << method to display car details ), ensure the bet is between 1 and the amount of money the user has currently.

Create a Car class that has attributes for name, make, model, quarter mile time, and max speed.

( You can reference https://www.0-60specs.com/0-60-times/ - or just make values up )

Add a method Race that accepts a reference to another instance of car for the car to race.

When racing, add a random -1.0 - 1.0 second adjustment to the quarter mile time to account for wind and other conditions.

return a string result that says which car wins the race and by how much time.

Display the results of the bet and the users new balance.

Allow the user to keep creating cars to race until they are done or out of money.

Solutions

Expert Solution

Code in C++

#include <iostream>
#include <stdlib.h>   
#include <time.h>   
using namespace std;
class Car { //Class for car
public:
string name, make, model; //String for car name, make and model
double q_time, max_speed; //Double data type for quarter mile time and max speed
void input() //Function to input car details by user
{
cout<<"Enter car name:";
cin>>name;
cout<<"Enter car make:";
cin>>make;
cout<<"Enter car model:";
cin>>model;
cout<<"Enter quarter mile time:";
cin>>q_time;
cout<<"Enter max speed:";
cin>>max_speed;
}
void display() //Function to diplay car details
{
cout<<"Name:"<<name <<" Make:"<<make<<" Model:"<<model<<" Quarter mile time:"<<q_time<<" Max speed:"<<max_speed<<"\n";
}
int race(Car b) //Function to race the cars
{
srand (time(NULL)); //Seeding the random number generator
//The below two lines of code add or remove some random time
//between -1 to 1 to the quarter mile time of both cars.
double one_red=((double(rand()) / double(RAND_MAX)) * (1 + 1)) + 1;
double two_red=((double(rand()) / double(RAND_MAX)) * (1 + 1)) + 1;
q_time+=one_red;
b.q_time+=two_red;
if(q_time<b.q_time) //If car 1 has lower quarter mile time, it wins
{
cout<<name<<" won by "<<b.q_time-q_time;
return 1;
}
if(q_time>b.q_time) //If car 2 has lower quarter mile time, it wins
{
cout<<b.name<<" won by "<<q_time-b.q_time;
return -1;
}
if(q_time==b.q_time) //If both cars win
{
cout<<"No body won";
return 0;
}
  
}
};

int main() //Main function
{
int bal=100; //Starting balance is 100 for the user
  
while(bal>0) //While user balance is greater than 0, he can play the game
{ int car_select;
int result;
int bet=1;
Car c1,c2; //Below lines of codes are for entering car details
cout<<"Enter Car 1 details\n";
c1.input();
cout<<"Enter Car 2 details\n";
c2.input();
cout<<" The details of the cars are:\n";
//Displaying the details of the car for the user to choose
cout<<"1.";
c1.display();
cout<<"2.";
c2.display();
cout<<"Select which car to bet on(Press 1 or 2):";//Selecting which car to bet on
cin>>car_select;
cout<<"Select the amount you want to bet:";
while(1)//This would only take bet value which is between 1 and balance of user
{
cin>>bet;
if(bet>=1 && bet<=bal)
break;
}
  
if(car_select) //Based on the car selection, race fucntion would be called
result=c1.race(c2);
else
result=c2.race(c1);
if(result) //If your car wins, result is 1
{
cout<<"You won the bet. Your new balance is:";
bal+=bet;
cout<<bal<<"\n";
}
else if(result==-1) //If your car looses, result is -1
{
cout<<"You lost the bet. Your new balance is:";
bal-=bet;
cout<<bal<<"\n";
}
else //If race is draw
{
cout<<"It was a draw. Your balance is:"<<bal<<"\n";
}
}

return 0;
}

Output:


Related Solutions

Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
Create a web page using PHP that allows the users to create an account (a username...
Create a web page using PHP that allows the users to create an account (a username and a password). There should be a login page that allows users to create an account by entering their preferred username and password. The same page should also let users login if they already have an account. If a user is logged in successfully , they should be able to enter a comment and also read the comments entered by others previously.
C# programming Create a classes called AlphabetDice that simulates a multi-sided dices with alphabet characters on...
C# programming Create a classes called AlphabetDice that simulates a multi-sided dices with alphabet characters on each side. For instance, a 10-sided AlphabetDice will random return 'a','b','c','d','e','f','g','h','i' or 'j'. The custom constructor should require a random seed and the number of sides. It should also have the following: A read-only property Sides that give us the number of sides A method char Next() that returns an appropriate random character (char) On a side note, to cast an integer to a...
1) Three compounds that have been used as fuel in cars and racing cars are ethanol,...
1) Three compounds that have been used as fuel in cars and racing cars are ethanol, nitromethane (CH3NO2, ΔH°f = -105.65 kJ/mole), and gasoline. Using octane (ΔH°f = - 208.24 kJ/mole) as representative of gasoline, calculate the following quantities for each of the three compounds: ΔH° of combustion per mole of the fuel, moles of gas produced per mole of fuel at 25°C and 1 atm pressure, heat (q) produced per gram of fuel, liters of gas produced per gram...
You are designing a web page that allows users to create an event listing. The event...
You are designing a web page that allows users to create an event listing. The event listing should include the date, time, location, title, phone, email, coordinator, and description of the event. The location should be between 10 and 100 characters long. The title should be between 1 and 50 characters long. The description should be between 10 and 200 characters long. Phone number consists of numbers and dashes. Email has to have an @ symbol. All data elements should...
You are designing a web page that allows users to create an event listing. The event...
You are designing a web page that allows users to create an event listing. The event listing should include the date, time, location, title, phone, email, coordinator, and description of the event. The location should be between 10 and 100 characters long. The title should be between 1 and 50 characters long. The description should be between 10 and 200 characters long. Phone number consists of numbers and dashes. Email has to have an @ symbol. All data elements should...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
can someone code this problem please? Introduction Students will create a C++ program that simulates a...
can someone code this problem please? Introduction Students will create a C++ program that simulates a Pokemon battle mainly with the usage of a while loop, classes, structs, functions, pass by reference and arrays. Students will be expected to create the player’s pokemon and enemy pokemon using object-oriented programming (OOP). Scenario You’ve been assigned the role of creating a Pokemon fight simulator between a player’s Pikachu and the enemy CPU’s Mewtwo. You need to create a simple Pokemon battle simulation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT