Question

In: Computer Science

IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...

IN C++!!!

Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods:

  1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor.
  2. A non-argument constructor method to create a default rectangle.
  3. Another constructor method to create a rectangle with user-specified height and width.
  4. Method getArea() that returns the area.
  5. Method getPerimeter() that returns the perimeter.
  6. Method getHeight() that returns the height.
  7. Method getWidth() that returns the width.

Now design and implement a test program to create two rectangle objects: one with default height and width, and the second is 5 units high and 6 units wide. Next, test the class methods on each object to print the information as shown below.

Solutions

Expert Solution

Program:

#include<iostream>
using namespace std;

class Rectangle
{
public:
double height,width;

Rectangle()
{
height=0;
width=0;
}

Rectangle(double ht,double wd)
{
height=ht;
width=wd;
}

double getHeight()
{
return height;
}

double getWidth()
{
return width;
}

double getArea()
{
return height*width;
}

double getPerimeter()
{
return 2*height+2*width;
}
};

int main()
{
Rectangle rect1;
cout<<endl;
cout<<"Rectangle Object1: "<<endl;
cout<<"---------------------"<<endl;
cout<<"Height: "<<rect1.getHeight()<<endl;
cout<<"Width: "<<rect1.getWidth()<<endl;
cout<<"Area: "<<rect1.getArea()<<endl;
cout<<"Perimeter: "<<rect1.getPerimeter()<<endl;
cout<<endl;

Rectangle rect2(5,6);
cout<<endl;
cout<<"Rectangle Object2: "<<endl;
cout<<"---------------------"<<endl;
cout<<"Height: "<<rect2.getHeight()<<endl;
cout<<"Width: "<<rect2.getWidth()<<endl;
cout<<"Area: "<<rect2.getArea()<<endl;
cout<<"Perimeter: "<<rect2.getPerimeter()<<endl;
cout<<endl;

return 0;
}

Output:

Note: "Next, test the class methods on each object to print the information as shown below." nothing is there in your question. still I tried so I hope it will be helpful for you!!


Related Solutions

DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the...
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle, GeometricObject, and Comparable. Also include explain paragraph.
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT