aspose file tools*
The moose likes Beginning Java and the fly likes classes and applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classes and applications" Watch "classes and applications" New topic
Author

classes and applications

Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok I am working on a project for class. I have to write a class called Rectangle that maintains two attributes to represent the length and breadth of a rectangle. I have to provide suitable get, set, and toString methods plus two methods that return the perimeter and area of the rectangle. There must be two constructors for this class. One a parameterless constructor that initializes both the length and breadth to 0, and the second one that takes two parameters to initialize the length and breadth.

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

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
start by trying to compile what you've written so far
eg setlength() != Setlength()

when it compiles OK, you can add toString()
recompile
when it compiles OK, you can add calculateArea()
recompile
when it compiles OK, etc
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok I have an error saying identifier expected (such as at public void int Setlength( int length )) so do I need to declare that? If so is that done at top of the code with the others? I think that might be what that means. I also have one other at the last line saying '(' expected. Many I hate it when I make dumb mistakes such as the spelling!! LOL.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In your "set" methods, you want to assign the argument value to your instance variable. So if you're assigning breadth = b, then your method argument should be defined as (int b) instead of (int breadth).

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".


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
what does this type of error mean: identifer expected; I am getting that on this line...



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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
public void int Setlength( int l )

you have to decide whether you want the method to return nothing (void), or to return an int, meaning you will need a return statement.

you can't do both
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok I think I have all of the little problems out, it compiles!! What do you all think! Thanks for all the great help. I will get started on the application now and I am sure I will have a few questions about that. Here is the final code for my class:

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Good job!

Layne


Java API Documentation
The Java Tutorial
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
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.

Good job!

Layne


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
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
I have a question concerning the class. I am using the class above and using it in an application and I am getting an error saying "variable length might not have been initialized" and there are about 4 of those. If they are initialized in the class do they need to be initialized in the app? My class is above and here is my app:

Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
anybody have any ideas why this might be happening? It may be something simple I am missing. Thanks.
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
The error msg means that you have to initialise your variable called length:

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!
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Thank you so much for all of your advice. I fixed some problems and everything is working fine except for my loop I think. Let's say the first dialog pops up asking to enter in 0-7 and i do that and lets say I enter in 3 well the dialog box says "Breadth is set at 0" blah blah, after that I should be able to click enter and the beginning dialog box pop up again. Is this not working because my loop is not set up right?
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok I figured everything out except why the perimeter and area do not work they pop up saying 0 everytime, is that something that needs to be changed in the class or app? Little confused about that.
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
perimeter shouldn't be a variable of your main class.. just create a method in your Rectangle class called getPerimeter or something, and show that to the user instead of the perimeter variable you'r using now..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classes and applications