Question

In: Computer Science

6.6 Lab: Rectangle Class This program creates a Rectangle object, then displays the rectangle's length, width,...

6.6 Lab: Rectangle Class

This program creates a Rectangle object, then displays the rectangle's

  • length,

  • width, and

  • area

    Change this program to calculate and display the rectangle's

  • perimeter.

Example:

In feet, how wide is your house? 20
In feet, how long is your house? 25
The house is 20.00 feet wide.
The house is 25.00 feet long.
The house has 500.00 square feet of area.
The house has 90.00 feet of perimeter.

/**
This program creates a Rectangle object, then displays the rectangle's
- length,
- width, and
- area

Change this program to calculate and display the rectangle's
- perimeter.

*/

#include <iostream>
#include <iomanip>
using namespace std;

class Rectangle
{
private:
double width;
double length;
public:
// setters
void setWidth(double w) { width = w; }
void setLength(double len) {length = len; }
  
// getters
double getWidth() const { return width; }
double getLength() const { return length; }
  
// other functions
double calculateArea() const { return width * length; }
/* Write your code here */
};


int main()
{
double houseWidth; // To hold the room width
double houseLength; // To hold the room length

// Get the width of the house.
cout << "In feet, how wide is your house? ";
cin >> houseWidth;

// Get the length of the house.
cout << "In feet, how long is your house? ";
cin >> houseLength;

// Create a Rectangle object and use setters to assign values to its data members
Rectangle house;
  
house.setWidth(houseWidth);
house.setLength(houseLength);
  
// Display the house's width, length, and area.
cout << setprecision(2) << fixed;
cout << endl;
cout << "The house is " << house.getWidth()
<< " feet wide.\n";
cout << "The house is " << house.getLength()
<< " feet long.\n";
cout << "The house has " << house.calculateArea()
<< " square feet of area.\n";

// Display the perimeter below
/* Write your code here */
  
return 0;
}
/***************************************************************
Save the OUTPUT below


*/
//Change the code only ever so slighty, not using advanced algorithims

Solutions

Expert Solution

CODE:

OUTPUT:

RAW CODE:

#include <iostream>
#include <iomanip>
using namespace std;

class Rectangle
{
private:
double width;
double length;
public:
        // setters
        void setWidth(double w) { width = w; }
        void setLength(double len) {length = len; }
          
        // getters
        double getWidth() const { return width; }
        double getLength() const { return length; }
          
        // other functions
        double calculateArea() const { return width * length; }
        /* Write your code here */
        double calculatePerimeter() const {
                return 2*( width + length ) ;
        }
};


int main()
{
double houseWidth; // To hold the room width
double houseLength; // To hold the room length

// Get the width of the house.
cout << "In feet, how wide is your house? ";
cin >> houseWidth;

// Get the length of the house.
cout << "In feet, how long is your house? ";
cin >> houseLength;

// Create a Rectangle object and use setters to assign values to its data members
Rectangle house;
  
house.setWidth(houseWidth);
house.setLength(houseLength);
  
// Display the house's width, length, and area.
cout << setprecision(2) << fixed;
cout << endl;
cout << "The house is " << house.getWidth() << " feet wide.\n";
cout << "The house is " << house.getLength() << " feet long.\n";
cout << "The house has " << house.calculateArea() << " square feet of area.\n";
cout << "The house has " << house.calculatePerimeter() << " square feet of perimeter.\n" ;
  
return 0;
}

NOTE:
If You have any doubts feel free to comment in comment section.
DO VOTE(LIKE).


Related Solutions

(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Write a program 'Rectangle-Area.c' that inputs the length and width of a rectangle and outputs its...
Write a program 'Rectangle-Area.c' that inputs the length and width of a rectangle and outputs its area. The program consists of two functions: the main function and a function that computes and returns the area of a rectangle. The output of the program should be in the main program, not in the function.      Sample Input: 5 8   Sample Output: The area of a 5 by 8 rectangle is 40.
Create a program to input the length and width of a rectangle and calculate and print...
Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input...
// Creates a Breakfast class // and instantiates an object // Displays Breakfast special information using...
// Creates a Breakfast class // and instantiates an object // Displays Breakfast special information using System; using static System.Console; using System.Globalization; class DebugNine2 {    static void Main()    {       Breakfast special = new Breakfast("French toast", 4.99);       //Display the info about breakfast       WriteLine(special.INFO);       // then display today's special       WriteLine("Today we are having {0} for {1}",          special.Name, special.Price.ToString("C2", CultureInfo.GetCultureInfo("en-US")));   } } class Breakfast {    public string INFO =       "Breakfast is the most important meal of the day.";       // Breakfast constructor requires...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double area = 0;     area = width * length;     cout << "\n***maxArea called" << endl;          cout << "***       rectangleCount = " << Rectangle::rectangleCount << endl << endl;    } Compete this code to find the maximum area of rectangle between a,b,c
A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased
For the following exercises, use the written statements to construct a polynomial function that represents the required information.A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased by twice that amount, express the area of the rectangle as a function of x.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT