Question

In: Computer Science

1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the...

1.

Write a class Rectangles which manages an array of Rectangle objects.

The constructor of the Rectangles takes an array of Rectangle objects. You can assume the array is filled

Provide these methods

  • An averageArea method that returns the average area of the Rectangle objects in the array. Only divide one time
  • A method swapMaxAndMin which swaps the Rectangle with the largest area with the one with the smallest area in the array. Only use one loop
  • A method toString which returns the string representation of the underlying array. (You can use Arrays.toString)

Provide javadoc

Tester File:

RectanglesTester.java

import java.awt.Rectangle;

public class RectanglesTester
{


        public static void main(String[] args)
        {
                Rectangle[] recs = {
                                new Rectangle(30, 50,  5, 20),
                                new Rectangle(20, 40, 50, 40),
                                new Rectangle(10, 10, 20, 10),
                                new Rectangle(50, 10, 2, 8)
                };

                Rectangles processor = new Rectangles(recs);


                System.out.printf("Aveage: %.2f\n", processor.averageArea());
                System.out.println("Expected: 579.00");

                processor.swapMaxAndMin();
                System.out.println(processor.toString());
                System.out.println("Expected: [java.awt.Rectangle[x=30,y=50,width=5,height=20], java.awt.Rectangle[x=50,y=10,width=2,height=8], java.awt.Rectangle[x=10,y=10,width=20,height=10], java.awt.Rectangle[x=20,y=40,width=50,height=40]]");

                Rectangle[] recs2 = {
                                new Rectangle(30, 50,  5, 20),
                                new Rectangle(20, 40, 50, 40),
                                new Rectangle(10, 10, 20, 10),
                };

                Rectangles processor2 = new Rectangles(recs2);


                System.out.printf("Aveage: %.2f\n", processor2.averageArea());
                System.out.println("Expected: 766.67");

                processor2.swapMaxAndMin();
                System.out.println(processor2.toString());
                System.out.println("Expected: [java.awt.Rectangle[x=20,y=40,width=50,height=40], java.awt.Rectangle[x=30,y=50,width=5,height=20], java.awt.Rectangle[x=10,y=10,width=20,height=10]]");
        }
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.awt.Rectangle;

public class Rectangles{

    Rectangle rects[];

    public Rectangles(Rectangle inRects[]){

        rects = inRects;

    }

    private double area(Rectangle r){

        return r.getHeight()*r.getWidth();

    }

    public double averageArea(){

        double avg_area = 0.0;

        int len = rects.length;

        for(int i=0; i<len; i++){

            avg_area += area(rects[i]);

        }

        avg_area /= len;

        return avg_area;

    }

    public void swapMaxAndMin(){

        int max = 0;

        int min = 0;

        for(int i=1; i<rects.length; i++){

            if(area(rects[max])<area(rects[i]))

                max = i;

            if(area(rects[min])>area(rects[i]))

                min = i;

        }

        Rectangle temp = rects[max];

        rects[max] = rects[min];

        rects[min] = temp;

    }

    public String toString(){

        String result = "[";

        for(int i=0; i<rects.length; i++){

            result += rects[i];

            if(i!=rects.length-1)

                result+=", ";

        }

        result += "]";

        return result;

    }

}


Related Solutions

Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c...
WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c = Circle(X, Y, R); 2) Please add a constructor that only accepts R value and initializes the Circle at the origin. 3) Please implement a constructor that behaves the same way as the default constructor. 4) Add a method named “touches” to the Circle class which receives another Circle instance and checks whether these two circles touch each other externally and only at one...
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! 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 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. Method getArea() that returns the area. Method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT