I was not sure which board to post this on because it is about an assignment that is partially an application and partially an applet. I am taking a class and it this is my homework assignment. I could use some help: We have to develop a Rectangle Class with the following requirements: - the class contains 3 private instance variables: width, length, color - the class contains a variable called ID used for assigning a unique serial number to each object of that type: ID can be read but not changed. - Rectangle can be constructed four different ways: 1. by specifying width, length, color 2. by specifying width, length 3. by specifying width (to create a square) 4. by specifying nothing(to create an object with default size) Rectangel class contains methods for: -computing the area -drawing the rectangle using its color field Rectangle class also contains the following methods: setLength - for setting the length of the rectangle setWidth - for setting the width of the rectangle getLength - for returning the length of the rectangle getWidth - for returning the with of the rectangle equals - for checking to see whether two rectangles are identical( return true or false) We have to write the class such that the height and width can only have values between 10 and 100. In other words, if the values are less than 10, they should equal 10, and if they are more than 100, they should equal 100. There are 2 test classes we need to use to test the Rectangle class with. One is the following:
The second one is the following:
So far this is what I have, and I know it is all wrong. I would appreciate any help you could give me to clean it up:
[ April 21, 2002: Message edited by: Dirk Schreckmann ]
Hello Randy, I edited your post above just to format the code using the [ code ] and [ /code ] tags (without the spaces). A couple of tips: 1. The above tags can be used to preserve formatting of code. 2. The tab character maintains some relation to the devil (according to Jim Yingst). Some code editors will represent a tab character using about 4 spaces, some with 8, I've seen some use 16 (or more maybe). I think the UBB code tags use about 8 - which is a bit obnoxious. ----------------------- The code you've presented has quite a few errors in it that won't allow it to compile. Off the top of my head: I remember seeing the access modifier spelled as "Private" when it should be "private"; you probably don't want a private class; when a method returns, it gives up control and any lines after the return aren't executed, and in the above example, the compiler will let you know that the statements after the return statements are unreacheable (and as such are not allowed); and there was at least one missing closing "}". The compiler will let you know when it encounters problems. The first few lines of its complaints often yield useful information for locating the problem. If I get a chance, I'd be glad to take a closer look at your code - and someone else might help you out as well. Good Luck. [ April 22, 2002: Message edited by: Dirk Schreckmann ]
Hi Randy, Lets talk about that logic you were mentioning. 1. a private class is of no use to anyone. That is why the compiler won't even let you do it. When you create a class you should always make it public and inside its' own java file. 2. You should try to have your constructors use overloading. Only the constuctor with the largest number of parameters should perform any work. All others should just call the next one with some defaults. For example:
3. In case you didn't notice Color is a java.awt class and is not an int. Therefore you should use it as such: private Color color; 4. All setXXXX methods should be public, so they can be called from other classes, and return void. 5. Since you have to perform the 10,100 check many times I would make it a private method. For example:
Then you can use it like this: length = getValidInputValue( newLength ); 6. The unique ID variable will require a static int value to keep track of the last unique ID. Something like this:
Since the value is final you will need to assign it inside your constructor. Since we have only one constructor that performs the work (see number 2 above), it becomes quite easy to do.
Notice the pre-increment operator to make sure that the next instance will get a different ID. Also according to your testing procedure the first ID should be 1 not 0. The value itself must be final so that other programs can't change it and will get a compiler warning if they try to change it (as your test program explains). 7. I am not sure of your level of programming or what your professor expects, but for the equals method you should accept an Object and make sure it is of the correct type. Something like:
Of course, this might be beyond your level so you can get away with just accepting a Rectangle:
Good Luck, Manfred.
Joined: Apr 16, 2002
Thank you for your help. I'm still a bit confused, but hopefully I'll work it out.
Joined: Dec 10, 2001
When designing your Rectangle class, you might want to just take it one step at a time, so you're trouble shooting as few things at once as possible. While considering the specifications, the basic idea would be to: 1. Define a very basic Rectangle class 2. Maybe add a field or two 3. Maybe add a constructor or two 4. Maybe add a method or two Getting it to compile and work as expected (testing) after each addition and/or change would limit the number of problems you'd be trying to solve at once. If you have any more specific questions (or are in a state of general confusion - and who isn't really?), just ask. Good Luck.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com