Question: Question: (More Rectangle Class) Please continue work on our previous lab about rectangle class and add the following: 1: One integer data field named numberOfObjects

Question: (More Rectangle Class) Please continue work on our previous lab about rectangle class and add the following:

1: One integer data field named numberOfObjects that specify the number of objects that the rectangle class created in test program, refer to the circle class on textbook.

2: A method called toSting() that returns a string that is one line description of each rectangle class.

Here is an example of toSting() for Circle class.

--------------------------

public String toString()

{

return "The radius is:" + this.radius+","+"The area is:" +this.getArea();

}

---------------------------------

// here is the statement that use this toString() method in test program.

Circle c1 = new Circle();

System.out.println(c1);

3: Modify your test program so that it will use the toString() method and print out the number of objects created by test program.

Bonus (20 points): Declares and creates an array of five Rectangle objects in your test program, initialize rectangle length and width use any number you want, then calculate the total area of the rectangles in the array. (Refer to sample code on our textbook).

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); } } 
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(); } } 

Here is the code from the previous assignment to modify:

############

public class Rectangle {

private double length;

private double width;

public Rectangle() {

length = 0.0;

width = 0.0;

}

public Rectangle(double l, double w) {

if (l>0)

this.length = l;

else

System.out.println("Lenght must be negative");

if (w>0)

this.width = w;

else

System.out.println("Width must be negative");

}

public double getLength() {

return length;

}

public double getWidth() {

return width;

}

public void setLength(double l) {

if (l>0)

this.length = l;

else

System.out.println("Lenght must be negative");

}

public void setWidth(double w) {

if (w>0)

this.width = w;

else

System.out.println("Width must be negative");

}

public double getArea() {

return length*width;

}

public double getPerimeter() {

return 2*(length + width);

}

}

############

import java.util.Scanner;

public class RectangleTest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter length: ");

double len = sc.nextDouble();

System.out.print("Enter width: ");

double wid = sc.nextDouble();

sc.close();

Rectangle rec = new Rectangle();

rec.setLength(len);

rec.setWidth(wid);

System.out.println("The length is " + rec.getLength());

System.out.println("The width is " + rec.getWidth());

System.out.println("Area: "+rec.getArea());

System.out.println("Perimeter: "+rec.getPerimeter());

}

}

/*

Sample run:

Enter length: 4

Enter width: 5

The length is 4.0

The width is 5.0

Area: 20.0

Perimeter: 18.0

*/

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!