Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default 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;      
        }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  3. #3
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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)

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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
    You should write a getArea(Rectangle) method, if the Rectangle class doesn't already give you one.

    -Gary-
    Last edited by gcalvin; 03-25-2010 at 03:18 AM. Reason: drugs :-)

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by bdario1 View Post
    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)
    You haven't answered any of my questions:
    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-

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  7. #7
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by bdario1 View Post
    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.
    OK, I gave you some pseudo-code above. I think when you say Rectangle, you are talking about java.awt.Rectangle. Try writing your method, and tell us where you get stuck.

    -Gary-

  9. #9
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    I cant use the java.awt.Rectangle the code i am getting stuck on accessing the values from the array

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by bdario1 View Post
    I cant use the java.awt.Rectangle the code i am getting stuck on accessing the values from the array
    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) {
        ...
        }
    And you'll need some more methods too:
    Java Code:
        public double getWidth() {
        ...
        }
    
        public double getHeight() {
        ...
        }
    ...and maybe:
    Java Code:
        public double getArea() {
        ...
        }
    -Gary-

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Sorry, maybe instead of this:
    Java Code:
        public Rectangle(double x, double y, double width, double height) {
        ...
        }
    ...it's supposed to be this:
    Java Code:
        public Rectangle(double upperLeftX, double upperLeftY,
                               double lowerRightX, double lowerRightY) {
        ...
        }
    In any case, there is something in the requirements that you are not telling us yet.

    -Gary-

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gcalvin View Post
    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:
    .....
    -Gary-
    And also a toString() method:
    Java Code:
      public String toString() {
         return // .... your code goes here
      }


    since it appears you will be printing the "rectangle" from within a System.out.println method.

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    And also a toString() method:
    Java Code:
      public String toString() {
         return // .... your code goes here
      }


    since it appears you will be printing the "rectangle" from within a System.out.println method.
    His code is certainly trying to do that, but the requirements don't say that he has to. I'm still pretty sure the exercise is supposed to use java.awt.Rectangle.

    -Gary-

  14. #14
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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)
    This method should find and return the smallest rectangle from the array of rectangles. The smallest rectangle is the rectangle with the smallest area. The input array should not be changed.

    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; 03-25-2010 at 06:03 AM.

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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.)
    But that is not the same as "do not import anything".

    The pseudo-code 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 pseudo-code 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-

  16. #16
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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

  17. #17
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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.length-3;
        	        	height = rectangles.length-2;
        	        	area = width * height;
        	        	if(area<smallestArea)
        	        		area = smallestArea;
        	        	if(area>smallestArea)
        	        		area = smallestArea;
        	        	if(area==0)
        	        		area = smallestArea;
        	        }
        	 }
     return area;     
    }
    }
    Are you talking about something like this cause as i understand it i need to grab the value from position zero and then from position one then multiply it to get the area of the rectangle and then check to see if it is bigger than the previous right? And he has changed the rules we can use the java.awt.Rectangle
    Last edited by bdario1; 03-31-2010 at 04:27 AM.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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;
       }
    #1: I like that you've declared a smallest area variable and that you'll use this to try to find the rectangle with the smallest area. But look, you've initialized it to 0. What if the rectangles in the array have areas of 10, 20, and 30? Would any of these areas be smaller than 0? Will any of them be identified as having the smallest area? Perhaps you don't want to use 0 as the initial value here. One trick is to use Integer.MAX_VALUE, since nothing will be bigger than that. Another trick is to calculate the area of the first rectangle and use it as the initial value for smallest 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

  19. #19
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    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.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bdario1 View Post
    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.
    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]);
          }
    Do you see how I'm getting each String item in the data array using the index "i"? Can you try to do the same with your Rectangle objects?


    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.
    No, you need to find which is the smaller at that time: the current rectangle's area or the most recent smallestArea value.


    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.
    If the method needs to return a Rectangle object, then don't try to return an int. You have to do what the method signature says it will do. You may want to have a Rectangle variable in your method, say smallestRectangle, that holds the current smallest Rectangle.

    Keep on slugging!

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  4. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:25 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •