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;
    }
}
5
  • 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 Commented Oct 15, 2014 at 4:26
  • 2
    @MadProgrammer Or just use Collections#max. O(n) is better than O(n log n), if you ask me. :-D Commented Oct 15, 2014 at 4: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 ;) Commented Oct 15, 2014 at 4: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. Commented Oct 15, 2014 at 4:43
  • stackoverflow.com/questions/369512/… Commented Oct 15, 2014 at 4:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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 }).
@ChrisJester-Young can u explain it in detail plz
@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.
@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 :)
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.