Question

In: Computer Science

Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...

Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()).

Ex: If the input is:

2011
18000
2018

where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is:

Car's information:
   Model year: 2011
   Purchase price: 18000
   Current value: 5770

Note: printInfo() should use three spaces for indentation.

___________________________________________________

given starting code:

__________________________________________________

Main.cpp

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

int main(int argc, const char* argv[]) {
int userYear;
int userPrice;
int userCurrentYear;
Car myCar;
  
cin >> userYear;
cin >> userPrice;
cin >> userCurrentYear;
  
myCar.SetModelYear(userYear);
myCar.SetPurchasePrice(userPrice);
myCar.CalcCurrentValue(userCurrentYear);
  
myCar.PrintInfo();

return 0;
}

_________________________________________________

Car.cpp

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

void Car::SetModelYear(int userYear){
modelYear = userYear;
}

int Car::GetModelYear() const {
return modelYear;
}

// TODO: Implement SetPurchasePrice() function

// TODO: Implement GetPurchasePrice() function

void Car::CalcCurrentValue(int currentYear) {
double depreciationRate = 0.15;
int carAge = currentYear - modelYear;
  
// Car depreciation formula
currentValue = (int)
round(purchasePrice * pow((1 - depreciationRate), carAge));
}

// TODO: Implement PrintInfo() function to output modelYear, purchasePrice, and
// currentValue

________________________________________________________________

Car.h

#ifndef CARH
#define CARH

class Car {
private:
int modelYear;
// TODO: Declare purchasePrice member (int)
int currentValue;

public:
void SetModelYear(int userYear);

int GetModelYear() const;

// TODO: Declare SetPurchasePrice() function

// TODO: Declare GetPurchasePrice() function

void CalcCurrentValue(int currentYear);

// TODO: Declare PrintInfo() method to output modelYear, purchasePrice, and
// currentValue

};

#endif

Solutions

Expert Solution

// Please comment below, if you have any doubt

// If the answer is helpful, please give a like :-)

//********************************************************************************

1) Main.cpp

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

int main(int argc, const char* argv[]) {
int userYear;
int userPrice;
int userCurrentYear;
Car myCar;
  
cin >> userYear;
cin >> userPrice;
cin >> userCurrentYear;
  
myCar.SetModelYear(userYear);
myCar.SetPurchasePrice(userPrice);
myCar.CalcCurrentValue(userCurrentYear);
  
myCar.PrintInfo();

return 0;
}

// ********************************************************************************

2) Car.cpp

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

void Car::SetModelYear(int userYear){
modelYear = userYear;
}

int Car::GetModelYear() const{
return modelYear;
}

void Car::SetPurchasePrice(int boughtPrice){
purchasePrice = boughtPrice;
}

int Car::GetPurchasePrice() const{
return purchasePrice;
}

void Car::CalcCurrentValue(int currentYear) {
double depreciationRate = 0.15;
int carAge = currentYear - modelYear;
// Car depreciation formula
currentValue = (int)round(purchasePrice * pow((1 - depreciationRate), carAge));
}

Car::PrintInfo() const{
cout << "Car's information:" << endl;
cout << " Model year: " << modelYear <<endl;
cout << " Purchase price: " << purchasePrice <<endl;
cout << " Current value: " << currentValue <<endl;
}

//*********************************************************************

3) Car.h

#ifndef CARH
#define CARH

class Car {
private:
int modelYear;
int purchasePrice;
int currentValue;

public:
void SetModelYear(int userYear);

int GetModelYear() const;

void SetPurchasePrice(int boughtPrice);

int GetPurchasePrice() const;

void CalcCurrentValue(int currentYear);

void PrintInfo() const;

};

#endif


Related Solutions

How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp)...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. Ex: If the input is: M&M's 10.0 34.0 2.0 1.0 where M&M's...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp)...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
5. In the following code snippet, which member function of the class Car is called first?...
5. In the following code snippet, which member function of the class Car is called first? class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed();...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
complete sudoku problem //*************************************************************** // D.S. Malik // // This class specifies the functions to solve...
complete sudoku problem //*************************************************************** // D.S. Malik // // This class specifies the functions to solve a sudoku problem. //*************************************************************** class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to promt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid =...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT