• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help with arrays

 
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im completely lost, im supposed to have this output, it is generated by rolling two dice 36000 random times and adding the faces that come up,  1 + 1 = 2 , 2+1 = 3, 3+1 until reaching 12, and count the frequency that each number shows up, and display its percentage.




I can roll one dice so far, i understand that in order to make up the sum i need to add the elements of two arrays, perhaps two arrays of 7 ints , and start looping at 1. but there is two things i cant figure out
first , how to add that second array to the first one to achieve the sum 1 2 3 4, second as it currently stands my program its stuck in the 5 thousands every time it rolls. I need some ideas, or some guidance please.
import java.util.Random;
import java.util.Scanner;



current output:
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are making things too complicated.

With 1 dice, how many possible outcomes do you have? How many "slots" do you need to count how many times each of those outcomes occurs?
With 2 dice, how many possible outcomes do you have? How many "slots" do you need to count how many times each of those outcomes occurs?

What's the relationship between a possible outcome and the slot into which it should be tallied?

Also,
if you want to roll one dice, how many times do you call rollDice()?
if you want to roll two dice, how many times do you call rollDice()?
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The names for your arrays are not representative of what they are used for.  The names die and die2 are misleading and they are probably part of what's causing your confusion.  What value does each element of the array hold? Also, why are you declaring the array to have 7 elements when your die only has 6 faces? What's the extra element for?
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lastly, look at the formula you're using for the percentage. Are you sure it's correct?  Are you sure you're dividing the count by the correct number?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As a side note, why is the total percentage chances, for a single die, add up to 360%? Should the maximum be 100%?

Henry
 
rick pine
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Lastly, look at the formula you're using for the percentage. Are you sure it's correct?  Are you sure you're dividing the count by the correct number?


I have changed that to 1000 instead.  from your questions this is what i gather. If i produce 2 arrays with an idex of 6 i would have a total of 12 values, if i want to roll two dice i should call roll dice twice on the for loop, the names of the arrays beacause it is indexing the numbers that the dice will display, so maybe to diceSide, or diceFace. Also the reason i set the arrays to 7 its because i started the loop at 1, if i set it to 6 and start the loop at i = 0 i get a 0 on the output, and that was the only way i found to stop that from happening.
 
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rick pine wrote:

Junilu Lacar wrote:Lastly, look at the formula you're using for the percentage. Are you sure it's correct?  Are you sure you're dividing the count by the correct number?


I have changed that to 1000 instead.  from your questions this is what i gather. If i produce 2 arrays with an idex of 6 i would have a total of 12 values, if i want to roll two dice i should call roll dice twice on the for loop, the names of the arrays beacause it is indexing the numbers that the dice will display, so maybe to diceSide, or diceFace. Also the reason i set the arrays to 7 its because i started the loop at 1, if i set it to 6 and start the loop at i = 0 i get a 0 on the output, and that was the only way i found to stop that from happening.


I would just create 3 arrays for this....first array is equal to {2,3,4...12}. Second array will hold the counts and third will hold the percentages. As you can realize they will all have a length of 11.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, by thinking you need two arrays you are overthinking and overcomplicating things. List down all the possible outcomes that you want to count. Do you really need to distinguish the result of rolling one dice from the result of rolling the other dice? Aren't you just adding the results of the two rolls and counting how many of that particular total you've gotten so far?

Why don't you take a pair of actual dice and walk through the process yourself? Then you'll understand better what you need to tell the computer to do. Just letting your brain and imagination do it is not always a good idea. Your imagination has a tendency to run wild and come up with all sorts of crazy things. Seeing and walking through the process yourself keeps your imagination in check.

As for your calculations, again your brain is deceiving you. Are you sure it's 1000? If you're wrong 2 out of 2 times, you're wrong 100% of the time. If you're wrong 1 out of 2 times, you're wrong 50% of the time. You *tried* TWO times, right?

If you played baseball and got 25 chances to bat and out of those 25 times at bat, you were able to get at least a base hit 5 times, what's your hit rate? It would be 5 out of 25 TRIES, right? That's 20%.

So, how many times are you TRYING to roll the dice?
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergiu Dobozi wrote:I would just create 3 arrays for this....first array is equal to {2,3,4...12}. Second array will hold the counts and third will hold the percentages. As you can realize they will all have a length of 11.


Sorry, but that's even worse and more complicated than what OP has in mind. Come on guys, you only need ONE array for this! THINK!

