I need to create a method getLargestObject() that finds the object in the array list with the largest area that returns its position and outputs the contents of the object. The current loop I am using doesn't work and I'm not sure how to compare the values of the areas in an arraylist so I can get the largest one.
package csu.cole;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File(
"C:/Users/Charles/Desktop/GeometricObjectsData.txt"));
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
while (input.hasNextLine()) {
String line = input.nextLine();
String[] tokens = line.split(", ");
if (tokens[0].equals("CIRCLE")) {
Circle c = new Circle();
if (tokens.length == 4) {
float radius = Float.parseFloat(tokens[1]);
c.setRadius(radius);
String color = String.valueOf(tokens[2]);
c.setColor(color);
Boolean filled = Boolean.valueOf(tokens[3]);
c.setFilled(filled);
c.getArea();
list.add(c);
System.out.println(c.toString());
} else if (tokens.length == 3) {
float radius = Float.parseFloat(tokens[1]);
c.setRadius(radius);
String color = String.valueOf(tokens[2]);
c.setColor(color);
Boolean filled = false;
c.setFilled(filled);
c.getArea();
list.add(c);
System.out.println(c.toString());
} else if (tokens.length == 1) {
String color = "white";
c.setColor(color);
Boolean filled = false;
c.setFilled(filled);
c.getArea();
list.add(c);
System.out.println(c.toString());
}
} else if (tokens[0].equals("RECTANGLE")) {
Rectangle r = new Rectangle();
if (tokens.length == 5) {
float height = Integer.parseInt(tokens[1]);
r.setHeight(height);
float width = Integer.parseInt(tokens[2]);
r.setWidth(width);
String color = String.valueOf(tokens[3]);
r.setColor(color);
Boolean filled = Boolean.valueOf(tokens[4]);
r.setFilled(filled);
r.getArea();
list.add(r);
System.out.println(r.toString());
} else if (tokens.length == 1) {
String color = "white";
r.setColor(color);
Boolean filled = false;
r.setFilled(filled);
r.getArea();
list.add(r);
System.out.println(r.toString());
}
}
}
}
public int getLargestObject() {
int max = Integer.MIN_VALUE;
for (int = 0; i < list.size(); i++){
if (list.get(i) > max){
max = list.get(i);
}
}
return max;
}
}