0

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;
    }
}
ChaCol
  • 223
  • 1
  • 7
  • 19
  • Use `Collections#sort` and pass it a custom `Comparator` that sorts the `GeometricObject` objects in reverse order, pull the first element of the list. If you do this on a copy of the original, you could even get it's original start position – MadProgrammer Oct 15 '14 at 04:26
  • 2
    @MadProgrammer Or just use `Collections#max`. O(n) is better than O(n log n), if you ask me. :-D – C. K. Young Oct 15 '14 at 04:32
  • @ChrisJester-Young Depends on the result you're after, but since this won't change the original list, sounds like a much better idea ;) – MadProgrammer Oct 15 '14 at 04:34
  • How do I compare just one specific value of the objects in the ArrayList? I just want to get the object with the highest area. The objects have other parameters such as radius/height/width/color/filled as well as area. – ChaCol Oct 15 '14 at 04:43
  • http://stackoverflow.com/questions/369512/how-to-compare-objects-by-multiple-fields – Tirath Oct 15 '14 at 04:50

1 Answers1

1

Collections#max is fun, but advanced. Seems like you just need to change your if statement

 if (list.get(i) > max){

to use the area so

 if (list.get(i).getArea() > max){

But you said "returns its position and outputs the contents of the object" so use max to track the location, not the object so

    public int getLargestObjectIndex() {
        int maxIndex = 0;
        for (int = 1; i < list.size(); i++){
            // compare the area of the current index to the max index
            if (list.get(i).getArea() > list.get(maxIndex).getArea()){
                // if this one is bigger, save the location
                maxIndex = i;
            }
        }
        // output the contents of the object
        System.out.println(list.get(maxIndex).toString())
        // return the location
        return maxIndex;
   }

Note that this loop starts at i = 1 because maxIndex starts at 0

ADDruid
  • 196
  • 1
  • 6
  • Heheh, "advanced". This kind of stuff makes me yearn for Ruby: `list.max_by(&:area)` (or, for more flexibility, `list.max { |a, b| a.area <=> b.area }`). – C. K. Young Oct 15 '14 at 05:11
  • @ChrisJester-Young can u explain it in detail plz – Kishan Bheemajiyani Oct 15 '14 at 05:15
  • @Krishna My point is that finding the object in a list with the maximum area is a one-liner in Ruby. It is also a one-liner in Java 8 (though not quite as short as the Ruby), but in older Java versions, it's just a PITA. – C. K. Young Oct 15 '14 at 05:18
  • @ChrisJester-Young ya u are saying true that finding object in list with max area will reduce the load of the Program rather comparing each. plz make me correct if i got anything wrong :) – Kishan Bheemajiyani Oct 15 '14 at 05:22
  • Would I add this in my Driver class or my GeometricObject class, because if I add it to the Driver class it keeps saying illegal modifier, however if I add it to the GeometricObject class it doesn't have access to the ArrayList list. – ChaCol Oct 15 '14 at 05:23
  • @Krishna It will certainly reduce the programmer's brain load. And programmer performance is generally much more important than code performance. – C. K. Young Oct 15 '14 at 05:25
  • Scala has similarly nice shortcuts, but always keep in mind these are just sytactic sugar. Max on an unsorted list will never be better than O(n). Typing less characters is always nice, but learning the fundamentals is much more important. – ADDruid Oct 15 '14 at 07:06