Solved

Java - Max/Min of X & Y Points

Posted on 2010-09-09
9
338 Views
Last Modified: 2012-05-10
So I am having trouble figuring out why at this code can figure out the max/min of x with points but can only find the max of the y points.

I believe the code is somehow unreachable.. Here is the code:  
public Point[] extremes() {
		if (this.isEmpty())
			return null;

		// left, right , top, bottom
		Point left = null, right = null, top = null, bottom = null;
		Point returnArray[] = new Point[4];

		double xLeft = points[0].getX();
		double xRight = points[0].getX();
		double yUp = points[0].getY();
		double yDown = points[0].getY();
		
		for (int i = 1; i < size; i++) {
			if (points[i].getX() < xLeft) {
				xLeft = points[i].getX();
				left = points[i];
			} else if (points[i].getX() > xRight) {
				xRight = points[i].getX();
				right = points[i];
			}
		}
		
		
		for (int j = 1;j<size; j++) {
			
			if (points[j].getY() > yUp) {
				yUp = points[j].getY();
				top = points[j];
			} else if (points[j].getY() < yUp) {
				yDown = points[j].getY();
				bottom = points[j];
			}
		}
		
		
		returnArray[0] = left;
		returnArray[1] = right;
		returnArray[2] = top;
		returnArray[3] = bottom;
		
		
		
		
		
		return returnArray;
	}

Open in new window

0
Comment
Question by:kilroyitt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33643091
first glance is you never checked to see if it equalled yUp, you only check for greater than or less than

also, shouldn't your arrays start with 0?
0
 

Author Comment

by:kilroyitt
ID: 33643169
Sorry I should have given more information about the problem, the array that is sent to this method is an array of points. Of those points I need to figure out which points are the highest/lowest on each axis. So the return array only needs to be 4 in length.

Comment to rocklroads: do you mean 0 for the for loops. I did that because wanted to initialize the points, so they could be compared.

*For this problem I do not need to worry about multiple points at the same height
0
 
LVL 35

Expert Comment

by:mccarl
ID: 33643289
In lines 27 & 30 you comparing to yUp on both lines. Line 30 should be comparing against yDown.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:mccarl
ID: 33643295
Also, why do it in two loops. You can just have the one loop and compare both X and Y in that loop!
0
 

Author Comment

by:kilroyitt
ID: 33643360
I had it one for loop initially but decided to split it in hopes of troubleshooting the problem:
*I have also corrected that problem in line 30, but I am still having trouble getting the bottom most to become something else than null.


To test here are the point I have been using
[(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]

And the output:
leftmost point in square: (2.0,2.0)
rightmost point in square: (4.0,2.0)
highest point in square: (3.0,3.0)
lowest point in square: null

Here is the current updated code:
public Point[] extremes() {
		if (this.isEmpty())
			return null;

		// left, right , top, bottom
		Point left = null, right = null, top = null, bottom = null;
		Point returnArray[] = new Point[4];

		double xLeft = points[0].getX();
		double xRight = points[0].getX();
		double yUp = points[0].getY();
		double yDown = points[0].getY();
		
		for (int i = 0; i < size; i++) {
			if (points[i].getX() < xLeft) {
				xLeft = points[i].getX();
				left = points[i];
			} else if (points[i].getX() > xRight) {
				xRight = points[i].getX();
				right = points[i];
			}else if (points[i].getY() > yUp) {
				yUp = points[i].getY();
				top = points[i];
			} else if (points[i].getY() < yDown) {
				yDown = points[i].getY();
				bottom = points[i];
			}
		}
		
		
		returnArray[0] = left;
		returnArray[1] = right;
		returnArray[2] = top;
		returnArray[3] = bottom;
		
		
		
		
		
		return returnArray;
	}

Open in new window

0
 
LVL 35

Expert Comment

by:mccarl
ID: 33643377
On line 21, Don't make it an "else" condition. Make two totally separate if/else blocks, it's just that they can be in the one loop. And, you have them around the wrong way, for the yUp yDown comparison. If you think carefully about 3 scenarios, a y less than the current minimum, a y between the min and max and a y greater than the max, and follow it through the if/then/else that you had, you should see the issue.

It should be like below...

public Point[] extremes() {
		if (this.isEmpty())
			return null;

		// left, right , top, bottom
		Point left = null, right = null, top = null, bottom = null;
		Point returnArray[] = new Point[4];

		double xLeft = points[0].getX();
		double xRight = points[0].getX();
		double yUp = points[0].getY();
		double yDown = points[0].getY();
		
		for (int i = 0; i < size; i++) {
			if (points[i].getX() < xLeft) {
				xLeft = points[i].getX();
				left = points[i];
			} else if (points[i].getX() > xRight) {
				xRight = points[i].getX();
				right = points[i];
			}

                        if (points[i].getY() < yDown) {
				yDown = points[i].getY();
				bottom = points[i];
			} else if (points[i].getY() > yUp) {
				yUp = points[i].getY();
				top = points[i];
			}
		}
		
		
		returnArray[0] = left;
		returnArray[1] = right;
		returnArray[2] = top;
		returnArray[3] = bottom;
		
		
		
		
		
		return returnArray;
	}

Open in new window

0
 
LVL 35

Expert Comment

by:mccarl
ID: 33643390
Sorry, forget that last part about the order of the comparison, I am obviously still waking up and my brain is not functioning 100% !!
0
 

Author Comment

by:kilroyitt
ID: 33643431
Even with the split x & y if/else statements and swapped yup and ydown if elses the output remains the same. Do you know why the left point object is not be initialized?

0
 
LVL 35

Accepted Solution

by:
mccarl earned 125 total points
ID: 33643499
Ah, I see what the issue is (and hopefully the last one). When you initialise the variable that hold the min/max values, you don't initialise the variables that hold the point that is the min/max. Because in your test data it is the first point that has the min Y, no other point is smaller than it so lines 24,25 in my last post (correctly) never get run. So you just have to change line 6 to ...
Point left = points[0], right = points[0], top = points[0], bottom = points[0];

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert from a json string array to a Java object 3 81
Tagging and Merging on Branch 1 70
DTD and JAVA versions 1 59
IBM TS2900 (3572) Tape Autoloader Java? 12 109
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question