r/learnjava • u/Primary-Reputation-2 • Jan 18 '23
Learning java at 44
I've decided to learn java so I'm doing a short free course on udacity. I'm quite enjoying but I got stuck on a very basic bit of code and need some help. I'm generating random numbers for dice but now need to change the number of sides from 6 to 8 using parameters. I gone over my notes but still not quite sure where I've gone wrong. This course doesn't give me the answer anywhere so I can't see where I've gone wrong. I know (int sides) are the parameters I have to use but not sure how. Some guidance please!!
Public int roll Dice (int sides){ randomNumber=Math.random(); randomNumber=randomNumber*6; randomNumber=randomNumber+1; int randomInt=(int)randomNumber;
return randomInt; }
16
u/wynand1004 Jan 18 '23
If you look at your code you have the number 6 in it. But now you want that to change based on the parameter sides. So, change the 6 to sides.
Good luck with your studies.
9
u/Primary-Reputation-2 Jan 18 '23
Ah of course. Thanks very much for your guidance
4
u/idhanjal Jan 18 '23
One thing to remember. Avoid using hard coded (like the 6 here in your code) anywhere. They always lead to unexplained behavior.
3
u/Pedantic_Phoenix Jan 18 '23
The wizards of the coast fiasco really did reach far and wide /s
2
u/Primary-Reputation-2 Jan 18 '23
Sorry, I don't understand what you're referring to.
2
u/Pedantic_Phoenix Jan 18 '23
Dungeons and Dragons owner company, they have trouble in this period, you asking about dice rolls reminded me of it. It's a very weird connection, don't mind me x)
9
u/_Atomfinger_ Jan 18 '23
Look at this line:
randomNumber=randomNumber*6;You're not using the sides parameter.
1
u/Housy5 Jan 18 '23
Is there a reason I see a lot of people use Math.random instead of the Random class?
1
u/Primary-Reputation-2 Jan 18 '23
What I learnt today was that random was in the math class. I might be wrong but I'm sure that's what they said
1
u/desrtfx Jan 18 '23
It is absolutely correct that there is
Math.random().Yet, it is also correct that there is a
Randomclass that has several methods for generating random numbers. There is.nextInt(int range)that generates a random integer between 0 inclusive and range exclusive, there is.nextDouble()which does exactly whatMath.random()does, as the latter is in fact using the former, and there are several other methods in this class.Have a look
java.util.RandomclassStrange fact: there is also an undocumented
.nextInt(int min, int max)method in newer Java versions that produces a random integer between min inclusive and max exclusive.1
u/desrtfx Jan 18 '23
People learn
Math.random()earlier as it is easier to use.Using the
Randomclass is a bit trickier especially in the early beginning when creating objects has not been introduced.
4
u/AutoModerator Jan 18 '23
It seems that you are looking for resources for learning Java.
In our sidebar ("About" on mobile), we have a section "Free Tutorials" where we list the most commonly recommended courses.
To make it easier for you, the recommendations are posted right here:
MOOC Java Programming from the University of Helsinki
accompanying site CaveOfProgramming
accompanying site NewThinkTank
Hyperskill is a fairly new resource from Jetbrains (the maker of IntelliJ)
Also, don't forget to look at:
If you are looking for learning resources for Data Structures and Algorithms, look into:
"Algorithms" by Robert Sedgewick and Kevin Wayne - Princeton University
Coursera course:
Coursebook
Your post remains visible. There is nothing you need to do.
I am a bot and this message was triggered by keywords like "learn", "learning", "course" in the title of your post.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/hugthemachines Jan 19 '23
I can see you already got good answers, I would just like to add something that can be good to know. There is a rule (of thumb) that says "Don't use magic numbers" and that means, don't add a number in your code that is without any kind of explanation.
Let's say a stick is 2 inches, instead of just putting a 2 in the code, we should have a variable with a good name. Like stickLengthInches = 2 or something fairly similar. Now people (or you in the future) who read the code can get a hint on what that number represents.
This may be something to think about on your ongoing code adventure.
2
Jan 23 '23
This is not related to your problem as I see others have helped you, but a quick suggestion and guidance.
You should try to look up at https://www.javatpoint.com/ as there are many guides to what you need.
All java complete guide, data structures (usually written on C but it can be translated to java easily). Java programs and many interview java questions that companies usually ask + java programs for interview (algorithms, doubly linked lists and how to use nodes, tree search, sorting,etc.).
I suggest you learn everything from the beginning and go further but skip javaFX, java AWT / Swing because for the beginner this is not needed and almost no interviewer at companies will ask questions about it for the starting positions or junior. They most care about algorithms, data structures, Big O and such.
Another note is to download the book: ''Cracking the coding interview'' and start doing it when you get confident in your skills. The book holds many questions and possible problems that big companies use. Most answers are written in Java but if some are not, you can always download the solutions on their github page for any language desired.
•
u/AutoModerator Jan 18 '23
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full - best also formatted as code block
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.