View the step-by-step solution to:

Question

Hello, the UML and Code is below, can someone help me figure out what I am doing wrong. I just keep getting so

many errors that I don't know what to do, anymore. Thanks for your help


DIRECTIONS:

Using Visio or Word, create, a UML diagram for a class to represent an object that can be contained within your other class. Some examples are given in the Introduction. For your new class, add at least 2 private attributes. For example, a HardDrive class might have a type (SSD, HHD, etc.) and a capacity (number of gigabytes or terabytes). A Car engine might have a number of cylinders and the horsepower rating. A refrigerator might have a freezer that can be described by its location (bottom, top, side-by-side) and a capacity.

For the methods, add two constructors (default and one that takes parameters), a setter and getter for each attribute and a function that prints out the attribute information. For example:

This is a ___ cylinder engine with a horsepower of ___.

If you are using Visio, you can connect your two UML shapes with relationship line indicating composition. Example:


Here is a possible UML for my new Engine class:

Engine

-numberOfCylinders : int

-horsepower : int

+Engine()

+Engine(cylinders : int, hp : int)

+setCylinders(cylinders : int) : void

+getCylinders() : int

+setHP(hp : int) : void

+getHP() : int

+printInfo() : void

 

Step 2 - Coding New Class

Open your project from last week and save it under a new name such as: CIS247_CP2_Team_1.

Use the Add Class function to make a header file and .cpp file for your new class. Code the new class in the header and source files as we did before. We now have 5 separate files in our project.

Step 3 - Update UML of Original Class

Update the UML of your original class to include an object of the contained class. I would add this line to my PropPlane private attributes:

           -engine1 : Engine

Then I would add a public setter and getter for the contained object. For example:

           +setEngine(eng1 : Engine) : void

           +getEngine() : Engine

Add a parameter for the object to the constructor that takes parameters; Example:

+PropPlane(manu : string, mod : string, engines : int, cost : double, year : int, used : bool, eng1 : Engine)

Step 4 - Update code for Original Class

In the header file of your first class, put a #include for the header file of your new class. For example:

#include "Engine.h"  //I put this in my PropPlane.h file

Now in the header file, add the new private attribute and the prototypes for the new setter and getter; add the new parameter to the prototype of the constructor with parameters.

In the .cpp file:

  • update the default constructor to call the default constructor of your included object.
  • update the constructor with parameters to handle the new parameter you added to the header file.
  • Add code for the setter and getter for your new class.
  • IMPORTANT: update your print function to print out information about the new class. The preferred way to is to use the print function you already wrote for your new class. For my PropPlane, I would add a line like this to my print function code:

engine1.printInfo();

Step 5 - Coding the Main Function

In your existing main function, update the Documentation header with information on this phase of the project.

Recall that last week we created two objects: one with the default constructor and one with the constructor with parameters. We will update both of these to include the new class.

For the first object, after the declaration you asked the user for values and then used the setters to enter the data. We will do the same for the new class. First declare an object of the new class. For my PropPlane, I would declare:

Engine eng1;

Then ask the user for the data and enter it with the setters. For my example:

int num, hp;   //number of cylinders and horsepower

cout << "Enter number of cylinders in the plane's engine(s): ";

cin >> num;

eng1.setCylinders(num);

cout << "Enter horsepower of engine(s): ";

cin >> hp;

eng1.setHP(hp);

//Now include this object inside of the first object. Let's say I had created plane1:

plane1.setEngine(eng1);

Now I am ready to print out my entire plane:

plane1.printInfo();  //This prints everything; you do not need to call the include object's printer

For the second object, we hardcoded values into the constructor. Do the same for your new class but BEFORE you create the second object. Here is how I would do it for the PropPlane.

Engine eng2(4, 125);   //4 cylinder engine with 125 horsepower

PropPlane plane2("Cessna", "414A", 2, 195000, 1971, true, eng2);

plane2.printInfo();

Step 6 - Update SaveToFile Function

Last week, we included a function that saves our objects to a file. We need to update this function to enter the data for the new included object. No changes need to be made to the code in main that calls the function with a pointer to the object. An example of what needs to be done is shown below, highlighted.

//Code for function; this is written for the PropPlane class; change this for your own class

void saveToFile(PropPlane* ptrPlane)

{

           //open the file and prepare to append data

           ofstream outToFile("data.txt", ios::app);

           //If the file is open, write the data to the file. Customize this for your class attributes using

all the getters

           if (outToFile.is_open())

           {

                       //notice the use of the arrow operator in this code; hypen and greater than sign

                       outToFile << ptrPlane->getManufacturer() << endl;

                        // etc. for all the attributes in PropPlane; new code below; notice the two-stage

                       //use of getters

                       outToFile << ptrPlane->getEngine().getCylnders() << endl;

                       outToFile << ptrPlane->getEngine().getHP() << endl;           

                       outToFile.close();          

                       //Tell user the data was written

                       cout << "Data was written to the file." << endl;

           }

           else

                       cout << "File was not opened successfully." << endl;

}

Step 7 - Debugging and Testing Your Program

Build your program and resolve any errors.

