Question: Java: 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:

Java: 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 objectsone 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.java: 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.java 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(); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!