After the class is done I have to write an application using/testing the above class. In that app I have to have a menu with the following options:
1 - to set the length
2 - to set the breadth
3 - to get the length
4 - to get the breadth
5 - to get the perimeter
6 - to get the area
7 - to print the object as string
0 - to quit
The program should create one Rectangle object at the beginning using the default constructor, and then repeatedly call the appropriate method for that object depending on the user selection from the above menu.
Ok I wanted to share all of that with you all so that you knew what I was trying to do. I have written a class that I hope is close to what I am trying to do. We havent covered this to much in class that is why I am coming to you. My code for the class is as follows. If you see that I am way off or something just let me know, I know I am not the best at this and I need a lot of work, so if I am just tell me. Thanks!! I appreciate any comments, good or bad
You probably don't need to define perimeter and area as variables. Instead, just define methods to calculate and return these values based on the length and breadth variables.
The toString() method can be overridden to return any String that you think would be helpful in identifying that particular instance. For example, you might use:
return "Rectangle with length " + length + " and breadth " + breadth;
Your no-args constructor doesn't need to actually do anything. As instance variables, length and breadth will be automatically initialized to zero anyway, so you can just use empty braces for the no-args constructor body. (Having a no-args constructor makes subclassing safer.)
Finally, watch your typos. "publich" is not "public" and "toStrin" is not "toString".
and also ( expected; how can that be expected on the last line of the code?
And thank you marc for your post, I appreciate it. I did what you mentioned and I just have those two errors, assuming it wont pop up with other errors after those are fixed
so for the area and perimeter could i use something like l * b and ( l + b ) * 2 ? since l and b are the length and breadth.
Originally posted by Layne Lund:
For future reference, it is easier for us to help you if you copy-and-paste the exact error message here, rather than trying to paraphrase it. Other than that, it looks like you are on your way to becoming a Java programmer.
Thank you Layne! I will take that advice and also thanks for the comment about being on my way to becoming a Java programmer. Gives me some inspiration
int lenght = 0; will work..
some other small remarks:
* end each 'case' with a break statement:
* Integer.parseInt will throw a NumberFormatException if the user enters something that can't be parsed.. you might want to capture that one..
* I could be wrong, but shouldn't the 'set length' option ask for input from the user, and then set the length? (I would guess that the get lenght option should show the user the current length)
Anyway.. looking nice.. almost there!