Question

Do exercise 9.1 in the book (page 362, or page 384 of the Comprehensive Version of...

Do exercise 9.1 in the book (page 362, or page 384 of the Comprehensive Version of the book)

Also add a method named getDiagonal which computes and returns the diagonal of the rectangle. For example the distance from the top left corner to the bottom right corner. Or equivalently the distance from the top right corner to the bottom left corner.

Call this method and display it's output in your test program. for the two Rectangle objects.

Here is exercise 9.1 from the book:

(The Rectangle class) Following the example of the Circle class in Section 9.2,
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 the specified width and height.
- A method named getArea() that returns the area of this rectangle.
- A method named getPerimeter() that returns the perimeter.

Write a test program that creates two Rectangle objects—one with width 4 and height 40, and the other with width 3.5 and height 35.9.

Display the width, height, area, and perimeter of each rectangle in this order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Hey dear student finds the solution of your query if you have any doubt feel free to ask. Thanks!!

This C++ program has a class named Rectangle which has two constructors one is default nad other is specified the values for data members and two member functions. One is to calculate Area of Rectangle and returns area, second to calculate the perimeter of rectangle calculate perimeter and returns perimeter. A test program which creates two objects of class Rectangle and specified values and call functions separately as you can see below in code and finally print all information on the screen.

 #include<iostream> using namespace std; class Rectangle //class rectangle { double height; //private data members double width; public: Rectangle() //defalut constructor { width = 1; //default values of data members height = 1; } Rectangle(double h, double w) //parametrized constructor { height = h; //specified values for data members width = w; } double getArea() //function that returns area { double area = height*width; return area; } double getPerimeter() //function that returns Perimeter { double peri = 2*(height+width); return peri; } }; int main() //main function to test { double h,w,h1,w1; //declaration of variables h = 40; //specific values to variables w= 4; h1= 35.9; w1 = 3.5; double area,peri,area1,peri1; Rectangle r1(h,w); //create object of class area = r1.getArea(); //call function area peri = r1.getPerimeter(); //call function and return value store into variable Rectangle r2(h1,w1); //create 2nd object of class area1 = r2.getArea(); //call function and returning value store into variable peri1 = r2.getPerimeter(); //call function and returning value store into variable cout<<" Rectangle 1"; //print all information about each rectangle cout<<"\n\n Width: "<<w; cout<<"\n Height: "<<h; cout<<"\n Area: "<<area; cout<<"\n Perimeter: "<<peri; //Rectangle 2 information cout<<"\n\n Rectangle 2"; cout<<"\n\n Width: "<<w1; cout<<"\n Height: "<<h1; cout<<"\n Area: "<<area1; cout<<"\n Perimeter: "<<peri1;      return 0; } 

C++ Code to Copy:

#include<iostream>
using namespace std;
class Rectangle //class rectangle
{
double height; //private data members
double width;
public:
Rectangle() //defalut constructor
{
width = 1; //default values of data members
height = 1;
}
Rectangle(double h, double w) //parametrized constructor
{
height = h; //specified values for data members
width = w;
}
double getArea() //function that returns area
{
double area = height*width;
return area;
}
double getPerimeter() //function that returns Perimeter
{
double peri = 2*(height+width);
return peri;
}
};

int main() //main function to test
{
double h,w,h1,w1; //declaration of variables
h = 40; //specific values to variables
w= 4;
h1= 35.9;
w1 = 3.5;
double area,peri,area1,peri1;
Rectangle r1(h,w); //create object of class
area = r1.getArea(); //call function area
peri = r1.getPerimeter(); //call function and return value store into variable
Rectangle r2(h1,w1); //create 2nd object of class
area1 = r2.getArea(); //call function and returning value store into variable
peri1 = r2.getPerimeter(); //call function and returning value store into variable
cout<<" Rectangle 1"; //print all information about each rectangle
cout<<"\n\n Width: "<<w;
cout<<"\n Height: "<<h;
cout<<"\n Area: "<<area;
cout<<"\n Perimeter: "<<peri; //Rectangle 2 information
cout<<"\n\n Rectangle 2";
cout<<"\n\n Width: "<<w1;
cout<<"\n Height: "<<h1;
cout<<"\n Area: "<<area1;
cout<<"\n Perimeter: "<<peri1;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Do exercise 9.1 in the book (page 362, or page 384 of the Comprehensive Version of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. 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 the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, 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 the specified width and height. ■ A method named getArea() that returns the...

  • (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class...

    (The Rectangle class) Following the example of the Circle class in Section 9.2, 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 the specified width and height. ■ A method named getArea()...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, 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...

  • Please use Java to answer the question. (The Rectangle class) Following the example of the Circle...

    Please use Java to answer the question. (The Rectangle class) Following the example of the Circle class in Section 8.2, 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 the specified width...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO 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 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    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()...

  • PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to...

    PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 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. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Python...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT