Question
A Java problem. Complete the class Tiles which manages an ArrayList of Rectangles. Each rectangle represents a ceramic floor tile. Tiles contains a constructor and
A Java problem. Complete the class Tiles which manages an ArrayList of Rectangles. Each rectangle represents a ceramic floor tile. Tiles contains a constructor and methods to process the rectangles
Provide a constructor that initializes the instance variable with an empty array list. Call the instance variable tiles.
Provide methods
1. add(Rectangle r) which adds a Rectangle object to the Tiles
2. totalArea() gets the sum of the areas of all the Rectangles in this Tiles
3. largest() gets the Rectangle with the largest area. If more than one Rectangle has the same ares, return the first.
4. toString() gets a string representation of the object. This is given to you
5. a private helper method area(Rectangle r) to get the area of a Rectangle. Use the helper method in the implementation of totalArea() and largest()
There is the Tiles(part of) and TilesTester, please complete the class Tiles.
Tiles.java
import java.util.ArrayList;
import java.awt.Rectangle; public class Tiles { public String toString() { return tiles.toString(); } }
TilesTester.java
public class TilesTester { public static void main(String[] args) { Tiles rectangles = new Tiles(); rectangles.largest(); //should not throw an exception System.out.println(rectangles.totalArea()); System.out.println("Expected: 0.0"); //add some rectangles rectangles.add(new Rectangle(0, 0, 20, 75)); rectangles.add(new Rectangle(0, 0, 100, 50)); rectangles.add(new Rectangle(0, 0, 80, 40)); rectangles.add(new Rectangle(0, 0, 50, 100)); rectangles.add(new Rectangle(0, 0, 50, 50)); Rectangle max = rectangles.largest(); if (max != null) { System.out.println("Largest: " + max); System.out.println("Expected: Rectangle[x=0,y=0,width=100,height=50]"); } double area = rectangles.totalArea(); System.out.println("total area: " + area); System.out.println("Expected: 17200.0"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started