• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Purpose of variables?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm confused by the variables smallest and average. What is there purpose? Why is there a method assigned to them as a value? All information on these mysterious variables is appreciated.
 
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George, welcome to coderanch. We wish you the best in your quest to learn java

Where did you get this program from ? Please QuoteYourSources <-- It's a link
Next, what happens when you run this program ?
I see a variable called smallest in line #9 and another one at line 16, which one are you confused about ?

If you haven't done it already, I suggest having a look at oracle's tutorial for java. Specifically about methods: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
and this: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

Do let us know if you still have any doubts about the code above, we would love to discuss further !
 
George Foreman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:
Where did you get this program from ? Please QuoteYourSources <-- It's a link


The program came from chegg. You need a subscription to access it. Should I still post the full link?


salvin francis wrote:
Next, what happens when you run this program ?




Note the added comments.




salvin francis wrote:
If you haven't done it already, I suggest having a look at oracle's tutorial for java. Specifically about methods: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
and this: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html



Will do.
 
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

Please don't include questions in the code as comments; they make the lines too long and comments which fall off the edge of the page might be ignored.

Is that the recommended answer? Was the code formatted so badly, or is that how you copied it?
You are right to query why they are using floats, which is an error.
Using 1 2 3 is simply a way to testing your code. It isn't adequate testing. You should test the code with the same values in a different order (3 2 1), with negative values, with fractional values, with values well away from 0 and values very close to 0. Try the following values for testing:-The pitfall with NaN is one they aren't anticipating, but using floats should produce incorrect results for all the above except No 5.

I shall let you guess why the word “smallest“ appears thrice with different meanings. Tell us what you think and we'll see whether we agree.The above is what I think is the correct solution, because you should use library code whenever possible. If you look at the documentation, you will find that method will handle NaN consistently.
 
George Foreman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Is that the recommended answer?



Is what the recommended answer?

Campbell Ritchie wrote:
Was the code formatted so badly, or is that how you copied it?



Ill take partial blame for that.

Campbell Ritchie wrote:
You are right to query why they are using floats, which is an error.


Why does it still run?

Campbell Ritchie wrote:
I shall let you guess why the word “smallest“ appears thrice with different meanings. Tell us what you think and we'll see whether we agree.


Well, on line 16 value1,2,3 is being passed to the method parameters on line 22. I'm not sure why on line 16 there is a variable declaration. Line 23 is necessary for the program to run perhaps because they have two different scopes?

Campbell Ritchie wrote:
The above is what I think is the correct solution, because you should use library code whenever possible. If you look at the documentation, you will find that method will handle NaN consistently.



I'll try to keep that in mind... thank you for all of your help.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

George Foreman wrote:. . . Is what the recommended answer? . . .

What you showed us.

. . . they have two different scopes?. . . .

Yes. Not only do the two variables have different scopes, they count as different variables. They only happen to have the same name.
Two down, one to go.

thank you . . .

That's a pleasure
 
George Foreman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Two down, one to go.



At the risk of being redundant, so the declaration on line 16 has nothing to do with the declaration on line 24 because of different variable scopes making them different variables. Line 24 declares smallest and lines 26, 28, & 30 initialize smallest, which hints why the errors if line 24 is omitted.

Correct me if I use the wrong terminology. I understand that values 1, 2, 3 are initialized by scanner and value 1, 2, 3 are then passed to parameters on line 22 to a, b, c. When line 16 is executed do value 1, 2, 3 become whatever they were previously initialized on lines 13-15? Is this just sloppy coding because values 1, 2, 3 could just be written as a, b, c from the beginning... but this would neglect the average method, so is it always a good idea to use some third party variables to pass into the parameters so they can be reusable?

I still don't understand the purpose of initializing a variable with a method and it's parameters... or even how that runs. Is that sloppy coding?
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

George Foreman wrote:. . . the declaration on line 16 has nothing to do with the declaration on line 24 because of different variable scopes making them different variables. Line 24 declares smallest and lines 26, 28, & 30 initialize smallest, which hints why the errors if line 24 is omitted.

Yesyesyesyesyes

Correct me if I use the wrong terminology. I understand that values 1, 2, 3 are initialized by scanner . . .

It appears they are read from the keyboard input: you enter 1 2 3 and the Scanner interprets that as 1.0f 2.0f 3.0f because it is using the float method.
By the way: try the entries all on the same line some time; the code should still work the sam.

1, 2, 3 could just be written as a, b, c . . .

But a b c and x y z are only in scope during those methods.

I still don't understand the purpose of initializing a variable with a method and it's parameters... or even how that runs. Is that sloppy coding?

Do you mean something like this?That means you are passing information to the method, letting it calculte something and pass the result back to a variable. That is normal coding.

Have you solved the problem about the two occurrences of the word “smallest” in line 16? That was my “one to go”.
 
George Foreman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Have you solved the problem about the two occurrences of the word,"smallest" in line 16? That was my one to go



Well one is a variable and the other is a method name.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. There is a rule somewhere in the Java® Language Specification saying that an identifier followed by (...) is automatically interpreted as a method name (or constructor name) rather than a variable.
 
Ranch Hand
Posts: 125
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naming conventions are important.
Maybe that is why you get confused.

line 22, is your method declaration. Try and give it a proper name.
A method, does something with your variables. line 22-35 inside the method body

Same for the other method.


Variables are atomic propositions in your code.
They are the building blocks for your program.

I would recommend you studying Discrete Mathematics to understand the language, behind programming with a programming language.

Try write in plain english what the program you are showing.

From your post I can understand that you are not familiar with Java terminology.
Doing these tutorials and learning how use terminology to translate your English algorithm could help.
https://docs.oracle.com/javase/tutorial/
 
salvin francis
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi George, I see that Campbell has answered all of your questions, I suggest posting your final code here so that it helps others. Personally, if the question didn't mention it, I would have suggested using varargs for both your methods. This would make your methods like this:

Also if you are inclined, there's a way to solve this using streams, but if it's only 3 numbers, I'll leave it to Math.min as Campbell suggested.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.min works nicely for a few numbers with a predefined count; I would hate to use it with varargs. Of course, the Stream technique you are suggesting gives a very elegant two‑line solution.

[addition]There is a potential pitfall with varargs. One we often forget.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

George Foreman wrote:. . . .

I presume there should be a c somewhere in that line.
Please test that code with three numbers two of them the same. I don't think it can handle all three of the following inputs:-
 
Master Rancher
Posts: 3719
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Of course, the Stream technique you are suggesting gives a very elegant two‑line solution.


Not sure why a second line would be required.
 
salvin francis
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...[addition]There is a potential pitfall with varargs. One we often forget.


Good catch !! In this case, we do need at least 1 argument and hence my method definition is not entirely correct.

Mike Simmons wrote:...Not sure why a second line would be required.


maybe Campbell is referring to two lines but one statement.
 
George Foreman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote: I suggest posting your final code here so that it helps others...



This should look a little prettier.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:. . . Good catch !! . . .

Thank you. Stephan taught me about that pitfall.


Mike Simmons wrote:...Not sure why a second line would be required.


maybe Campbell is referring to two lines but one statement.

Yes; I am used to indenting Stream statements so all the dot operators align vertically.

If you use varargs, you would get a method rather like this:-Actually, since average() returns an OptionalXXX, you can use one of its methods to return a double regardless.
 
Fire me boy! Cool, soothing, shameless self promotion:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic