Runescape Bits & Bytes https://www.rsbandb.com/forums/ |
|
Need some help with Java!! https://www.rsbandb.com/forums/viewtopic.php?f=38&t=82123 |
Page 1 of 1 |
Author: | Riceay13 [ October 2nd, 2011, 9:11 pm ] |
Post subject: | Need some help with Java!! |
Hi guys! I need help with writing a method. I have three classes, a Circle class (creates a circle object onto a canvas), a Canvas class, and a SuperCircleDrawer class (and arraylist for Circles). I need to make a method called drawLargestCircle in the SuperCircleDrawer class that takes no arguments and causes only the largest circle in the collection to be drawn. In the case of a tie, it doesn't matter which of the largest circles you draw, but you should only draw one. All other circles should be made invisible. I can use for each and/or while loops. Circle Class SuperCircleDrawer Class |
Author: | Adbot [ October 2nd, 2011, 9:11 pm ] |
Post subject: | Register and login to get these in-post ads to disappear |
Author: | addiv [ October 3rd, 2011, 1:56 am ] |
Post subject: | Re: Need some help with Java!! |
For what its worth, here's one solution: Set an integer variable (largestCircle) to hold the largest index found. Init it to zero and overwrite it to any arraylist index you find that has a larger diameter. Loop arraylist If diameter of circle at current index > diameter of circle at largestCircle set largestCircle = current index End loop If this is a homework assignment try to do this using the pseudo-code above before looking at the code sample below. I've learned more from mistakes than copying someone else's code. Spoiler for Code:
|
Author: | Riceay13 [ October 3rd, 2011, 10:10 am ] |
Post subject: | Re: Need some help with Java!! |
Thanks Addiv. Here's what my method looks like. /** * Draws the largest circle in the arraylist while hiding all of the others. */ public void drawLargestCircle() { int biggestSize; //Holds the largest circle's diameter. biggestSize = 0; for(Circle circles : circleCollection) { if(circles.getDiameter() > biggestSize) { biggestSize = circles.getDiameter() +1; // in case of a tie, this allows only one circle to be set to biggestSize } } for(Circle circles : circleCollection) { if(circles.getDiameter() +1 == biggestSize) { biggestSize = circles.getDiameter() -1; circles.makeVisible(); } else { circles.makeInvisible(); } } } |
Author: | addiv [ October 3rd, 2011, 11:12 am ] |
Post subject: | Re: Need some help with Java!! |
It looks a lot cleaner with the "for each" syntax. It's been a while since I've played around with Java ![]() |
Page 1 of 1 | All times are UTC - 7 hours |
Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |