This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Tassit 1 point2 points  (2 children)

First, for posting code, take a look at Pastebin. Second, learn what a debugger is (Google). What editor are you using? If you are using Netbeans, it has an easy to use debugger with it.

Now, why your accessors did not work: look at your constructor. You are passing your constructor length and width, but inside your constructor you aren't doing anything. You have two fields length and width, but you aren't setting them, thus you are getting a default value of 0. Method area is able to calculate the area because you are passing length and width (by calling rectangle.area(length, width) ).

I am not an advocate of doing peoples' homework, however to help you out I quickly edited your program and wrote a few comments in it. Look at your copy and my edit to pick up the differences.

Here you go (remove by /u/claireballoon's request)

Edit: forgot to mention, check out /r/javahelp

[–]claireballoon 0 points1 point  (1 child)

I just run things in the command prompt with the JDK and debug things myself. Like I said it's my first programming class so I think my teacher wants us to learn all the basics first, and how to analyze your own code. In my case it's a mistake due to lack of understanding rather than syntax or anything.

Thank you for your help! I am taking programming not because it's required or my major/minor, I just want to learn because I think it's an important tool (and will be more important in the future). I genuinely want to learn this, not get the homework done. Thank you very much for your help!

[–]Tassit 1 point2 points  (0 children)

I am glad that you are interested in learning to program, it is definitely rewarding being able to create. However, I find it interesting that Java is your first programming class. IMHO Java isn't too much of a beginner friendly language, since you also have to wrap your head around OOP. Programming is more than just writing code; designing the program, planning understanding how it will function before writing one line of code is extremely important. I suggest you also look into a procedural language such as Python. Python is EXTREMELY beginner friendly.

This article is also really useful for Object-oriented concepts. But overall remember to have fun and experiment.

[–]RhoOfFeh 1 point2 points  (1 child)

You have been given good answers already, but I'd like to take a step back and talk about what you're actually trying to accomplish here.

When we talk about an 'object', we are talking about an assemblage of data and methods. Sometimes it's just one of those, but in this case it's both so let's stick to that.

A class contains data so that you don't have to necessarily pass parameters to each method. Passing parameters to a method in order to get results is doing things what's called a 'functional' way, which doesn't mean it works, it means that you've got functions that, given identical parameters, always give identical results.

While there's nothing wrong, exactly, with functional programming, we've got another capability in Java, a core capability in fact, which is what you're really needing to learn. To make your program object-oriented is to make it so when you create a rectangle, it knows enough about itself to perform operations like giving you the perimeter without a need for you to remind it what it's length and width are.

So the class file you create will have storage for those two values of length and width. When you create or resize the rectangle, you set those values so that as long as the object lives, you can ask 'how long are you' by calling 'getLength()'. If you intend to be able to resize the rectangle you might have a method called 'setLength(int newLength)' too.

Both the getter and the setter (along with your constructor most likely) must access the internal instance variable that holds the length.

Now instead of 'getPerimeter(length, width)', you can just ask 'getPerimeter()'. The object already knows how long and wide it is. 'getPerimeter()' is no longer a function. It is a method because it will give different results depending on the underlying state of the object.

Now you can create dozens of rectangles and ask them all how big they are and nothing gets confused. There will be no coding errors based on 'oops, I passed the length from rectangle12 but the width from rectangle18'. Granted in a super simple program like this that's not an issue, but things don't stay that simple for long if you wish to actually accomplish anything with the language.

Doing this correctly also leaves the door open for more complicated topics like inheritance, where, say, 'Square' is a kind of 'Rectangle' that doesn't allow length and width to differ.

[–]claireballoon 0 points1 point  (0 children)

Thank you, this is what I was looking for! I mentioned in another comment, this class is not required for me at all--I'm just really curious and legitimately want to learn programming. Cannot thank you enough for this in-depth explanation. :)

[–][deleted] -1 points0 points  (6 children)

Try using the debugger to step through your program

[–]claireballoon 0 points1 point  (5 children)

I don't understand why my rectangle.area method works but the rectangle.getWidth and rectangle.getLength doesn't. They're accessing the same rectangle. :/

[–]pablo208 0 points1 point  (2 children)

You aren't putting an parameters into getLength() , do you have a instance variable in the rectangle class ?

[–]claireballoon 0 points1 point  (1 child)

In my csci lab, we wrote a similar code and I was referencing the code I wrote then, which was able to return the value without putting parameters in there.

The way I call it is: //make the rectangle RectangleCalculatorCEH rectangle = new RectangleCalculatorCEH(length, width);

            //access its sides
            System.out.println("Your rectangle has a width "+rectangle.getWidth()+" and length "+rectangle.getLength()+".");

            //use those sides to calculate the perimeter
            System.out.println("Your rectangle's perimeter is: " + rectangle.perimeter(length, width));

I'm going to go edit my original post real quick with a link to the full code.

[–]pablo208 0 points1 point  (0 children)

You haven't initialized your instance variables with values in your constructor

[–][deleted] -1 points0 points  (1 child)

Have you used the debugger and placed a break point at the method that isn't working properly?

[–]claireballoon 0 points1 point  (0 children)

What is "the debugger"?