If you were to tally this manually, this is one reasonable way to do it:

So, first time I roll a 7, tally one for 7. Next, I roll snake eyes, tally one for 2. Next, I roll an 8. Tally one for 8. Next, I roll another 7. Tally another one for seven. Next, ... Need I go on?
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you're still confused at this point, let me frame this same problem in a different way -- so you at least still have to work your noggin

Imagine we tell someone to sort out a big bag of pingpong balls, each marked with a number from 1 to 10. We ask our guy to count how many balls are marked 1, how many balls are marked 2, and so on to 10. We also ask our guy to report what percentage of all the balls are marked 1, what %tage are marked 2, and so on to 10.

We show our guy a set of 10 boxes that he can use to sort out the balls. The boxes are each labeled with a number, which would be great except that they're numbered from 0 to 9 instead of from 1 to 10. Our guy says, "That's fine, I can manage with these." Here's what the boxes look like when our guy arranges them in a row:

So, our guy now starts taking balls out of the bag, walking up and down the row of boxes, and dropping each ball into a box. In the end he gets all the balls sorted out in their own box with all the other balls with the same mark on it. Now, in Box 0, what mark does each ball have? In Box 1, what mark does each ball have? Box 2? Box 3? ... Box 9?  Can you figure out how our hypothetical guy decided which box he needed to drop each ball based on its mark? Then, because our guy has super memory and was counting all along, he tells us "There are 241 balls in Box 0, that's XXX percent of the total" (because he also knows how many balls there were in all in the bag to start with) and he goes down the line reporting the percentage numbers for each box in turn.

Now, with that in mind, pick up your dice and be *that* guy.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
Now, with that in mind, pick up your dice and be *that* guy.



This almost sounds like a movie quote... and if it is, it is going to irk me for the rest of the day, trying to figure it out...

Henry
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:. . . This almost sounds like a movie quote... and if it is, it is going to irk me for the rest of the day, trying to figure it out...

Henry

I didn't find any movie quotes by searching, but one link with dreadful pick-up lines appeared, which can be relied on to send any woman running.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The actual phrase you'll have probably heard before is "Don't be *that* guy!"  I wanted to put a positive spin on it for this case.
 
Sergiu Dobozi
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you said one array is enough, I made the program with one array. This is what I have so far....I just need to figure out how to format the output to show up better. I can't figure out how to place a percentage marker after each third value.


[Moderator edit: deleted code]
 
Sergiu Dobozi
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I had to delete one line that would have destroyed the program completely.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergiu Dobozi wrote:Since you said one array is enough, I made the program with one array. This is what I have so far...


I had a look at that code before I deleted it. It still was far more complicated than it needs to be.  You need ONE simple array that will hold the tally for each distinct result that you can get.  You don't need a nested/multidimensional array for this because everything besides the tallies can be calculated.
 
Sergiu Dobozi
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Sergiu Dobozi wrote:Since you said one array is enough, I made the program with one array. This is what I have so far...


I had a look at that code before I deleted it. It still was far more complicated than it needs to be.  You need ONE simple array that will hold the tally for each distinct result that you can get.  You don't need a nested/multidimensional array for this because everything besides the tallies can be calculated.


Ah, thanks, now I see it works just as well with a simple array. Sorry, I got carried away with writing the code and forgot I wasn't OP
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rick pine wrote:the names of the arrays beacause it is indexing the numbers that the dice will display, so maybe to diceSide, or diceFace.


A problem clearly stated is a problem half solved —Dorothea Brande

You haven't clearly stated your problem, that's why you're having a hard time figuring out what name you should call your array. What does each element of that array represent? What exactly is that number? When you print it out, what heading do you put those numbers under? That label is what you should call the array.

Also the reason i set the arrays to 7 its because i started the loop at 1, if i set it to 6 and start the loop at i = 0 i get a 0 on the output, and that was the only way i found to stop that from happening.


Free your mind. You are shackling yourself to addressing each element in the array by a specific number. Go back to the example of the guy and the pingpong balls that I posted earlier. *That* guy knew how to make mental adjustments.
 
rick pine
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allright after reading all your questions and comments, i started from scratch and came up with this. I have changed the names of the variables to represent what is happening on the program. that helped with the clarification.
Adressing  percent problem, you are correct if take two guesses and im wrong twice im wrong 100 percent of the time, if im wrong once, im wrong 50percent of the time. So if i roll 10000 / 100 gives me the correct percentage, i figured i should divide totals[i]/360 because of the 36000. There is no need for two arrays, just one set to the max number of possible combinations from two dice : 6 + 6 = 12 however i still had to set the array to 13 and start the roll loop at 1, if i dont now i get an error when running. Again im sure there is a better (proper) way of fixing it. perhaps my problem originates from setting all indexes to 0.
Let me know what you think of this ,(be gentle ).




 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks good to me.  One thing I'd do is make the number of iterations a constant.  "36000" and "360" are called magic numbers, because it's not clear what they mean.  Try this:

I think that works!  But you get the idea.
 
rick pine
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes the final iterations variable works, i will adopt that change. Thanks for all the questions you gave me, they helped me think through the problem.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a good start... (was that gentle enough? )

There's a "Java way" to write for loops. The way you wrote it is more like "The Pascal Way" of writing for loops. I programmed in Pascal once upon a time.

Since Java arrays use 0-based indices, you should get used to that and adjust your mindset.  The Java way to write that for loop is:

Note that we initialize the loop variable roll to 0, not 1, hence, the loop termination expression uses <, not <=

You should get used to reading and writing this idiom because that's the normal convention.



 
Saloon Keeper
Posts: 5533
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:(...) Now, with that in mind, pick up your dice and be *that* guy.


Hmm that guy with this big bag is doing a lot of work... especially when you realize that he has to put every drawn ball back into the big bag, otherwise the example is not exactly that of the two dice rolling. So, are you SURE you want to be that guy?    

On a more serious note: with java 8 and its streams and collectors, you can get the same results without using any array. Anyone willing to take up that glove?
 
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it working, but its not an ideal way.  I had to do this line to stop it adding null to the start of the strings.

Here is the whole method:
 
rick pine
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i usually write that way because it causes less confusion on my part, i know that it will start at 1 and will end at x. as opposed to starting at 0 and going until x < 0 . but i understand that i need to adapt to standards.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at this code that you wrote:

The for-loop on lines 8-10 is unnecessary. You don't have to explicitly initialize an int[] elements to 0 because Java will already do that for you.  If you created a new Object[10], all its elements would be null, a new boolean[10] array would have all its elements initialized to false. An int[11] will have all its element initialized to 0.  So, you can get rid of lines 8-10.  Don't write any more code than you have to.

Speaking of not writing more code than you have to, you don't really need those face1 and face2 variables.  You could have just written this:

So, you can get rid of lines 4, 5, 12, and 13

Now, you only have 11 possible outcomes with 2 die/dice rolls, that is, 2 to 12. That's 11 numbers. I told you don't get shackled to those exact numbers. I told you to be *that* guy who can adjust his mindset so that balls marked "1" will go into the box labeled "Box 0", balls marked "2" go into the box labeled "Box 1", ... , balls marked "10" go into the box labeled "Box 9".  If you took the time to write that down, you'd see a pattern

Do you see a pattern? It's really very simple math, right? Similarly, if you write down your array indices and the possible roll values you get, you can easily find a mathematical relationship between the Roll value and the index in the totals array that you need to update:

Do you see the pattern there? There's definitely a consistent adjustment you can make to the roll value to get to the proper totals[] index that you need to increment, right? The "adjustment" you need to make is often called the "offset," so I would define a constant called that and use it to calculate the index of the element in the totals array that needs to be updated. You can figure out the math for that, right?  

Also, I find the name "ITERATIONS" a little too formal "computerese". Try to use names that are in the problem domain or something that makes your code sound more conversational when you read it. It makes the reader more comfortable and not feel likes he's reading a technical journal or something. Granted, OFFSET isn't something that you often hear in a casino or a back alley game of craps either but I can't think of a good, everyday alternative for it. Can you? But you can certainly do better than ITERATIONS.

And what about the name totals?  Why name the array "totals" when your table heading for those numbers is "Frequency"?  Wouldn't your code make more sense if you put a number taken out of an array called frequency under the "Frequency" column?

Finally, you can also define constants in terms of other constants. You just have to declare a constant before any other constants that use it. So, in the code below, you have to declare TRIES before you use it to calculate the value for PERCENT_TRIES.

Do you see how better names can improve the readability of your code?
 
Piet Souris
Saloon Keeper
Posts: 5533
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to disagree about using the variable 'ROLL_VALUES'. Why introduce that extra complication? In the old situation we are wasting two array elements, but with the big bonus of simplicity. A price I am  willing to pay..

Suppose that we now have three or more dice, and that they are of a special kind: they all contain 6 random numbers. Now, it becomes much harder to determine a suitable mapping function from output to array-index. In the old system you don't have this complexity, at the expense of some wasted elements.

You wouldn't have these problems when using a Map<Integer, Integer>.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And finally, one last trick to make your code easier to read. Judging from the way you wrote another method called rollDice(), this should make sense to you. You probably realize by now that putting all your code in main() is a pain (we even have a wiki page to prove it!) and putting little things like calculating one roll of the dice into its own little method that you can call as many times as you like is a pretty convenient technique.  One other thing it does is that it helps make your code clearer, right?

So, to keep your code more readable rather than more cryptic, you take the formula and "hide" it inside its own little method and use the more user-friendly method name in your main program code.

Now, once you've figure out the formula for adjusting the roll value so that you are incrementing the correct element of the frequency array, you might look at that line of code and think that it's pretty gnarly and hard to understand. You'd be correct in thinking that. You can do the same thing you did with rollDice() to fix that.

That's it. Now your gnarly formula is hidden inside its own method and your main code is more user-friendly and expressive.
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I have to disagree about using the variable 'ROLL_VALUES'. Why introduce that extra complication? In the old situation we are wasting two array elements, but with the big bonus of simplicity. A price I am  willing to pay..

Suppose that we now have three or more dice, and that they are of a special kind: they all contain 6 random numbers. Now, it becomes much harder to determine a suitable mapping function from output to array-index. In the old system you don't have this complexity, at the expense of some wasted elements.

You wouldn't have these problems when using a Map<Integer, Integer>.


That's fine, but I didn't take my psychic pills today so I wasn't able to foresee the need to handle more funky requirements other than what OP said he needed to handle.  

You have a point in saying that adding another constant like ROLL_VALUES may not be very useful, especially since it's only used in one place. I wouldn't really mind either way if that constant was there or not. I just added it to give an example of another name that reveals intent. It might not be the best name so feel free to suggest a different name if you have one.

As for using a Map<Integer, Integer> for the frequency counts, feel free to go ahead and discuss that implementation as well -- it might be interesting to see how much it would take to transform this base code so that it can handle more complicated scenarios. Right now, I'm just trying to stick as close as possible to what OP was actually asked for in this exercise.
 
Piet Souris
Saloon Keeper
Posts: 5533
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that OP's (and your!) method are perfectly suitable for the job at hand, and so I'm reluctant to talk about other ways. Quieta non movere !
 
rick pine
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really appreciate your ways to improve my code, i will keep them for personal use and to improve my skills. But if i were to turn this in with your ideas i probably would get zero   , it would look too good for a novice.
My professor would be like  
I did remove the initialized arrays, i took for granted that java already sets primitives to their defaults on arrays, thank you for bringing that up.
Im getting carried away with the emoticons  
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rick pine wrote:I really appreciate your ways to improve my code, i will keep them for personal use and to improve my skills. But if i were to turn this in with your ideas i probably would get zero   , it would look too good for a novice.


Good deal. Since you already solved the problem yourself, the hope in showing you other ways to do it was that you can start getting some sense of code "aesthetics" -- I always find it a pleasure to read other people's code that's elegant, well-structured, and well thought-out. My hope is that by showing what I learned from others, newbies like yourself can also develop the same kind of appreciation I have for "beautiful" code.
 
rick pine
Ranch Hand
Posts: 90
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you mean by "beautiful code" its a goal of mine to get to that point, you have been very helpful and seem to have tons of experience, if you don't mind me asking, in your opinion what are some good java learning resources on the web, i can google but the results are overwhelming. I want to learn more than what is thought in the classroom.  
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honestly, and not to be a shameless self-promoter, most of the things I learned about Java were from here, at JavaRanch. It started when I was studying for what was then called the Sun Certified Java 2 Programmer exam. Yeah, that's how long ago it was, Java was still in its 2nd iteration. Studying for the certification exam helped me become somewhat of a "mechanic", getting to know what was under the hood, the nuts and bolts, so to speak.

The next step for me was to become a "tradesman/journeyman". My sources of information back in that phase included websites like javaworld.com, drdobbs.com, ibm.com/developerworks, and later on, infoq.com

