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

[Java] Please check your code at the link I provide below: http://www.codecheck.

ID: 3814520 • Letter: #

Question

[Java] Please check your code at the link I provide below:

http://www.codecheck.it/files/16122804551rnf6kxx8qha22lcpp9qvfqyt

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

add(Rectangle r) which adds a Rectangle object to the Tiles

totalArea() gets the sum of the areas of all the Rectangles in this Tiles

largest() gets the Rectangle with the largest area. If more than one Rectangle has the same ares, return the first.

toString() gets a string representation of the object. This is given to you

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

Provide Javadoc

Use the following file:

TilesTester.java

[Please finish my code]

import java.util.ArrayList;
/**
* Manages an list of rectangular tiles objects
* @author Jamie J
*/
public class Tiles
{
ArrayList<Rectangle> tiles;
/**
* Constructor initializes the array list
*/
public Tiles()
{
tiles = new ArrayList<Rectangle>();
}
/**
* Add a rectangle object to the array list
* @param r a reactangle object
*/
public void add(Rectangle r)
{
tiles.add(r);
}
/**
* Get the sum of the areas
* @return nothing
*/
public double totalArea()
{
return 0;
}
/**
* Get the largest area
* @return nothing
*/
public Rectangle largest()
{
return null;
}
/**
* Get a string representation
* @return string representation
*/
@Override
public String toString()
{
return tiles.toString();
}
/**
* Get the area of a rectangle
* @return nothing
*/
private double area(Rectangle r)
{
return 0;
}
}

Explanation / Answer

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

Tiles.java


import java.util.ArrayList;

public class Tiles {
   ArrayList<Rectangle> list = null;
   public Tiles() {
       list = new ArrayList<Rectangle>();
   }
   public void add(Rectangle r){
       list.add(r);
   }
   public double totalArea() {
       double total = 0;
       for(Rectangle r: list){
           total = total + r.getArea();
       }
       return total;
   }
   public Rectangle largest() {
       if(list == null || list.size() == 0){
           return null;
       }
       double maxArea = list.get(0).getArea();
       int maxAreaIndex = 0;
       for(int i=0; i<list.size(); i++){
           if(maxArea < list.get(i).getArea()){
           maxArea = list.get(i).getArea();
           maxAreaIndex=i;
           }
       }
       return list.get(maxAreaIndex);
   }
}

Rectangle.java


public class Rectangle {
   private int height, width,x,y;
   public Rectangle(int a, int b,int width, int height){
       this.height = height;
       this.width = width;
       x =a;
       y = b;
   }
   public double getArea() {
       return height * width;
   }
   public String toString() {
       return getClass().getName()+"[x="+x+",y="+y+",width="+width+",height="+height+"]";
   }
}

Output:

0.0
Expected: 0.0
Largest: a5.Rectangle[x=0,y=0,width=100,height=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