Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

int variable highScorePosition question  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I basically know how this code works - but don't know why or how variable highScorePosition is used assigning it to method calculateHighScorePosition. I get that its going to be 1 thru 4 when the code runs but why is it necessary in this ==> int highScorePosition = calculateHighScorePosition(1500) ect….
my guess is that it has to do with the return in the if else logic that then assigns highScorePosition  1 thru 4

 
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if playerScore is exactly 1000, or exactly 500?
Not sure what your question is about this code.
 
Jasper Jax
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is that I don't understand this :

       int highScorePosition = calculateHighScorePosition(1500);
       displayHighScorePosition("Tim", highScorePosition);


why dose highScorePosition have to be after the int - could I not use another variable. Whats throwing me off is the two highScorePosition in the above lines


(See rest of code in my first post)
 
Sheriff
Posts: 24089
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Are you asking why the variable in that code fragment is named highScorePosition?

Well, you could name it something else. Like:



But programmers should write code which can be understood by other programmers. That includes giving a variable a name which describes the purpose it's being used for. So a variable used to store the high score position would be named "highScorePosition" rather than "ratPoison".
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Functionally the same as this:
You can declare a variable with its type and initialize it in one statement. There can only be one declaration though, in your case, it's the one with "int" in front of it. Also, once declared, you can use it repeatedly, but not with the "int" in front of it. That would be a duplicate declaration which is not permitted.

Is this what you're looking for?
 
Sheriff
Posts: 12994
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jasper Jax wrote:My question is that I don't understand this :

       int highScorePosition = calculateHighScorePosition(1500);
       displayHighScorePosition("Tim", highScorePosition);

why dose highScorePosition have to be after the int - could I not use another variable. Whats throwing me off is the two highScorePosition in the above lines


I think the way you're asking your question is also throwing us off. We're not sure we understand where your confusion lies.

Let me walk you through what those two lines of code are doing. This is basically a repeat of Carey's answer, said in a different way.

1: int highScorePosition = calculateHighScorePosition(1500);

The bolded part declares an int variable named highScorePosition.

2: int highScorePosition = calculateHighScorePosition(1500);

This other half calls the calculateHighScorePosition() method with an argument of 1500. The value returned by the call is then assigned to the variable highScorePosition. In this case, the value assigned is 1.

3: displayHighScorePosition("Tim", highScorePosition);

This calls the displayHighScorePosition() method. The second argument is the same variable you declared and assigned a value to in the previous line. That means the values passed to the displayHighScorePosition() method for this call will be "Tim" and 1.

Does this clear things up? What isn't clear to us, I think, is why you'd ask "could I not use another variable?" - I'm not really sure what you're thinking that would make you ask that question.
 
Junilu Lacar
Sheriff
Posts: 12994
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you gave is quite verbose. It could be made more succinct and readable like this:

The above code still has the bug that Carey alluded to before though. See what happens when you make these changes to the above code:
 
Jasper Jax
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I understand now - I was confused on how highScorePosition variable was getting its value. The question really wasn't worded right. Now I understand, thanks you everyone who chimed in, and yes I've fixed the bug where if the value is 1000 or 500 ect.. by adding >= or <= to the code. I am new at the "return" function and the fact that a variable could be assigned to the results of a method.

Thank you - you guys are AWSOME!!!  
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Jasper Jax
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jasper Jax
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - as per Junilu Lacar, the code is quite verbose, thanks for the example. I'll see if I can clean that up a bit ; )
 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just to help you learn.

Here is another way to do this.
I removed static methods and encapsulated them into an object. I added in a Map and a loop using Java 8 lambda.

 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you create an immutable Map in Java9+? Look at this (overloaded) method.
 
Master Rancher
Posts: 3887
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering there is an ofEntries method that seem quite nice, all those (9!) overloaded of methods seem remarkably redundant.
Am I missing something obvious?
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They thought that combining multiple entries into an array with the ... operator runs too slowly; it is much faster to let the compiler count the number of entries, pick the overloading, and have an un‑overridden method to execute at runtime. Hence the apparently excessive overloading.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot write of(K/V... entries) and have a method take alternating Ks and Vs; for argument counts outwith the range 0...10, you need to use Map.Entry... for the parameter type.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!