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...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE 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...
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.
Object Design Example: Bank Account Object Implement the bank account class that stores account number and...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and the balance. It has methods to deposit and withdraw money. In addition, the default constructor sets account number to 0000 and balance 0. Other constructor should take the account number and the initial balance as parameters. Finally, the account class should have a static data member that stores how many accounts are created for a given application. a) Draw the UML diagram b) Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT