Results 1 to 20 of 32
Thread: Rectangles method
 03252010, 03:34 AM #1Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
Rectangles method
I have a rectangles method that i need to create that will do the following when tested.
For findSmallest, the input array{new Rectangle(0, 0, 3, 8), new Rectangle(1, 1, 6, 7), new Rectangle(2, 2, 6, 4)} should return the last rectangle object  Rectangle(2, 2, 6, 4)
Do not use library classes to manipulate or search through arrays. (The Arrays class is not allowed.)
This is what i have so far and i am stuck please help
Java Code:public static Rectangle findSmallest (Rectangle[] rectangles) { { //Rectangle min = new Rectangle (1038,1029); int width; int height; int area; int width2; int height2; int area2; // find the maxIndex to find how long new array will be for(int i=0; i < rectangles.length; i++) { //Scanner scan = new Scanner ((Readable) rectangles[i]); width = rectangles[0]; height = rectangles[1]; area = width * height; //Scanner scan2 = new Scanner ((Readable) min); width2 = rectangles[0]; height2 = rectangles[1]; area2 = width2 * height2; if (area < area2) System.out.print("Rectangle"+rectangles[i]); if (area > area2) System.out.print("Rectangle"+rectangles[i]); if (area == area2) System.out.print("Rectangle"+rectangles[i]); } } return rectangles; }
 03252010, 04:03 AM #2Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
Do you already have a Rectangle class, or is that what you need to write? If it already exists, what/where is its API? In what class is findSmallest() being defined?
Gary
 03252010, 04:06 AM #3Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
here are the documents for the method we need to create.
/**
* This method finds and returns the smallest rectangle in the
* array. The smallest rectangle is defined as the one
* with the smallest area. If two rectangles have the same
* smallest area, the one that occurs last in the array
* is returned.
* <p>
*
* This method requires that the rectangles array
* must not contain null.
* <p>
*
* <p><b>You will write this method as part of programming assignment #9.</b>
*
* @param rectangles An array of rectangle objects
* @return The smallest rectangle in the array
* @throws NullPointerException If rectangles is null or rectangles contains null
*/
and i need to test it with For findSmallest, the input array{new Rectangle(0, 0, 3, 8), new Rectangle(1, 1, 6, 7), new Rectangle(2, 2, 6, 4)} should return the last rectangle object  Rectangle(2, 2, 6, 4)
 03252010, 04:11 AM #4Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
The actual findSmallest() method will go something like this:
Java Code:set Rectangle result to null set smallestArea to 0 for each Rectangle in array if this Rectangle's area is less than smallestArea or smallestArea is 0 set smallestArea to this Rectangle's area set result to this Rectangle return result
GaryLast edited by gcalvin; 03252010 at 04:18 AM. Reason: drugs :)
 03252010, 04:13 AM #5Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
 03252010, 04:17 AM #6Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
Are we talking about this Rectangle?
Rectangle (Java Platform SE 6)
If so, in a forum it would be helpful to say you are talking about java.awt.Rectangle.
Gary
 03252010, 04:19 AM #7Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
there is no class i need to write one. So i need to write a findSmallest method once called on the new Rectangle arrays it will compute the area of the rectangles stored in the array and show the smallest rectangle.
 03252010, 04:23 AM #8Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
 03252010, 04:28 AM #9Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
I cant use the java.awt.Rectangle the code i am getting stuck on accessing the values from the array
 03252010, 04:42 AM #10Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
Why can't you use java.awt.Rectangle?
If that is true (you need to write your own Rectangle class?) then your Rectangle class must have a constructor as defined in the exercise:
Java Code:public Rectangle(double x, double y, double width, double height) { ... }
Java Code:public double getWidth() { ... } public double getHeight() { ... }
Java Code:public double getArea() { ... }
 03252010, 04:47 AM #11Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
Sorry, maybe instead of this:
Java Code:public Rectangle(double x, double y, double width, double height) { ... }
Java Code:public Rectangle(double upperLeftX, double upperLeftY, double lowerRightX, double lowerRightY) { ... }
Gary

 03252010, 04:50 AM #13Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
 03252010, 07:00 AM #14Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
Here is what is asked of me, i can not import anything and i can not change the method.
Java Code:public static Rectangle findSmallest(Rectangle[] rectangles)
Requirements
For each method, you may not change the JavaDoc comments, the method name, the method visibility, the parameters, or the return type. Limit your changes to within the body of each method.
Do not use library classes to manipulate or search through arrays. (The Arrays class is not allowed.)
If desired, you may add private static methods to the class, but no class variables. (You may add local variables to the body of each method.) Your solutions should be confined to the ArrayExercises class.
This is what it should do
For findSmallest, the input array{new Rectangle(0, 0, 3, 8), new Rectangle(1, 1, 6, 7), new Rectangle(2, 2, 6, 4)} should return the last rectangle object  Rectangle(2, 2, 6, 4).
Java Code:/** * This method finds and returns the smallest rectangle in the * array. The smallest rectangle is defined as the one * with the smallest area. If two rectangles have the same * smallest area, the one that occurs last in the array * is returned. * <p> * * This method requires that the rectangles array * must not contain null. * <p> * * <p><b>You will write this method as part of programming assignment #9.</b> * * @param rectangles An array of rectangle objects * @return The smallest rectangle in the array * @throws NullPointerException If rectangles is null or rectangles contains null */ public static Rectangle findSmallest (Rectangle[] rectangles) { return null; // You will write this method as part of programming assignment #9. } }
Last edited by bdario1; 03252010 at 07:03 AM.
 03252010, 07:27 AM #15Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 5
Well, one way or another you are going to need a Rectangle class. You say you cannot import anything, but I have not seen that in your requirements. I see this:
Do not use library classes to manipulate or search through arrays. (The Arrays class is not allowed.)
The pseudocode I provided you describes what you need to do. Whether you use java.awt.Rectangle or write your own Rectangle class, follow the steps in the pseudocode to write your findSmallest() method.
Further hints:
Java Code:public static Rectangle findSmallest (Rectangle[] rectangles) { // correct so far { // why this curly brace? //Rectangle min = new Rectangle (1038,1029); // no int width; // none of these are needed int height; int area; int width2; int height2; int area2; // find the maxIndex to find how long new array will be for(int i=0; i < rectangles.length; i++) // OK, but there are better ways to iterate through an array { //Scanner scan = new Scanner ((Readable) rectangles[i]); // a Scanner will not help you, but I guess you know that // already, and anyway, a Scanner would require that you // "import something" width = rectangles[0]; // no  width is not an element of the array height = rectangles[1]; // neither is height area = width * height; //Scanner scan2 = new Scanner ((Readable) min); width2 = rectangles[0]; height2 = rectangles[1]; area2 = width2 * height2; if (area < area2) System.out.print("Rectangle"+rectangles[i]); if (area > area2) System.out.print("Rectangle"+rectangles[i]); if (area == area2) System.out.print("Rectangle"+rectangles[i]); } } return rectangles; // no  rectangles is an array, and you need to return one Rectangle }
Gary
 03252010, 08:35 AM #16Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
Ok thanx for the help, yea he mentioned that we can not import anything as we won't need to since we will have to create it ourselves. I will try this when i get home and hopefully i won't get stuck. thanks again
 03312010, 03:28 AM #17Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
Java Code:public static Rectangle findSmallest (Rectangle[] rectangles) { { int smallestArea=0; for(int i=0; i < rectangles.length; i++) { int width = 0; int height = 0; int area = 0; width = rectangles.length3; height = rectangles.length2; area = width * height; if(area<smallestArea) area = smallestArea; if(area>smallestArea) area = smallestArea; if(area==0) area = smallestArea; } } return area; } }
Last edited by bdario1; 03312010 at 04:27 AM.

Let's look at your code in a bit more detail, OK? Please refer to the comments below that correspond to the # numbers in your code
Java Code:public static Rectangle findSmallest(Rectangle[] rectangles) { { // #1 int smallestArea = 0; for (int i = 0; i < rectangles.length; i++) { int width = 0; int height = 0; int area = 0; // #2 width = rectangles.length  3; height = rectangles.length  2; area = width * height; // #3 if (area < smallestArea) area = smallestArea; if (area > smallestArea) area = smallestArea; if (area == 0) area = smallestArea; } } // #4 return area; }
#2: It's good that you're trying to calculate the width of each rectangle, but will this code do this for you? Isn't rectangle.length the length of the rectangle array itself, and doesn't it have nothing to do with the width or height of any Rectangle held within the array? How do you get the individual rectangle that corresponds to index number "i"? Would this work better in this situation?
#3: This is less a Java problem and more of a logic problem. Given your code, will smallestArea ever be changed? You need to work this out on paper and think logically to solve this section.
#4: You return an int, area. What should you be returning instead? Will your method need another variable that is declared at its start to hold the value of what you want to return?
Best of luck and HTH
 03312010, 04:55 AM #19Member
 Join Date
 Mar 2010
 Posts
 46
 Rep Power
 0
Ok that makes sense for the Integer.MAX_VALUE, but the problem is i don't know how to calculate the area from the rectangle array i don't know how to grab the values from it i tried to do rectangles[3] and things like that and i get an error which confuses me even more. For #3 i think i could resolve that once i figure out which one is the the biggest value after i loop through the rectangle arrays. and for #4 i should return an Rectangle object with the biggest area. It just seems simple but i am having problems resolving it by java code.

Have you yet iterated through an array and printed out the contents? If so, you get the Rectangle objects the same way. For example:
Java Code:String[] data = {"one", "two", "three"}; for (int i = 0; i < data.length; i++) { System.out.println(data[i]); }
For #3 i think i could resolve that once i figure out which one is the the biggest value after i loop through the rectangle arrays.
and for #4 i should return an Rectangle object with the biggest area. It just seems simple but i am having problems resolving it by java code.
Keep on slugging!
Similar Threads

method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03262010, 06:12 PM 
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10262009, 08:20 PM 
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07162009, 02:15 PM 
How to Draw Round Rectangles in SWT
By Java Tip in forum SWT TipsReplies: 0Last Post: 06282008, 09:25 PM 
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05072008, 08:37 AM
Bookmarks