Question

Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...

Codes: (car.h ; car.cpp ; carDemo.cpp)

/*
-----------------------
Car
-----------------------
- make: string
- year : int
-----------------------
+ Car()
+ setMake(m: string) : void
+ getMake() : string
+ setYear(y: int) : void
+ getYear() : int
----------------------
*/

#ifndef CAR_H
#define CAR_H
#include <iostream>
using namespace std;

class Car {

private:
    string make;
    int year;

public:
    Car();
    Car(string);
    Car(int);
    Car(string, int);
    void setMake (string);
    string getMake() {return make;}
    void setYear (int);
    int getYear() {return year;}

};
#endif


#include "Car.h"
#include <iostream>
#include <cstdlib>
using namespace std;

Car::Car() {
        make = "";
        year = 0;
}

Car::Car(string m) {
        make = m;
        year = 0;
}

Car::Car(int y) {
        make = "";
        year = y;
}

Car::Car(string m, int y) {
        make = m;
        year = y;
}

void Car::setMake (string m) {
        make = m;
}

void Car::setYear (int y) {
        year = y;
}


#include "Car.h"
#include <iostream>
using namespace std;

int main() {

        Car myCar;
        myCar.setMake("Toyota");
        myCar.setYear(1999);

        cout << myCar.getMake() << " " << myCar.getYear() << endl;

        Car myCar2("Ford");
        cout << myCar2.getMake() << " " << myCar2.getYear() << endl;

        Car myCar3(2016);
        cout << myCar3.getMake() << " " << myCar3.getYear() << endl;

        Car myCar4("Honda", 2005);
        cout << myCar4.getMake() << " " << myCar4.getYear() << endl;

        return 0;
}

Using the previous code for the Car Class, implement this UML:

/*
-----------------------
          Car
-----------------------
- make: string
- year : int
- speed: int
-----------------------
+ Car(string, int)
+ setMake(m: string) : void
+ getMake() : string
+ setYear(y: int) : void
+ getYear() : int
+ setSpeed(s: int) : void
+ getSpeed() : int
+ accelerate() : void
+ brake() : void
----------------------
*/

Note that there is only one constructor which receives arguments for and sets the make and year. Have your constructor then set the speed to zero.

Note the two new member methods: accelerate and brake. Accelerate should increase speed by 5, and brake should decrease speed by 5. Note that your speed can never be a negative number, so ensure that your method definition guards against that by outputting an error message.

Write a demo program that exercises this class. Create a car object, accelerate it, brake it, exercise it. Have your class definition also print out the "state" of the object after each acceleration or braking. Sample output:

Accelerating...
Toyota | 1998 | 5
Accelerating...
Toyota | 1998 | 10
Accelerating...
Toyota | 1998 | 15
Braking...
Toyota | 1998 | 10
Braking...
Toyota | 1998 | 5
Braking...
Toyota | 1998 | 0
Braking... Error: can't brake a car that's standing still.
Toyota | 1998 | 0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//Car.h file

#ifndef CAR_H

#define CAR_H

#include <iostream>

using namespace std;

class Car {

private:

    string make;

    int year;

    //new attribute

    int speed;

public:

    Car(string, int);

    void setMake(string);

    string getMake() const { return make; }

    void setYear(int);

    int getYear() const { return year; }

    //new methods

    int getSpeed() const { return speed; }

    void setSpeed(int s);

    void accelerate();

    void brake();

};

//overloading << operator to display state of the Car

ostream& operator<<(ostream& out, const Car c)

{

    out << c.getMake() << " | " << c.getYear() << " | " << c.getSpeed();

    return out;

}

#endif

//Car.cpp file

#include "Car.h"

#include <iostream>

#include <cstdlib>

using namespace std;

Car::Car(string m, int y)

{

    make = m;

    year = y;

    speed = 0;

}

void Car::setMake(string m)

{

    make = m;

}

void Car::setYear(int y)

{

    year = y;

}

//new method implementations

void Car::setSpeed(int s)

{

    //setting speed only if it is non negative

    if (s >= 0) {

        speed = s;

    }

}

void Car::accelerate()

{

    cout << "Accelerating..." << endl;

    speed += 5;

}

void Car::brake()

{

                //displaying error if speed is 0

    if (speed == 0) {

        cout << "Braking... Error: can't brake a car that's standing still." << endl;

    }

    else {

                //braking and decrementing speed by 5

        cout << "Braking..." << endl;

        speed -= 5;

        //if speed went negative, making it 0

        if (speed < 0) {

            speed = 0;

        }

    }

}

//CarDemo.cpp file

#include "Car.h"

#include <iostream>

using namespace std;

int main()

{

                //creating a Car

    Car myCar("Toyota", 1998);

    //accelerating and displaying state for 3 times

    for (int i = 0; i < 3; i++) {

        myCar.accelerate();

        cout << myCar << endl;

    }

    //braking and displaying state for 4 times

    for (int i = 0; i < 4; i++) {

        myCar.brake();

        cout << myCar << endl;

    }

    return 0;

}

//OUTPUT

Accelerating...

Toyota | 1998 | 5

Accelerating...

Toyota | 1998 | 10

Accelerating...

Toyota | 1998 | 15

Braking...

Toyota | 1998 | 10

Braking...

Toyota | 1998 | 5

Braking...

Toyota | 1998 | 0

Braking... Error: can't brake a car that's standing still.

Toyota | 1998 | 0

Add a comment
Know the answer?
Add Answer to:
Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show the compilation of Programming Challenge below in separate header and implementation files and show a...

    Show the compilation of Programming Challenge below in separate header and implementation files and show a UML diagram of this class. #include “Car.h” Int main() { Car car(2015, “Volkswagon”); Cout<<”Car’s Make: “ << car.getMake() <<endl; Cout<< “Car’s Year:” << car.getYear() <<endl; Cout<< “Car’s Speed : “ <<car.getSpeed()<<endl; Cout<< endl; For(int I =0; I < 5; i++) { Car.acceleration(); Cout<< “Speed after accelerate: “ << car.getSpeed() << endl; } For(int I = 0; i < 5; i++) { Car.brake(); Cout <<...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...

    CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle class by making the following changes: • Create a static variable called numVehicles that holds an int and initialize it to zero. This will allow us to count all the Vehicle objects created in the main class. • Add the copy constructor • Increment numVehicles in all of the constructors • Decrement numVehicle in destructor • Add overloaded versions of setYear and setMpg that...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • This is assignment and code from this site but it will not compile....can you help? home...

    This is assignment and code from this site but it will not compile....can you help? home / study / engineering / computer science / computer science questions and answers / c++ this assignment requires several classes which interact with each other. two class aggregations ... Question: C++ This assignment requires several classes which interact with each other. Two class aggregatio... (1 bookmark) C++ This assignment requires several classes which interact with each other. Two class aggregations are formed. The program...

  • Please provide a multi lined pseudo code for the program below.....also it is giving me a...

    Please provide a multi lined pseudo code for the program below.....also it is giving me a build error? Could you possibly tell me how to fix the error too? #include <iostream> #include <string> using namespace std; class ship { public: string name; int year; ship(string n, int y) { name = n; year = y; } int getYear() { return year; } string getName() { return name; } void setYear(int y) { year = y; } void setName(string n) {...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT