tags:

views:

79

answers:

3

I'm working on a self-assigned project over break, and am running into one major sticking point. This is a basic line-recognition program in Java.

I have a Points class which creates the Point from the ordered pairs & sends it to the Line class for slope & intercept calculation. The results are sent back to the Points class which deposits the results into the ArrayList linelist. There are three parts to each Line object: the slope & intercept integers and the Point from which they were calculated from.

From a loop, how can I access the slope & intercept variables from each object in the linelist for comparison? Since I don't want to compare the points, just the slope & intercept, the equals() method is not what I need.

+1  A: 

From a loop, how can I access the slope & intercept variables from each object in the linelist for comparison?

Just add another loop over the same list inside the loop and do the comparison in there.

BalusC
+1  A: 

You could have your Line object implement the Comparable interface. You could then put the comparison logic within the Line class in the compareTo method. The logic there will test whether the slope and the intercept match, and if so return 0. Other results could return 1 or -1 to enforce ordering, if you want.

Obviously, you don't need to implement this interface to effect your desired results. However, as this is homework, doing so will allow you to play around with some features of the JDK, like using sorted Lists, etc. With a sorted list, for example, you could obviate the need for a nested loop, as you would only need to compare the object in question against one you just removed from the Iterator in the previous iteration to see if you need to add it to an existing linelist or start a new one.

akf
+2  A: 

It sounds like you want something like this:

class Point {
    final int x;
    final int y;
    Point(int x, int y) { this.x = x; this.y = y; }
}

class Line {
    final int slope;
    final int intercept;
    Line(Point p1, Point p2) {
        this.slope = ...;
        this.intercept = ...;
    }
}

class Points {
    public void doIt(ArrayList<Point> points) {
        ArrayList<Line> lines = new ArrayList<Line>();
        for (int i = 0; i < points.size(); i++) {
            for (int j = i + 1; j < points.size(); j++) {
                lines.add(new Line(points.get(i), points.get(j));
            }
        }
        for (int i = 0; i < lines.size(); i++) {
            for (int j = i + 1; j < lines.size(); j++) {
                Line line1 = lines.get(i);
                Line line2 = lines.get(j);
                if (line1.slope == line2.slope && line1.intercept == line2.intercept) {
                    // blah blah blah
                }
            }
        }
    }
}

(For the peanut gallery, yes there is scope for micro-optimization. But no, it isn't worth the effort unless you've profiled the program and found there to be a real and significant bottleneck.)

[For a more elegant and scalable solution, override the equals(Object) (and hashcode()) methods on Line class and replace the lines list with a TreeSet. This will reduce O(N**2) comparisons to O(NlogN) comparisons. But that's overkill for a basic programming exercise.]

Stephen C
What you wrote is pretty much identical to my own code except for the second for loop, and that's what I was tripping up at.This is for a class that I had to drop in the first couple weeks of the spring semester last year due to personal trouble and am scheduled to start it in 3 weeks. Just wanted to try and get a head start since this semester is gonna be a skull-cracker.
Jason