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

A Java problem. Complete the class Tiles which manages an ArrayList of Rectangle

ID: 3801146 • Letter: A

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 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

Explanation / Answer

import java.util.ArrayList;

import java.awt.Rectangle;

class Tiles{
ArrayList<Rectangle> rectangles;
Tiles(){
rectangles = new ArrayList<Rectangle>();
}
void add(Rectangle r){
rectangles.add(r);
}
private int area(Rectangle r){
return r.length*r.width;
}
int totalArea(){
int area = 0;
for(Rectangle r : rectangles){
area = area + area(r);
}
return area;
}
Rectangle largest(){
if(rectangles.isEmpty()){
return null;
}
Rectangle largest = rectangles.get(0);
for(int i=1;i<rectangles.size();i++){
if(area(rectangles.get(i))>area(largest)){
largest = rectangles.get(i);
}
}
return largest;
}
}
class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
       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("Max is "+max.length+" , "+max.width);
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");
   }
}

OUTPUT :

0
Expected: 0.0
Max is 100 , 50
Expected: Rectangle[x=0,y=0,width=100,height=50]
total area: 17200.0
Expected: 17200.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote