Results 1 to 9 of 9
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default rectangles and square constructors

    i have another problem with constructors. here is the problem:
    Complete the class, named Rectangle, that creates a new rectangle, given the integer height (h) and width (w) of the rectangle. This means that the constructor has two parameters.
    Recall that a square is a special case of a rectangle, with its height equal to its width. Hence, a square can be constructed as a Rectangle, with only a single parameter. This is a case of overloading the constructor, by having two constructor methods, each with different parameters than the other.
    Besides the constructors, this class should include a method named perimeter, to compute the perimeter of the rectangle (2 x width + 2 x height), as well as a method named area to compute the area of the rectangle (width x height).
    I wasn't sure what they meant by "constructs".
    here is my code:

    Java Code:
    /**
       A rectangle with height and width.
    */
    public class test
    {
       private int height;
       private int width;
    
       /**
          Constructs a rectangle.
          @param aHeight: the height of the rectangle
          @param aWidth: the width of the rectangle
       */
       public void Rectangle(int aHeight, int aWidth) 
       { 
          int perimeter = (2*aWidth) + (2*aHeight);
       }
    
       /**
          Constructs a rectangle that is a square.
          @param aSide: the length of a side of the square
       */
       public void Rectangle(int aSide) 
       { 
      		int area;    
    		area = aSide*aSide;   
       }
    
       /**
          Computes the perimeter.
          @return the perimeter of the rectangle
       */
       public int perimeter() 
       {
    		return perimeter;
       }
    
       /**
          Computes the area.
          @return the area of the rectangle
       */
       public int area() 
       { 
    		return area;
       }
    }
    and the code they will use to check it:

    Java Code:
    public static String check(int h, int w, int sideSq)
       {
          Rectangle aRect = new Rectangle(h, w);
          Rectangle aSquare = new Rectangle(sideSq);
          return aRect.perimeter() + " " + aRect.area() + " "
               + aSquare.perimeter() + " " + aSquare.area();
       }
    }

  2. #2
    Join Date
    Mar 2011
    Posts
    98
    Rep Power
    0

    Default

    For starters, your constructors should simply create the Rectangle object. So take the constructor parameter(s) and set the Rectangle's properties (height and width).

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    aren't they passed in, such as the height and width, and the length of one side? then you just do the math to create the object?

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

    Default

    Problems:
    • Your code has no constructors. Constructors have no return type, not void not anything, and are named the same as the class.
    • Your class fields are never set by any of your methods. All you're doing is changing variables that are local to your methods, and as soon as the methods end, your local variables disappear.
    • You're trying to return a variable, perimeter, that doesn't exist in the class.


    Solutions:
    • Create true constructor(s).
    • Set your class fields in the constructor(s)
    • Calculate the perimeter only when you need it, only in the getPerimeter() method, and then return it.

  5. #5
    Join Date
    Mar 2011
    Posts
    98
    Rep Power
    0

    Default

    Yes, they are passed in. But in object-oriented programming, you create your objects first, then you invoke methods on them. So first create an object of Rectangle (with a particular height and width). Then, invoke methods like perimeter() and area() on it.

    You have to think object-oriented, not procedural (like C). You may not see the benefit of doing that in a small Rectangle class like this. But for a true OO program, you need to think in terms of classes, objects, and methods.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    so if i am trying to create a rectangle, would this be good code for it?:
    Java Code:
    public void Rectangle(int aHeight, int aWidth) 
       { 
          int rectangle = (2*aHeight) + (2*aWidth);
       }

  7. #7
    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 droidus View Post
    so if i am trying to create a rectangle, would this be good code for it?:
    Java Code:
    public void Rectangle(int aHeight, int aWidth) 
       { 
          int rectangle = (2*aHeight) + (2*aWidth);
       }
    No. You're just creating a temporary int variable named rectangle that exists only in the method and disappears once the method ends. Again, your class needs constructors, needs to set the class fields in the constructors. You really should read the book and tutorials that you're studying as it's all described there how to do this.

    e.g., If I wanted to create a class Foo with a class field, bar, I'd do:

    Java Code:
    public class Foo {
      private int bar;
    
      // my constructor -- same name as class, no return type
      public Foo(int bar) {
        this.bar = bar;  // set the class field
      }
    }
    Edit: I see that you've been told most of this in your previous threads!
    Last edited by Fubarable; 04-28-2011 at 10:03 PM.

  8. #8
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    so when i have
    public Rectangle(int aHeight, int aWidth)
    {
    }
    why does it want a return type if void can't be there since it is a constructor?
    and i guess i have to use this? im not familiar with it, but i will see what i can do
    Last edited by droidus; 04-28-2011 at 10:09 PM.

  9. #9
    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 droidus View Post
    so when i have
    public Rectangle(int aHeight, int aWidth)
    {
    }
    why does it want a return type if void can't be there since it is a constructor?
    What is the name of your class? What is the name of the constructor? Again, please review your book on classes and their constructors.

Similar Threads

  1. Why only one of the two rectangles is painted?
    By JOHNINALBANY in forum Java 2D
    Replies: 5
    Last Post: 07-07-2012, 10:54 PM
  2. Collision between 2 rectangles
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 04:18 AM
  3. Drawing Rectangles - NullPointerExceptions
    By cselic in forum Java 2D
    Replies: 4
    Last Post: 05-20-2010, 02:37 PM
  4. Rectangles method
    By bdario1 in forum New To Java
    Replies: 31
    Last Post: 03-31-2010, 09:32 PM
  5. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:25 PM

Posting Permissions

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