• Post Reply Bookmark Topic Watch Topic
  • New Topic

When a constant should be passed into a method  RSS feed

 
Robert James Liguori
Author
Ranch Hand
Posts: 587
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I love the CodeRanch, cause you guys always help me when I'm in a pickle... like I am here...

How do I code up constants that are not really constants?



Reference:  When a constant should be passed into a method - http://robertjliguori.blogspot.com/2017/03/when-constant-should-be-passed-into.html
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I code up constants that are not really constants? 

Can you explain how a constant is not a constant?  That seems like a contradiction.
 
Robert James Liguori
Author
Ranch Hand
Posts: 587
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks norm... from my blog...




So I've been working on air density equations that use the dry air constant.

Since I'm working with Java I set this up simply like this...


Quite beautiful and simple, right?  No, wrong!

Since the constant is made up of gases... the overall 'constant' can be different based on location.  I discovered this eventually by trying to figure out why different air density calculators were giving different results.  For example:


Dry Air Gas Constant of 286.9 from The Engineering Toolbox
Dry Air Gas Constant of 287.22 from Peace Software
Dry Air Gas Constant of 287.05 from Brisbane Hot Air Ballooning
Dry Air Gas Constant of 287.058 from Wikipedia


So with all this being said, how do I implement my code?!? I gander there are a few options...

Constant/non-constant Option 1
OPTION: I could just create constants for each known location...



PROBLEM: However, I don't think this would work as it may not be practical to keep doing coding changes for locations, and what if the values at those locations change?  (you can't dynamically change a final constant).

Constant/non-constant Option 2
OPTION: I could set the code up with getters/setters to allow for the changing of the 'constant' :



PROBLEM: The desire is to have the utility available as a Java utility class, and getters/setters do not support this model.


Constant/non-constant Option 3
OPTION: perhaps I could enumerate the constants, and associate the values after the fact.



PROBLEM:  The code would still be subject to adding and modifying regional values.

Constant/non-constant Option 4
OPTION: I could utilize a web service that queries the dry air gas constant via location. 

PROBLEM: This seems to be the best approach, but such a thing does not exist


Constant/non-constant Option  5
OPTION: Since all the constants are very close in value... only provide one and let the user deal with difference in results from there desired value of which they cannot set.

PROBLEM: The result would not be accurate/precise.

Constant/non-constant Option  6
OPTION: Allow the user to pass into the methods as an argument, the desired constant value.

PROBLEM: Uh yeah, why would someone pass a 'constant' into a method?!?  Well, if you followed the post, this is apparently thee time you'd want to... and probably what makes the most sense.


Do you have other suggestions?  I posted this question to CodeRanch... let's see what those guys and gals have to say.

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this is a Java programming problem at all. It's a design problem. Like, you've got the opinions of four organizations about what the value should be for this constant and you want to use the constant in your application. So, are you going to ask the user which one they'd like to use? Or are you going to choose one and tell the user which one you're using? Or is there a rule that says that Version X should be used by engineers in Country Y because it's a professional standard there and you want to accommodate that? Or what?
 
Robert James Liguori
Author
Ranch Hand
Posts: 587
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't think this is a Java programming problem at all. It's a design problem. Like, you've got the opinions of four organizations about what the value should be for this constant and you want to use the constant in your application. So, are you going to ask the user which one they'd like to use? Or are you going to choose one and tell the user which one you're using? Or is there a rule that says that Version X should be used by engineers in Country Y because it's a professional standard there and you want to accommodate that? Or what?


... right, well said, thank you... so you see why I'm epically confused here.  This isn't helping: https://www.physicsforums.com/threads/why-does-the-dry-air-gas-constant-vary.909000/
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a physicist either. So before deciding which of those constants to use, or whether to inflict the choice on my users, I would ask somebody with expertise the area whether it matters and if so why it matters. So far you haven't got that far with the physics forum people but maybe you could continue there. Perhaps telling them the details would elicit something useful: "I'm trying to write this computer program to do (whatever it is) and I'm finding what looks like conflicting values for this constant, and I don't have the expertise to evaluate whether that's a problem for me or not, so what should I do?" or something along that line.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An observation: The discussion sounds like a how a Locale would be used.  A set of values that depend on ....
 
Robert James Liguori
Author
Ranch Hand
Posts: 587
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:An observation: The discussion sounds like a how a Locale would be used.  A set of values that depend on ....


I'm still learning here... but apparently, the dry air constant isn't really a constant... so I made way to allow for changing the Molecular Mass in the Air by supporting a DryAir class...

https://github.com/robertjliguori/JAirDensity/blob/master/src/main/java/com/gliesian/jairdensity/DryAir.java



However, this still doesn't resolved who and how the values for the volume ratios are set.

-- Robert
 
Robert James Liguori
Author
Ranch Hand
Posts: 587
6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out... thanks for your help.

Determining the DRY AIR GAS CONSTANT
http://robertjliguori.blogspot.com/2017/03/determining-dry-air-gas-constant.html
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a physicist, like I said earlier, but I did major in physics for two years before dropping out. And I notice that whereas none of the inputs to your calculation have more than 7 significant figures, and whereas several of them have only one or two, your final result for the constant has way too many significant figures. The other numbers you quoted for the constant had only about four significant figures, having presumably been calculated by physicists.

For example the first number you calculate, you post as 21.87286272. This should probably be rendered as 21.8729 or 21.873; I'm sure there must be scientific math rules for how many significant digits you should keep but I don't remember them (being a physics dropout).
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you are seeing numerous different values for "dry air gas constant" is that it is a function of the gas that you are talking about.

That number is defined as being equal to the number of grams per mole of gas. So it is different for different gases.

At sea level, air is about 78% nitrogen and 21%oxygen, so if you use those weights you get one value, but the makeup of "air" can change with temperature, humidity, altitude, and a host of other variables, including how close you are to the nearest volcano.

Bottom line is that there is no exact single value that is "correct".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!