### Program For Car moving

Problem 1
Write a program that creates Car objects. Your Car class should store the following attributes of a car:
Color ('R'=red; 'G'=green; 'B'=blue; 'W'=white; 'S'=Silver)
Ignition (false=off; true=on)
Position (any X, Y coordinate in a 20 X 20 grid)
main() should create an array of 10 Car instances with random color, ignition set to off/false, and position randomly assigned. Once you assign the beginning state to the cars you should have a loop which asks the user what she wants to do next. For example:
Which car would you like to use next (0-9)?
Then ask the user what she wants to do to the car she selected. For example:
What would you like to do next
1 - change ignition;
2 - change position of car;
3 - quit this program)?
If the user wants to change the car's ignition status it will call the car’s instance method which changes the ignition state (explained below).
If the user wants to change the car's position, main() will prompt the user for a direction (e.g. What direction would you like to move the car (1 - horizontal; 2 - vertical)?) and the distance (e.g. How far (negative value to move left)?; How far (negative value to move up) ?). Then the appropriate method (move horizontal or move vertical -- see below) would be called. You must use care to use the correct car's variables when calling the methods.
Each of the qualities will be represented by an instance variable in the Car class. You should write an instance method for each of the following:
 color assignment: Randomly picks one of the five colors (represented by a letter) and returns the corresponding char to main(). This method should be called once from the Car class’ constructor. The color will not change during the execution of the program.
Fall 2013 Object Oriented Programing Assignment#1
 move car horizontally: This method will need the number of spaces you would like to move the car (a negative value will move the car left; positive value will move the car right) and will adjust the xPosition of the car.
If the car's ignition is not on, this method tells the user that s/he must turn the ignition on first and leaves the xPosition unchanged.
If the user tries to move the car beyond the border of the 20 X 20 grid, this method should leaves the xPosition unchanged.; instead you should report an
error message.
Problem 2
Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 rupees toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected.
Model this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0. A member function called payingCar()increments the car total and adds 50 to the cash total. Another function, callednopayCar(),increments the car total but adds nothing to the cash total. Finally, a member function called display()displays the two totals.
I don't think you're going to get a response that way.
Problem 2

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class tollbooth
{
int no_of_car;
int tot_toll;
public:
tollbooth()
{
no_of_car = 0;
tot_toll = 0;

}
void car_passes();
void paying_car(void);
void non_paying_car(void);
void display(void);
};

void tollbooth :: car_passes()
{
char cartype;
cout<<"\n\nEnter Car Type Paying car(P) & Nonpaying car(N) : ";
cin>>cartype;
if(cartype=='P'||cartype=='p')
paying_car();
else if(cartype=='N'||cartype=='n')
non_paying_car();
}

void tollbooth :: paying_car(void)
{
tot_toll=tot_toll+50;
no_of_car=no_of_car+1;
}
void tollbooth :: non_paying_car(void)
{
no_of_car=no_of_car+1;
}
void tollbooth :: display()
{
cout<<"\n\nTotal number of cars passed :"<<no_of_car;
cout<<"\n\nTotal amount collected :"<<tot_toll;
}

int main()
{
tollbooth t;
int ch;
clrscr();

cout<<"\n1. Car Passes";
cout<<"\n2. Display";
cout<<"\n3. Exit";

cin>>ch;
switch(ch)
{
case 1: t.car_passes();
break;
case 2: t.display();
break;
case 3:exit(0);
}
getch();
return 0;
}
Last edited on
Topic archived. No new replies allowed.