Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question Details design a class named Rectangle to represent a rectangle . the c

ID: 3627595 • Letter: Q

Question

Question Details
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 a specified width and height.

* a method named getArea() that returns the area of this rectangle.

* a method named getPerimeter() that returns the perimeter.

Draw the UML diagram , implement the class , and write a test program that creates two Rectangle objects .assign width 4 and height 40 to first object and width 3.5 and height 35.9 for the second one . assign color red to all Rectangle objects .
display the properties of both objects and find their areas and perimeters.

it is question 8.1 from the book introduction to java program (eighth edition) page (296)

In addition to the above, in the same program

—Provide get methods for the width and height

—Provide set methods for the width and height that validate the parameter and assign the default value if the parameter is invalid(less than or equal to 0)

—Call the set methods from both constructors

—Provide a static variable numberOfObjects and a static method getNumberOfObjects (these are in the Circle3 class)

—Name your test program TestRectangle.java. You must create a third Rectangle object from user input for the width and height.

—In addition to the output described in this exercise, you must also output the number of objects you created by calling the getNumberOfObjects method.

—Create a UML class diagram based on the example on page 284. Use a simple text editor to do this and save it as a text file: RectangleUML.txt

I am more interested in these bold answers, you can check the question, "java homework about objects - CS 1323" on the Cramster.

Explanation / Answer

public class rectangle{
    /**********************
    *Variable Declaration
    *********************/
    double width;
    double Height;
    static int noofobjects;

    /**************************
    *DEFAULT CONSTRUCTOR
    **************************/
    public rectangle(){
        setWidth(0);              // calling setwidth
       setHeight(0);            // calling setheight
       noofobjects++;
    }
    /*****************************
    *CUSTOM CONSTRUCTOR
    ****************************/
    public rectangle(double wid, double hi){
      setWidth(wid);                   // calling setwidth
       setHeight(hi);             // calling setheight
       noofobjects++;
    }

    /****************************
    *Getters And Setters
    ****************************/
    public void setWidth(double wi)
{

    if(wi<0) Width = 0 ;
    else   Width=wi;
    }

    public void setHeight(double hi)
{

if(hi<0) Height = 0;
else      Height=hi;
    }

  
    public double getWidth(){
       return Width;
    }
  
    public double getHeight(){
       return Height;
    }

    public double getArea(){
       return Width*Height
    }
    public double getPerimeter(){
       return (2*Height)+(2*Width);
    }

   public int getNumberOfObjects()                        // return no of objcets.
    {
    return noofobjects;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote