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

Refer to the example code of the Circle class in Chapter 8.2, design a class nam

ID: 3689581 • Letter: R

Question

Refer to the example code of the Circle class in Chapter 8.2, design a class named Rectangle to
represent a rectangle. The class contains:
Two double data fields named width and length that specify the width and length of the
rectangle. The default values are 0.0 for both width and length.
A no-arg constructor that creates a default rectangle.
A constructor that creates a rectangle with the specified width and length.
A method named getArea() that returns the area of this rectangle.
A method named getPerimeter() that returns the perimeter.
Accessor and Mutator methods for length and width;
Make sure that your constructors and mutator methods can check the new length and width to make
sure only positive values can be used in your code, otherwise print out error message to the user.
Create your own project in Eclips, add a test class with main method as usual first. Then add
another new class called Rectangle without the main method in the same project. Now you should
have two class files in your project. Implement the Rectangle class first. In your test class main
method, creates two Rectangle objects—one with width 4 and length 40 and the other with
width 3.5 and length 35.9. Display the width, length, area, and perimeter of each rectangle in this
order.
I also uploaded two java files, one is partial code of the Rectangle class, you still need to define the
constructors with two parameters, accessor and mutator methods for data field length, and the
getPerimier() method. The second file is the partial code of the test program class, you need to
create two other rectangle objects, calculate and print out the width, length, area and perimeter for
each rectangle object. You can download and use those two files and add your code to it.

Circle

public class Circle {
private double radius;
// no-arg constructor
public Circle() {
      radius= 0.0;
}
// constructor with parameter
public Circle(double newRadius) {
    if (newRadius>0)
      this.radius = newRadius;
    else
       System.out.println("Radius must be negative");
}

/** Return radius */
public double getRadius() {
    return radius;
}

/** Set a new radius */
public void setRadius(double newRadius) {
    if (newRadius>0.0)
      this.radius = newRadius;
    else
       System.out.println("Radius must be greater than 0");
}

/** Return area */
public double getArea() {
    return radius * radius * Math.PI;
}

/** Return diameter */
public double getDiameter() {
    return 2 * radius;
}

/** Return perimeter */
public double getPerimeter() {
    return 2 * radius * Math.PI;
}

/* Print the circle info */
public void printCircle() {
    System.out.println("the radius is " + radius);
}
}

TestCircle


public class TestCircle {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Circle circle1 = new Circle(1.0);
      
        System.out.println("The radius is " + circle1.getRadius());
        System.out.println("The area is " + circle1.getArea());
        System.out.println("The diameter is " + circle1.getDiameter());
        circle1.printCircle();
          
   }

}

In java

Explanation / Answer

class Rectangle
{
    double width,length;
    Rectangle()
    {
        width=0.0;
        length=0.0;
    }
    Rectangle(double w,double l)
    {
        if(w>0 && l>0)
        {
        width=w;
        length=l;
        }
        else
        System.out.println("Dimensions can't be negative");
    }
    public double getWidth()
    {
        return width;
    }
    public double getLength()
    {
        return length;
    }
    public void setLength(double l)
    {
        if(l<0)
        System.out.println("Length must be greater than zero");
        else
        length=l;
    }
    public void setWidth(double w)
    {
        if(w<0)
        System.out.println("Width must be greater than zero");
        else
        width=w;
    }
    public double getArea()
    {
        return width*length;
    }
    public double getPerimeter()
    {
        return 2*(width+length);
    }
  
  
}
public class TestRectangle{

     public static void main(String []args){
         Rectangle r1=new Rectangle(40,4);
         Rectangle r2=new Rectangle(35.9,3.5);
         System.out.println(" =====Info of rectangle 1======");
         System.out.print("Width: "+r1.getWidth()+" Length: "+r1.getLength());
         System.out.print(" Area: "+r1.getArea()+" Perimeter: "+r1.getPerimeter());
          System.out.println(" =====Info of rectangle 2======");
         System.out.print("Width: "+r2.getWidth()+" Length: "+r2.getLength());
         System.out.print(" Area: "+r2.getArea()+" Perimeter: "+r2.getPerimeter());
         System.out.print(" ");
     }
}