Test your program a few times and make sure that everything is printed correctly with good formatting and no misspelled words. Make sure decimal numbers print nicely including money.

When everything looks good, you are ready to submit.

Step 8 - Submitting Your Week 4 Code

Start a Word document that is named with your Team name. In the document, Team name and the names of all team members who made a contribution this week even if it's just ideas and critique or being a tester.

Paste a copy of the code in the Header files, class code files and main function code file. Paste in a screenshot of your test run(s). Submit this document.

You must also submit the complete project folder, zipped.

Here is an example of my output for my sample project:



My Code:

Source.cpp


#include "Computer.h"

#include <string>

#include <iomanip>

#include <iostream>

#include <cstdlib>

using namespace std;


int main()

// declare object with default constructor

{

      Computer chromebook1;


      chromebook1.setEdition("First");

      chromebook1.setBrand("Google");

      chromebook1.setSizeHardDrive(562);

      chromebook1.setPrice(399.99);

      chromebook1.setrefurbishedComputer(false);


      cout << "Edition: " << chromebook1.getEdition() << endl;

      cout << "Brand: " << chromebook1.getBrand() << endl;

      cout << "Size of Hard Drive" << chromebook1.getSizeHardDrive() << endl;

      cout << "Price: " << chromebook1.getPrice() << endl;

      cout << "Discounted Price: " << chromebook1.calcDiscount() << endl;


      system("pause");

      return 0;

}



Screen.h


#pragma once

#include "Computer.h"


class Screen

{

private:

      int screenSize;

      int resolution;

      int pixels;

      string type;

      double price;

      Computer computer1;

public:

      Screen();                                 // Default constructor

      ~Screen();

      Screen(int size, int res, int pix, string type, double prc);

      int getSize();

      void setSize(int size);

      int getRes();

      void setRes(int res);

      int getPix();

      void setPix(int pix);

      string getType();

      void setType(string type);

      double getPrice();

      void setPrice(double prc);

      void printData();                        // This computer has a _____ size screen with ____ k resolution


};



Screen.cpp


#include "Screen.h"




Screen::Screen()

{

      screenSize = 0;

      resolution = 0;

      pixels = 0;

      type = "unknown";

      price = 0.0;

      computer1;

}



Screen::~Screen()

{

}


Screen::Screen(int size, int res, int pix, string type, double prc) :

      screenSize(size), resolution(res), pixels(pix), type(type), price(prc) {}


void Screen::setSize(int size) { screenSize = size; }

void Screen::setRes(int res) { resolution = res; }

void Screen::setPix(int pix) { pixels = pix; }

void Screen::setType(string type) { type = type; }

void Screen::setPrice(double prc) { price = prc; }

int Screen::getSize() { return screenSize; }

int Screen::getRes() { return resolution; }

int Screen::getPix() { return pixels; }

string Screen::getType() { return type; }

double Screen::getPrc() { return price; }

void Screen::printData();




Computer.h


#pragma once

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;


class Computer

{

private:

      string edition;

      string brand;

      int sizeHardDrive;

      double price;

      bool refurbishedComputer;


public:

      Computer(string edt, string brd, int size, double prc, bool refurb);

      Computer();

      string getEdition();

      string getBrand();

      int getSizeHardDrive();

      double getPrice();

      void setEdition(string edt);

      void setBrand(string brd);

      void setSizeHardDrive(int size);

      void setPrice(double prc);

      void setrefurbishedComputer(bool refurb);

      double calcDiscount();

      void printInfo();

      ~Computer();

};



Computer.cpp


#include "Computer.h"



Computer::Computer()

{

      edition = "unknown";

      brand = "unknown";

      sizeHardDrive = 0;

      price = 0;

      refurbishedComputer = true;

}

Computer::Computer(string edt, string brd, int size, double prc, bool refurb)

{

      this->edition = edt;

      this->brand = brd;

      this->sizeHardDrive = size;

      this->price = prc;

      this->refurbishedComputer = refurb;

}


Computer::~Computer()

{

}

string Computer::getEdition()

{

      return edition;

}

string Computer::getBrand()

{

      return brand;

}

int Computer::getSizeHardDrive()

{

      return sizeHardDrive;

}

double Computer::getPrice()

{

      return price;

}

void Computer::setEdition(string edt)

{

      this->edition = edt;

}

void Computer::setBrand(string brd)

{

      this->brand = brd;

}

void Computer::setSizeHardDrive(int size)

{

      this->sizeHardDrive = size;

}

void Computer::setPrice(double prc)

{

      this->price = prc;

}

void Computer::setrefurbishedComputer(bool refurb)

{

      this->refurbishedComputer = refurb;

}

double Computer::calcDiscount()

{

      return (price -(price* .25));

}

void Computer::printInfo()

{

      cout << "Computer Edition: " << edition << endl;


      cout << "Computer Brand: " << brand << endl;


      cout << "Size of Hard Drive: " << sizeHardDrive << endl;


      cout << "Computer Price " << price << endl;


      cout << "Refurbished? (y/n) " << refurbishedComputer << endl;


      cout << fixed << setprecision(2);

}

Top Answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online