13. Carpet Calculator (C++, must work in Visual studios 2017, and must show outp
ID: 3750832 • Letter: 1
Question
13. Carpet Calculator (C++, must work in Visual studios 2017, and must show output of program)
The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor (width times length) by the price per square foot of carpet. For example, the area of floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet that costs $8 per square foot would cost $960. (12 X 10 X 8 X 960.)
First, you should create a class named RoomDimension that has two FeetInches objects as attributes: one for the length of the room and one for the width. (You should use the version of the FeetInches class that you created in Programming Challenge 11 with the addition of a multiply member function. You can use this function to calculate the area of the room.) The RoomDimension class should have a member function that returns the area of the room as a FeetInches object.
Next, you should create a RoomCarpet class that has a RoomDimension object as an attribute. It should also have an attribute for the cost of the carpet per square foot. The RoomCarpet class should have a member function that returns the total cost of the carpet.
888
Once you have written these classes, use them in an application that asks the user to enter the dimensions of a room and the price per square foot of the desired carpeting. The application should display the total cost of the carpet
Explanation / Answer
FeetInches.cpp
#include "FeetInches.h"
FeetInches::FeetInches(void)
{
feet = inches = 0;
}
FeetInches::FeetInches(int f, int i)
{
feet = f;
inches = i;
simplify();
}
FeetInches::FeetInches(FeetInches &obj)
{
inches = obj.inches;
feet = obj.feet;
simplify();
}
FeetInches::~FeetInches(void)
{
}
void FeetInches::simplify()
{
if (inches >= 12)
{
feet += inches / 12;
inches = inches % 12;
}
else if (inches < 0)
{
feet -= ((abs(inches) / 12) + 1);
inches = 12 - (abs(inches) % 12);
}
}
//Collaborated with Shanika Merritt for this function
void FeetInches :: simplifyArea(int inchesInput) {
//1 ft squared = 144 in squared
this->feet = inchesInput / 144;
//Rounds feet up by one if there are any inches remaining
if (inchesInput % 144 > 0) {
this->feet++;
}
}
FeetInches FeetInches::operator - (const FeetInches &obj)
{
FeetInches temp;
temp.inches = inches - obj.inches;
temp.feet = feet - obj.feet;
temp.simplify();
return temp;
}
FeetInches FeetInches::operator--()
{
--inches;
simplify();
return *this;
}
FeetInches FeetInches::operator--(int)
{
FeetInches temp(feet, inches);
inches--;
simplify();
return temp;
}
FeetInches FeetInches::operator + (const FeetInches &obj)
{
FeetInches temp;
temp.inches = inches + obj.inches;
temp.feet = feet + obj.feet;
temp.simplify();
return temp;
}
FeetInches FeetInches::operator++()
{
++inches;
simplify();
return *this;
}
FeetInches FeetInches::operator++(int)
{
FeetInches temp(feet, inches);
inches++;
simplify();
return temp;
}
bool FeetInches::operator<(const FeetInches &obj)
{
bool status;
if (feet < obj.feet)
status = true;
else if (feet > obj.feet)
status = false;
else if (inches < obj.inches)
status = true;
else
status = false;
return status;
}
bool FeetInches::operator==(const FeetInches &obj)
{
bool status;
if (feet == obj.feet && inches == obj.inches)
status = true;
else
status = false;
return status;
}
FeetInches FeetInches::operator*(const FeetInches &obj)
{
FeetInches totalFeetSquared;
totalFeetSquared.feet = 0;
totalFeetSquared.inches = 0;
int totalInches1 = feet * 12 + inches;
int totalInches2 = obj.feet * 12 + obj.inches;
int totalInches12 = totalInches1 * totalInches2;
totalFeetSquared.simplifyArea(totalInches12);
/*temp.inches = totalInches1 * totalInches2;*/
//temp.simplify();
return totalFeetSquared;
}
double FeetInches::operator*(double cost)
{
double totalCost;
totalCost = cost * (feet + inches / 12.0);
return totalCost;
}
ostream &operator<< (ostream &strm, FeetInches &obj)
{
strm << "Feet: " << obj.feet << " Inches: " << obj.inches;
return strm;
}
FeetInches.h
#pragma once
#include <iostream>
using namespace std;
class FeetInches;
ostream &operator<< (ostream &strm, FeetInches &obj);
class FeetInches
{
private:
int feet;
int inches;
void simplify();
public:
FeetInches(void);
FeetInches(int feet, int inches);
FeetInches(FeetInches &obj);
~FeetInches(void);
// mutators -- setters
void setFeet(int feet) { this->feet = feet; }
void setInches(int inches) { this->inches = inches; simplify(); }
// accessors -- getters
int getFeet() { return feet; }
int getInches() { return inches; }
// operators
FeetInches operator - (const FeetInches &);
FeetInches operator--();
FeetInches operator--(int);
FeetInches operator + (const FeetInches &);
FeetInches operator ++ ();
FeetInches operator++(int);
bool operator<(const FeetInches &);
bool operator==(const FeetInches &);
FeetInches operator*(const FeetInches &obj);
double operator*(double cost);
void simplifyArea(int inchesSquared);
friend ostream &operator<< (ostream &strm, FeetInches &obj);
};
RoomCarpet.cpp
#include "RoomCarpet.h"
#include "RoomDimension.h"
#include "FeetInches.h"
RoomCarpet::RoomCarpet()
{
carpetCost = 0;
}
/*Sets roomcarpet values equal to the ones in the passed roomdimension object
Price of carpet is passed through with cost*/
RoomCarpet::RoomCarpet(RoomDimension &roomDimensionObject, double cost) {
measurements.setLength(roomDimensionObject.getLength());
measurements.setWidth(roomDimensionObject.getWidth());
carpetCost = cost;
}
RoomCarpet::~RoomCarpet()
{
}
//Setters
//Sets roomcarpet values equal to the ones in the passed roomdimension object
void RoomCarpet::setRoomDimension(RoomDimension &roomDimensionObject) {
measurements.setLength(roomDimensionObject.getLength());
measurements.setWidth(roomDimensionObject.getWidth());
}
void RoomCarpet::setCarpetCost(double cost) {
carpetCost = cost;
}
//Getters
double RoomCarpet::getCarpetCost() {
return carpetCost;
}
RoomDimension RoomCarpet::getMeasurements() {
return measurements;
}
//Calculate the total cost of the carpet
double RoomCarpet::calculateTotalCost() {
double feetTotal;
double inchesTotal;
double totalCost;
FeetInches area;
area = measurements.calculateArea();
/*Inches was rounded up to feet in calculateArea so we only
need to call the feet value*/
feetTotal = area.getFeet() * carpetCost;
return totalCost;
}
RoomCarpet.h
#pragma once
#include "RoomDimension.h"
#include "FeetInches.h"
class RoomCarpet
{
private:
RoomDimension measurements;
double carpetCost;
public:
RoomCarpet();
RoomCarpet(RoomDimension &roomDimensionObject, double cost);
~RoomCarpet();
void setRoomDimension(RoomDimension &roomDimensionObject);
void setCarpetCost(double cost);
double getCarpetCost();
RoomDimension getMeasurements();
double RoomCarpet::calculateTotalCost();
};
RoomDimension.cpp
#include "RoomDimension.h"
#include "FeetInches.h"
//constructors, destructor
RoomDimension::RoomDimension()
{
this->length.setInches(0);
}
//Sets roomdimension attributes equal to feetinches attributes
RoomDimension::RoomDimension(FeetInches &lengthObject, FeetInches &widthObject) {
this->setLength(lengthObject);
this->setWidth(widthObject);
}
RoomDimension::~RoomDimension()
{
}
//setters
void RoomDimension::setLength(FeetInches &lengthObject) {
length.setFeet(lengthObject.getFeet());
length.setInches(lengthObject.getInches());
}
void RoomDimension::setWidth(FeetInches &widthObject) {
width.setFeet(widthObject.getFeet());
width.setInches(widthObject.getInches());
}
//getters
FeetInches RoomDimension::getLength() {
return length;
}
FeetInches RoomDimension::getWidth() {
return width;
}
//Calculates the area by multipling two FeetInches objects
FeetInches RoomDimension::calculateArea() {
FeetInches calculatedObject;
calculatedObject = length * width;
return calculatedObject;
}
RoomDimension.h
#pragma once
#include "FeetInches.h"
class RoomDimension
{
private:
FeetInches length;
FeetInches width;
public:
RoomDimension();
RoomDimension(FeetInches &lengthObject, FeetInches &widthObject);
~RoomDimension();
//setters
void setLength(FeetInches &lengthObject);
void setWidth(FeetInches &widthObject);
//getters
FeetInches getWidth();
FeetInches getLength();
FeetInches calculateArea();
};
Source.cpp
#include "FeetInches.h"
#include "RoomCarpet.h"
#include "RoomDimension.h"
#include <iostream>
#include <iomanip>
int main() {
int widthFeet, widthInches, lengthFeet, lengthInches;
double cost, totalCost;
//Get input from user
cout << "Please enter the width feet and inches seperated by a space: ";
cin >> widthFeet >> widthInches;
cout << "Please enter the length feet and inches seperated by a space: ";
cin >> lengthFeet >> lengthInches;
cout << "Please enter the price per foot of the carpet: ";
cin >> cost;
//Create two FeetInches object to hold dimensions
FeetInches *width = new FeetInches(widthFeet, widthInches);
FeetInches *length = new FeetInches(lengthFeet, lengthInches);
//Create RoomDimension object to hold FeetInches objects and to calculate area
RoomDimension *rmObject = new RoomDimension(*length, *width);
cout << "The area is: " << (*rmObject).calculateArea() << endl;
//Create RoomCarpet object to hold RoomDimension object and calculate price
RoomCarpet *rmCarpetObject = new RoomCarpet(*rmObject, cost);
//Print/Calculate price of carpet
cout << setprecision(2) << fixed;
cout << "The total cost of the carpet is: $" << (*rmCarpetObject).calculateTotalCost() << endl;
//Delete memory
delete width;
delete length;
delete rmObject;
delete rmCarpetObject;
system("pause");
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.