Most of all, I bought a lot of books on software development. I have about 5 shelves of books on just about every aspect of software development, three of which contain books that I still refer to often. The really active books, the ones I refer to the most, sit on one and a half bookshelves just a few steps away from my desk (I'm a remote worker so I work from home). The least active two bookshelves are in my basement, with books on Pascal/Delphi, RPG, COBOL, and very old Java books, just in case I want to reminisce on the old days. I actually bought a Peter Grogono book on Pascal Programming from Amazon recently because I wanted to refer back to some of the theories I learned at the very beginning of my journey in programming, many, many years ago.

The most active books I have now are about agile software development, test-driven development, design, and thinking/learning. If I were to list the top 10 books I use right now, it would be this:

1. Understanding the Four Rules of Simple Design by Corey Haines (ebook)
2. Clean Code by Robert Martin
3. Effective Java, 2nd Edition by Joshua Bloch
4. Working Effectively with Legacy Code by Michael Feathers
5. Pragmatic Thinking and Learning: Refactor Your Wetware by Andrew Hunt
6. Growing Object-Oriented Software, Guided by Tests by Nat Pryce and Steve Freeman
7. Refactoring to Patterns by Joshua Kerievsky
8. Refactoring: Improving the Design of Existing Code by Martin Fowler
9. Software Architecture for Developers by Simon Brown
10. Agile Software Development: Principles, Patterns, and Practices by Robert Martin
10. The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt and David Thomas
10. Java 8 in Action ...
10. Java Testing with Spock ...
10. Extreme Programming Explained: Embrace Change by Kent Beck
10. The (mostly) complete series on "Extreme Programming" after Beck's book ...
10. ... and many more ...

I hope you get what I mean

So, there's not really one place I go, although I've been spending a lot of time here lately. The one thing I can say that has helped me improve the most is a lot of practice. The first book on that list is not a random choice. It really is the book I've been reading and re-reading the most lately. It's a very short 70-page ebook that has a single programming problem in it: Conway's Game of Life. As short as the book is, it's chockfull of the kind of things I've been trying to share here on this site. It also talks about "Perfect Practice." Haines is one of four people who started Code Retreat back in 2009, just a couple of hours away from where I live in Ohio. Code Retreat is all about "Perfect Practice" -- as every music teacher ever has said to their students, "Practice only makes habit. Only Perfect Practice makes perfect."

One of Haines' bits when he makes an intro at Code Retreats that he facilitates goes something like this (paraphrasing from what I remember):

There's a level of coding that you do on a daily basis. This is the level where you're comfortable. You can get things done at this level. But you tend to cut corners because you need to get things done because that's what you're paid to do. Then there's the level of coding that we consider to be perfect. And there's always a gap between our daily level of coding and that perfect level of coding. That gap is important because if we tried to code perfectly all the time, we wouldn't get a whole lot of things done. But that gap is also a measure of how much we suck. Code Retreat is about narrowing that gap, just a little bit. We practice writing perfect code, without the pressure of getting anything done. If all you do at Code Retreat is write one line of perfect code, one method that's perfect, then you've learned something.

And when you go back to work on Monday, you just might be able to raise your daily level of coding that one little bit, so your code is a little cleaner, a little simpler, a little more expressive, and is covered by some better tests.



Finally, I learned a lot from practicing Aikido. That may seem to be coming out of left field but it has to do with mindset and outlook. I'll just leave it at that because this is already too long and you don't want me to get started talking about Aikido. If you really want to read what I have to say about that (ya weirdo! ), you can go to the Agile forum on this site and dig up some of my past posts there, they usually have some kind of mention of Aikido and how I relate it to Agile development.

Good luck.
 
Sheriff
Posts: 8890
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:top 10 books I use right now, it would be this

I already bought and read 3 books from your recommended list: '2', '3', '10.2' - the biggest impact to me did the Clean Code at the moment. Being a quite pedantic person, I really liked authors ideas taught in the book. How author pays attention and highlights importance of the smallest details. Aligned to this topic I want to share one quote from the book which touched me a lot:

Uncle Bob's Clean Code wrote:When people look under the hood, we want them to be impressed with the neatness, consistency, and attention to detail that they perceive.
We want them to be struct by the orderliness. We want their eyebrows to rise as they scroll through the modules.
We want them to perceive that professionals have been at work.



And just now bought 4th one:

1. Understanding the Four Rules of Simple Design by Corey Haines

Looking to how many times this book have been quoted by Junilu, and the fact that it appears as a first in book in his list, I can't wait to start reading it. Will start right now actually.

Thank you for the list
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic