Results 1 to 9 of 9
- 04-28-2011, 09:10 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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:
and the code they will use to check it: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; } }
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(); } }
- 04-28-2011, 09:17 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
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).
- 04-28-2011, 09:29 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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?
-
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.
- 04-28-2011, 09:37 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
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.
- 04-28-2011, 09:55 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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:
Edit: I see that you've been told most of this in your previous threads!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 } }Last edited by Fubarable; 04-28-2011 at 10:03 PM.
- 04-28-2011, 10:05 PM #8
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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 doLast edited by droidus; 04-28-2011 at 10:09 PM.
-
Similar Threads
-
Why only one of the two rectangles is painted?
By JOHNINALBANY in forum Java 2DReplies: 5Last Post: 07-07-2012, 10:54 PM -
Collision between 2 rectangles
By CNew in forum New To JavaReplies: 1Last Post: 12-05-2010, 04:18 AM -
Drawing Rectangles - NullPointerExceptions
By cselic in forum Java 2DReplies: 4Last Post: 05-20-2010, 02:37 PM -
Rectangles method
By bdario1 in forum New To JavaReplies: 31Last Post: 03-31-2010, 09:32 PM -
How to Draw Round Rectangles in SWT
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks