This week's giveaway is in the Java/Jakarta EE forum. We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line! See Secondly, I'm trying to call a method from a class with all of the calculations to print out the area of a room that is to be painted, but when I input the dimensions, it's coming out at 0 and I can't figure out why
This is the class with all the calculations;
And here is the class containing the main method;
I'm trying to learn so please try to explain what it is I haven't done if you can
Thanks a lot
You have got far too may fields in that class. You ought to have a room class, which has the three dimensions (and obviously has no doors or windows ). Then you can work out its surface area, which is not a basic characteristic of the room. It is secondary to l w h. And the number of paint cans required is nothing to do with the room at all. It is something to do with painting, so that belongs in a different class.
But you need to work out how you are going to get the area. You can calculate it at the end of the constructor and cache it, or you can calculate it in the getArea() method. You would appear to be doing neither.
Rykurex Peters wrote:First off, I've noticed a lot of programs have things such as "length = l" and then in the class where the constructor is, it's named lengthRoom or something like that, does this actually affect how the program works?
What you name your variables does not affect how the program executes, except that if you have a local variable with the same name as a member variable, or a member variable in a child class with the same name as an accessible member variable in an ancestor class, one varaible can hide the other. In the case of locals, you just have to use "this.X" to distinguish the member variable from the local "X". In the case of member hiding member, just don't do it in the first place.
It's also worth pointing out that lowercase el (l), uppercase eye (I) and uppercase oh (O) are terrible variable names, since l, I, and 1 can be hard to distinguish depending on the font, as can 0 and O.
Secondly, I'm trying to call a method from a class with all of the calculations to print out the area of a room that is to be painted, but when I input the dimensions, it's coming out at 0 and I can't figure out why
You're never calling your calculations() method.
posted 5 years ago
Thanks a lot for the replies but I'm still not sure quite what to do.
(I will go over this and create it with 3 classes but right now I don't have enough time to do so :/)
Is it not okay that I have areaToPaint = formula goes here ... as a field?
Also, I tried doing separate methods for the calculations but I still had to declare all of the variables at the top anyway, is there something I'm overlooking?
And Jeff, thanks for the help about the variable names But I'm trying to call the getArea() method, so it returns the area
I've been messing about with it for a while but still can't figure out how to get it running