File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calling methods from another class with constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling methods from another class with constructors" Watch "Calling methods from another class with constructors" New topic
Author

Calling methods from another class with constructors

Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
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?
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Welcome to the Ranch

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Answer the question "When does the calculations() method execute?" and you should be closer to seeing why you're getting the results you're getting.


Junilu - [How to Ask Questions] [How to Answer Questions]
Rykurex Peters
Greenhorn

Joined: Oct 28, 2012
Posts: 21
Thank you!!
I understand what I had done wrong now ^_^
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Well done
It is surprising how that sort of thing can be difficult to spot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling methods from another class with constructors