Question

Lab 2 – Classes include the header files for the classes Overview The purpose of this...

Lab 2 – Classes

include the header files for the classes

Overview

The purpose of this assignment is to give you some experience writing classes in C++, including utilizing accessors and constructors to organize data properly.

Description

This program will represent a hypothetical car production line, which consists of a chassis (body of a vehicle) and wheels. These two components will be used to make fully functioning vehicles (just use your imagination). To that end, there are three classes you will be writing:

• Vehicle

• Chassis

• Wheel

For this assignment, main.cpp will be provided for you, so you don’t have to worry about the structure of the program. Instead, you can focus solely on the structure of the classes and their interactions. I suggest looking at main closely, to help you understand the structure of the project. Chassis The Chassis class is the backbone of your vehicle. You will need to store the following data:

• The size of the chassis (small, medium, large)

• The quality of the chassis (poor, fair, good)

• The number of wheels it can support The number of wheels a given chassis can support is dependent on the size of the vehicle. If the vehicle is small, it functions with 3 wheels (the tri-wheeled wonder).

If it is medium, 4 wheels are required. If the vehicle is large, you will need to have 6 wheels. The default constructor for the chassis will insure it is a medium sized chassis of fair quality. You will also need to implement two more constructors, one that will allow you to specify a size for the chassis, and one that will allow you to specify a size and quality for the chassis. Finally, you will need a getter method for the number of wheels called getNumWheels(). Wheels Your Wheel class should contain variables for the following:

• Mileage Left

• Condition the wheel is in (poor, fair, good) The default condition for Wheels made will be fair. A fair wheel will have 10,000 miles available to it at the start. A good wheel will have 20,000 and a poor wheel will start with 5,000. You will once again need a default constructor and a second constructor that overrides the condition of the wheel. Vehicle The Vehicle class will be the finished product of your production line, being comprised of the other objects defined below. You will need to store the following data: • The price • Wheels • A Chassis (body of car)

• Is it drivable? (Boolean) The quality of the chassis and the condition of the wheels will determine the price of the vehicle. The base price of all vehicles is 500. The chassis will apply a multiplier of 5, 8 or 12 to this, depending on its quality. Likewise, the quality of each wheel will apply a multiplier of 1.5, 1.8 or 2.2. (Maybe price should be a float to account for this?). Additionally, a vehicle only becomes drivable once it has the appropriate number of wheels added to it. An example: A good, small chassis with 2 poor wheels and 1 fair wheel. Bolded is the wheel multipliers (3 of them). 500 * 12 * (1.5 * 1.5 * 2.2) = 29,700 In addition to the above, you will need to implement the following methods:

• addWheel() will is fairly self-explanatory, adding a new wheel object to your car (consider using a vector for this). An additional condition is that if you have already added the max number of wheels for the given vehicle (chassis size), the message “You’ve already added all the wheels!” should be displayed

• isBuilt() should return a Boolean as to whether or not the chassis and all wheels have been added to the vehicle.

• Drive() will give your car the ability to go for a test run. The integer it takes in should be the mileage you want the vehicle to travel. If the value entered is greater than the tire with the least mileage left, you should display the output “Broke Down!”. After every drive print “You’ve traveled x miles!” where x is the amount traveled. If you broke down, this value could be different from the value passed into the function. Additionally, make sure to change the condition of the wheels based on their mileage left. If above 10,000 they are good. If above 5,000 they are fair. Below that you have poor wheels. Keep in mind, a change in condition also means a change in price for the vehicle! Finally, if you attempt to drive the vehicle before it is built, the message “Vehicle not built. Literally un-drivable” should be displayed.

• getChassis() should just return the current vehicle’s chassis.

• Display() should display all the information of a vehicle and its associated chassis and wheels. Here is a sample output: Tips A few tips about this assignment:

• You can print out a tab character (the escape sequence '\t') to help line up theoutput.

• Don't try to tackle everything all at once. Work on one class at a time. Can't have a Car without a Chassis.

• You can customize the way numbers are displayed in C++ (particularly floating-point numbers). The header file contains this functionality. Look into std::fixed andstd::setprecision()

Main class

#include "Vehicle.h"
#include "Wheel.h"
#include


