Please show me the sample output Write a program that calculates the price of ca
ID: 3717573 • Letter: P
Question
Please show me the sample output
Write a program 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 = 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 Part A) 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.
Note: start by developing the UML diagram.
#ifndef FEETINCHES_H
#define FEETINCHES_H
// The FeetInches class holds distances or measurements
// expressed in feet and inches.
class FeetInches
{
private:
int feet; // To hold a number of feet
int inches; // To hold a number of inches
void simplify(); // Defined in FeetInches.cpp
public:
// Constructor
FeetInches(int f = 0, int i = 0)
{ feet = f;
inches = i;
simplify(); }
// Mutator functions
void setFeet(int f)
{ feet = f; }
void setInches(int i)
{ inches = i;
simplify(); }
// Accessor functions
int getFeet() const
{ return feet; }
int getInches() const
{ return inches; }
// Overloaded operator functions
FeetInches operator + (const FeetInches &); // Overloaded +
FeetInches operator - (const FeetInches &); // Overloaded -
};
#endif
// Implementation file for the FeetInches class
#include <cstdlib> // Needed for abs()
#include "FeetInches.h"
//************************************************************
// Definition of member function simplify. This function *
// checks for values in the inches member greater than *
// twelve or less than zero. If such a value is found, *
// the numbers in feet and inches are adjusted to conform *
// to a standard feet & inches expression. For example, *
// 3 feet 14 inches would be adjusted to 4 feet 2 inches and *
// 5 feet -2 inches would be adjusted to 4 feet 10 inches. *
//************************************************************
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);
}
}
//**********************************************
// Overloaded binary + operator. *
//**********************************************
FeetInches FeetInches::operator + (const FeetInches &right)
{
FeetInches temp;
temp.inches = inches + right.inches;
temp.feet = feet + right.feet;
temp.simplify();
return temp;
}
//**********************************************
// Overloaded binary - operator. *
//**********************************************
FeetInches FeetInches::operator - (const FeetInches &right)
{
FeetInches temp;
temp.inches = inches - right.inches;
temp.feet = feet - right.feet;
temp.simplify();
return temp;
}
Explanation / Answer
Here is the required code to implement RoomDimension, RoomCarpet classes in a single file and demonstrate finding the cost of carpeting a room with given length and width. Thanks.
//code.cpp
#include "FeetInches.h"
#include <iostream>
using namespace std;
class RoomDimension{
FeetInches length;
FeetInches width;
public:
RoomDimension(){
}
//a constructor to accept both length and width of the room
RoomDimension(FeetInches len,FeetInches wid){
length=len;
width=wid;
}
//method to return the area as feet inches object
FeetInches getArea(){
//finding total length in feets (including inches part)
double totalLengthFeet=length.getFeet()+(length.getInches()/12.0);
//finding total width in feets (including inches part)
double totalWidthFeet=width.getFeet()+(width.getInches()/12.0);
//finding the area
double areaFeets=totalLengthFeet*totalWidthFeet;
//creating a FeetInches object using this value. Some truncations will occur
//due to type casting as FeetInches use integer values
FeetInches result(areaFeets);
return result;
}
//setter methods if needed
void setLength(FeetInches len){
length=len;
}
void setWidth(FeetInches wid){
width=wid;
}
};
class RoomCarpet{
RoomDimension roomDimension;
double cost_per_sq_ft;
public:
//constructor to accept RoomDimension object and cost per square feet
RoomCarpet(RoomDimension dimension,double cost){
roomDimension=dimension;
cost_per_sq_ft=cost;
}
//method to find and return the total cost of carpetting
double getCost(){
//finding the area
FeetInches area=roomDimension.getArea();
//finding the total feets in area (including the inches)
double totalFeet=(double)area.getFeet()+area.getInches()/12;
//finding the total cost
double totalCost=cost_per_sq_ft*totalFeet;
return totalCost;
}
};
int main(){
//creating two FeetInches objects for length and width of the room
FeetInches length(12,6);
FeetInches width(10,2);
//creating a RoomDimension using above fields
RoomDimension room(length,width);
//creating a RoomCarpet using the RoomDimension
RoomCarpet carpet(room,8);
//displaying the cost
cout<<"Cost for carpeting a room with length "<<length.getFeet()
<<" ft "<<length.getInches()<<" inches long & "<<width.getFeet()
<<" ft "<<width.getInches()<<" inches wide is $"<<carpet.getCost()<<endl;
return 0;
}
/*OUTPUT*/
Cost for carpeting a room with length 12 ft 6 inches long
& 10 ft 2 inches wide is $1016
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.