int main()
{
Wheel plainWheel;

Chassis plainChassis("medium");
Vehicle firstCar(plainChassis);

for(int i=0; i < firstCar.getChassis().getNumWheels(); i++) {
firstCar.addWheel(plainWheel);
}

//since all plainWheels have been added, it should be built
if(firstCar.isBuilt()) {
std::cout << "Built!\n" << std::endl;
firstCar.Display();
}


Chassis superChassis("large","good");
Vehicle superCar(superChassis);

if(!superCar.isBuilt()) {
std::cout << "Where are the wheels!\n" << std::endl;
}

Wheel superWheel("good");

superCar.addWheel(superWheel);
superCar.addWheel(superWheel);
superCar.addWheel(superWheel);
superCar.addWheel(superWheel);

superCar.addWheel(plainWheel);
superCar.addWheel(plainWheel);

superCar.Display();
superCar.Drive(8000);
superCar.Display();

Wheel badWheel("poor");
Chassis reallyBadChassis("small", "poor");
Vehicle badCar(reallyBadChassis);

for(int i=0; i < badCar.getChassis().getNumWheels(); i++) {
badCar.addWheel(badWheel);
}

badCar.Display();
badCar.Drive(6000);
badCar.Display();

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

Chassis.h file

*/

#include<string>
using namespace std;
class Chassis
{
   string size;
   string quality;
   int noOfWheels;

   public:
       Chassis()
       {
           size="medium";
           quality="fair";
           noOfWheels=setNoOfWheels(size);
       }
       Chassis(std::string size)
       {
           this->size=size;
           noOfWheels=setNoOfWheels(size);
       }
       Chassis(string size,string quality)
       {
           this->size=size;
           this->quality=quality;
           noOfWheels=setNoOfWheels(size);
       }
       int setNoOfWheels(string size)
       {
           if(size == "small")
               return 3;
           else if(size=="large")
               return 6;
           else
               return 4;
       }
       int getNumWheels()
       {
           return noOfWheels;
       }
       string getSize()
       {
           return size;
       }
       string getQuality()
       {
           return quality;
       }
};

/*

Wheel.h

*/

#include<string>
using namespace std;
class Wheel
{
   int mileage;
   string condition;
  
   public:
       Wheel(){
           condition="fair";
           mileage=setMileage(condition);
       }
      
       Wheel(string cond)
       {
           condition=cond;
           mileage=setMileage(condition);
       }
      
       int setMileage(string condition)
       {
           if(condition=="poor")
               return 5000;
           else if(condition=="fair")
               return 10000;
           else if(condition=="good")
               return 20000;
       }
       int setMileage(int mill)
       {
           mileage=mill;
           if(mill>10000)
               condition="good";
           else if(mill>5000)
               condition="fair";
           else
               condition="poor";
       }
       int getMileage()
       {
           return mileage;
       }
       string getCondition()
       {
           return condition;
       }
};

/*

Vehicle.h file

*/

#include<vector>
#include<string>
using namespace std;
class Vehicle
{
   double price;
   vector<Wheel> wheels;
   Chassis chassis;
   bool drivable;
   public:
   Vehicle()
   {
       price=500;
       drivable=false;
   }
  
   Vehicle(Chassis chass)
   {
       chassis=chass;
       price=500;
       drivable=false;
       setPrice(chass.getSize());
   }
   setPrice(string size)
   {
       if(size == "small")
               price=price*5;
       else if(size=="medium")
           price=price*8;
       else if(size=="large")
               price=price*12;
          
   }
   setPrice(Wheel wheel)
   {
       if(wheel.getCondition()=="poor")
           price=price*1.5;
       else if(wheel.getCondition()=="fair")
           price=price*1.8;
       else if(wheel.getCondition()=="good")
           price=price*2.2;
  
   }      
   void addWheel(Wheel wheel)
   {
       if(wheels.size()<chassis.getNumWheels())
       {
               wheels.push_back(wheel);
               setPrice(wheel);
       }
       else
           cout<<"You’ve already added all the wheels" <<endl;
          
       if(wheels.size()==chassis.getNumWheels())
           drivable=true;
   }
  
   bool isBuilt()
   {
       if(wheels.size()==chassis.getNumWheels())
       {
           return true;
       }
       return false;
   }
  
   void Drive(int mill)
   {
       bool brokeDown=false;
       int brokeDownMileage;
       for (auto wheel = wheels.begin(); wheel != wheels.end(); ++wheel)
{
   int wheelMileage= (*wheel).getMileage();
   if(wheelMileage<mill)
   {
       cout<<"Broke Down!"<<" "<<wheelMileage<<endl;
       brokeDown=true;
       brokeDownMileage=wheelMileage;
       break;
           }
       }
      
       for (auto wheel = wheels.begin(); wheel != wheels.end(); ++wheel)
{
   int wheelMileage= (*wheel).getMileage();
   if(brokeDown)
   {
       int remainingMileage=wheelMileage-brokeDownMileage;
       (*wheel).setMileage(remainingMileage);
           }
           else
      
   {
       int remainingMileage=wheelMileage-mill;
       (*wheel).setMileage(remainingMileage);
           }
       }
      
       if(brokeDown)
           cout<<"you have travelled "<<brokeDownMileage<<" miles"<<endl;
       else
           cout<<"you have travelled "<<mill<<" miles"<<endl;
      
   }
   Chassis getChassis()
   {
       return chassis;
   }
  
   void Display()
   {
       cout<<"price: "<<price<<"\t drivable :"<<drivable<< " Chassis size: "<<chassis.getSize()<<" \t chassis no of wheels: "<<chassis.getNumWheels()<<"\t chassis quality: "<<chassis.getQuality()<<endl;
       cout<<"wheel details: "<<endl;
       for (auto wheel = wheels.begin(); wheel != wheels.end(); ++wheel)
{
   cout<<" milleage"<<(*wheel).getMileage()<<"\t condition:"<<(*wheel).getCondition()<<endl;
       }
   }
};

/*

main

*/


#include <iostream>
#include<string>
#include "Chassis.h"
#include "Wheel.h"
#include "Vehicle.h"
using namespace std;


int main()
{
  
Wheel plainWheel;

Chassis plainChassis("medium");
Vehicle firstCar(plainChassis);

for(int i=0; i < firstCar.getChassis().getNumWheels(); i++) {
firstCar.addWheel(plainWheel);
}

//since all plainWheels have been added, it should be built
if(firstCar.isBuilt()) {
std::cout << "Built!\n" << std::endl;
firstCar.Display();
}


Chassis superChassis("large","good");
Vehicle superCar(superChassis);

if(!superCar.isBuilt()) {
std::cout << "Where are the wheels!\n" << std::endl;
superCar.Display();
}

Wheel superWheel("good");

superCar.addWheel(superWheel);
superCar.addWheel(superWheel);
superCar.addWheel(superWheel);
superCar.addWheel(superWheel);

superCar.addWheel(plainWheel);
superCar.addWheel(plainWheel);

superCar.Display();
superCar.Drive(8000);
superCar.Display();

Wheel badWheel("poor");
Chassis reallyBadChassis("small", "poor");
Vehicle badCar(reallyBadChassis);

for(int i=0; i < badCar.getChassis().getNumWheels(); i++) {
badCar.addWheel(badWheel);
}

badCar.Display();
badCar.Drive(6000);
badCar.Display();

getchar();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Lab 2 – Classes include the header files for the classes Overview The purpose of this...
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
  • Python programming I need help with the following: Create a class Vehicle. The class should have...

    Python programming I need help with the following: Create a class Vehicle. The class should have name, weight, fuel, number of wheels and move attributes/methods. Create child class that are semi, car, sail boat and bicycle. Give the class the ability to have a constructor that sets the name. Have the other attributes hard coded to suit the vehicle. Give each a way of speaking their attributes as well as the type of animal. For example “Hi I am Lightening...

  • Complete with javadoc comments for constructor and setter/getter methods Overview The purpose of this lab is...

    Complete with javadoc comments for constructor and setter/getter methods Overview The purpose of this lab is to implement a generic Singly Linked Lists using a generic Node UML for Node<E> and SinglyLinked List< E> Node<E> Node<E> -data:E -next: Node<E> +Node( initialData: E, initialNext :Node<E> ) getData():E + getNext):Node<E> setData( newData : E ):void + setNext( newNext :Node<E>):void + SinglyLinkedList<E> SinglyLinkedList<E -head: Node< E> -tail: Node<E> -numElements : int SinglyLinkedList ( ) getSize():int appendList(newElement : E):void prependList(newElement E):void exists( target: E...

  • Please use Java to write the program The purpose of this lab is to practice using...

    Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....

  • Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in...

    Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in interesting ways. Now you’ll get a chance to use more of what objects offer, implementing inheritance and polymorphism and seeing them in action. You’ll also get a chance to create and use abstract classes (and, perhaps, methods). After this project, you will have gotten a good survey of object-oriented programming and its potential. This project won’t have a complete UI but will have a...

  • 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...

  • Getting the following errors and not sure why. My classes for Van are defined just like...

    Getting the following errors and not sure why. My classes for Van are defined just like for everything else. Also not sure why I am getting the error "undeclared identifier" . Below are the errors when I try and build: ______________________________________________________________________________________ ./vehicles.h:1:2: error: unterminated conditional directive #ifndef VEHICLES_H ^ vehicles.cpp:33:44: error: member initializer 'verbose_' does not name a non-static data member or base class : serialNumber_(0), passengerCapacity_(0), verbose_(false)                                            ^~~~~~~~~~~~~~~ vehicles.cpp:36:10: error: out-of-line definition of 'Vehicle' does not match any...

  • I need help constructing a special inventory system. I need certain components that will check the...

    I need help constructing a special inventory system. I need certain components that will check the age of the customer and then assign them to a car model. It will be business logic classes. To test your classes, you will use them in a relatively small test application. The requirements for testing and the classes within PYTHON 3 are as follows: • Need to build and design TWO components of the reservation sys. One class to represent customers, and another...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

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