So, youíre stuck on a Java programming homework assignment? Youíve come to the right place to get help, but we arenít here to do your homework for you. If you follow these guidelines, youíll probably be able to at least get started by yourself, and when you get stuck, youíll know the best way to ask a question about it.
Step 0- Calm down.
Do: Get up, go on a walk. If youíve been staring at the same screen for 12 hours, your brain has turned to mush and you might as well be looking at Klingon. You wonít get anything done.
Donít: Become frustrated and copy and paste random code you find on the internet, or try adding or removing lines at random just to see what happens without understanding any of it. Thatís only going to make things much worse.
Do: Take breaks. Iím not saying check facebook every 5 minutes or play a round of solitaire every time you save. Work, but get away from the computer every once in a while! Work for 45 minutes, then take a 5 minute stroll around the house.
Example: Iíve been hacking away at this code for a few hours, and nothing seems to be sticking. So, I take a walk around the block, maybe eat something other than coffee and diet soda, and come back in an hour or two with fresh eyes. I might have even come up with a solution while I was gone (most of my best ideas happen in the shower).
Step 1- Read.
Do: Read the assignment carefully. Maybe highlight or underline the important parts. Read it again, until you understand it.
Donít: Skim the assignment and start writing code right away. Youíre going to miss a crucial part of the assignment, or a hint hidden in the text of the assignment.
Donít: Give the assignment a quick read-through and then say ďI donít even know what this is asking!Ē Read it again, more slowly. You might have to write down some of it in your own words.
Example: Before I even turn my computer on, Iím going to sit down and read through the assignment- CAREFULLY. Iím going to read it again, this time circling anything I think is a separate piece (see step 3). Letís say my assignment is to print the square root of a value input by the user. Iím going to circle any hints the assignment contains, and I might underline things like ďuser inputĒ as well as the format in which the output should be displayed.
Step 2: Before you write code, write it out in English.
Do: Ask yourself, how would you do this without a computer? How would you do it with just a piece of paper and a pencil?
Do: Pretend you have a really dumb friend who has no idea how to accomplish the goal. Write out instructions that he could follow to get the job done (remember, heís really dumb, so the instructions must be extremely simple- if one of the instructions can be broken up into two simpler instructions, do it). Congratulations, youíve just written an algorithm!
Donít: Start writing code before you really understand what the code is supposed to do.
Example: How would I tell somebody the square root of a number she gave me? Iíd ask her for a number, then write it down, then take the square root, then tell her the answer. Seems simple enough. Letís write instructions for my dumb friend:
1- Ask the person ďCan you give me a number?Ē
2- Write that number down.
3- Label that number ďnumberFromPersonĒ so you can refer to it more easily.
4- Take the square root of numberFromPerson, and write that number down.
5- Label the new number squareRootOfNumber.
6- Read squareRootOfNumber to the user.
These instructions might seem trivial, but this process comes in handy for more complicated problems or if you get stuck. Keep in mind that step 3 above might be broken down into many more steps.
Step 3: Break the problem down into smaller steps.
Do: Refer to what you wrote down from Step 2, and convert each dumb-friend-instruction into code. If you donít know how to convert it to code, the instruction probably isnít simple enough. Break it down further.
Do: Think about the different parts of the program. Only focus on one at a time, and only move on to the next step when the current step works perfectly by itself.
Do: Take each one of those steps and write a separate mini-program that does JUST THAT STEP.
Donít: Look at the entire assignment and say ďI donít even know where to start!Ē
Example: Since my assignment involves printing the square root of an input number, Iíve got at least three pieces: getting user input and saving it to a variable, squaring the value in a variable, and outputting a value in a particular format. So I know I should start by writing three tiny programs: one that ONLY takes user input; one that ONLY squares a value; and one that ONLY outputs a value. Each one of those steps might be further broken down into even smaller steps.
Step 4: Start small. Smaller than you want to. Smaller than that.
Do: Take the different pieces from Step 3 one at a time. Each one can probably be broken down into even smaller pieces. Do the next smallest step- it should be less than 2 or 3 lines, maybe even less than one line. Test it.
Do: Test. Debug. If something doesnít work, nowís the time to catch it. Add print statements to figure out whatís going on in your program.
Do: Think about the very next, smallest piece you know you have to do. Donít know where to start? At least write the barebones class and main method declaration. If you know you need a method that takes a parameter and returns a value, start by writing the method declaration.
Donít: Write the entire assignment and THEN try to compile it. Instead, write your program a single line at a time. Test that line. It might feel slow and tedious, but itís much easier to deal with a single error than it is to wait until you have 99 problems (and a NPE ainít one).
Example: Iíve broken down my square root assignment into three steps, so Iím going to focus on just getting the user input.. but now what? Okay, I remember Step 0 and I calm down. Whatís the very next thing I have to do? Oh! My IDE isnít even open! Thatís the very next, smallest thing I know I have to do. Then I know I have to create a class file, and create a main method. Iíve written a few lines by now, so even though it doesnít do anything, does it compile? Okay, now how do I go about getting user inputÖ
Step 5: Combine the steps.
Do: Only think about combining the steps after you have all of the steps working independently. This will also help if you get stuck on a certain step (more on that later).
Do: After you have each of the little pieces working by themselves (and tested), then combine them. Take the little programs you wrote and convert each into a separate method of a single program. Each program might be multiple methods, and you might have to write more methods to help ďglueĒ the separate programs together.
Donít: Copy and paste everything into one file and hope for the best. Instead, test two steps to make sure theyíre working separately. Now try to add the second step to the first step- slowly. Test it out. Then add the third step, etc.
Example: By now I have written three programs- input, square root, and output. My input program could be converted into a method that prompts the user (again, prompting the user could be its own separate method called by the input method) and returns the value input by the user. The square root program turns into a function that takes a single parameter and returns the square root of that parameter. Finally, the output program could be a method that takes a single parameter and displays it somehow.
I combine these programs by first testing them all- if they work, I can combine the input and the square root function. Iím going to need a third function that links them together- for something this simple, the main method might work. So from the main method, I call the input method and store the value it returns into a variable. Now I can pass that variable into the square root function, and store the value it returns into another variable. When Iím sure that works, I can try adding the output function by passing the variable into it. If it doesn't work, I go back to testing the step that's causing the trouble- by itself.
This also makes it easier to change a single piece of the program later- say you want to display the output in a different format, or in a GUI- now you only have to change a single method! Later, you might want to display the fourth root instead of the square root- again, just a single method changes!
But what if I get stuck?
If you follow those guidelines, you should be well on your way to writing your program. But, every programmer hits technical snags where our expectations donít meet our results- and thatís what these forums are for.
Step 0- Follow the steps above.
Did you carefully read the assignment? Did you write out the instructions for your dumb friend? Did you split each step up into its own program? If not, do that now.
Step 1: Get caught up.
Do: Go back and read everything carefully. Read your book and the notes you took in class.
Do: Take a look at prior assignments youíve done. Is there anything in them you can use? If you skipped any, do them now.
Don't: Copy and paste old assignments to build new assignments. You can use old assignments as reminders, but copying and pasting is just going to lead to problems.
Donít: Take zero notes or wait until the last minute.
Donít: Complain about your deadline to other people. There are thousands of posts here, and the fact that you didnít manage your time does not mean that you get precedence over them.
Example: Argh, Iím stuck because I donít remember how to write a method declaration! So I go back to an old assignment and remind myself of what it looks like. Then I get confused on the difference between an int and a double, so I go through my notes and look at where the teacher explained them.
Step 2- Debug debug debug.
Do: Learn how to use your IDEís debugger. At the very least, get into the practice of adding a ton of print statements, so you understand whatís going on.
Donít: Try solving a problem before you know what the problem actually is.
Example: Iíve got my method that prompts the user and returns the input. But the method seems to be returning the prompt message. So I debug the program and discover that Iím accidentally returning the wrong variable. Whoops!
Step 3- Consult the tutorials and API.
Do: Always have the API open in a firefox tab. Itís your best friend now.
Do: Consult the Java tutorials before coming to anybody else. There are tutorials for everything, especially beginner stuff. Read through them carefully. Try out all of the examples in them.
Donít: Give up and dump your code to a forum after not finding anything in your notes.
Donít: Blame the teacher. Plenty of people learn on their own without the benefit of a teacher, classmates, or a classroom. Saying things like ďmy teacher never explained this!Ē is just a waste of time. The answer is out there- find it.
Example: I know I have to take the square root of a value, but I donít remember how to do that. So I consult the API, find the Math class which sounds promising, and voila! If Iím still not sure, Iíll google something like ďJava Math square root tutorialĒ.
Step 4: Time to make a forum post.
Do: Focus on the smallest step youíre stuck on, which should have its own standalone program for testing (also known as an SSCCE).
Do: Be specific. Where are you stuck? What have you tried? Whatís happening? If youíre getting an Exception, show the line thatís throwing the Exception and copy and paste the entire stack trace. If youíre getting unexpected behavior, describe what you expected to happen and whatís happening instead.
Donít: Copy and paste your homework assignment description without a specific question. Thatís academic dishonesty, and it could get you kicked out of school. We arenít going to help you cheat.
Donít: Copy and paste all of your code. You should ONLY post the small step youíre stuck on (which shouldnít be hard if you followed the guidelines). We have thousands of posts here, and we donít have time to wade through extra code that has nothing to do with your actual question.
Do: Give a general idea of the main goal, just in case thereís a better way to do the step youíre trying to do.
Example: Iím stuck on the part of my program thatís supposed to return a square root, and Iíve followed all of the steps above, but Iím still not sure whatís happening. So, I focus on the individual program I wrote for that individual step, and I explain in my forum post that I expect it to return the square root of a parameter, but it seems to always return slightly lower numbers than I expect. I hardcode the program to print out the square root of 40, and I explain that it prints out 6 instead of the 6.3245 that I expect. I include the SSCCE program so that people can look at my code without worrying about extra stuff. I also explain that my high-level goal is to take user input and display its square root. It turns out I forgot that storing a number as an int drops anything after the decimal! Doh!
Step 5- Scroll all the way to the top, and repeat the process all over again. Thatís programming.
By following these guidelines, you are definitely more likely to figure out a problem by yourself. But when you get stuck, you are also more likely to get help much faster than somebody who didnít follow them- itís easier to help somebody who has their problem boiled down to a single small program than it is to wade through a hundred lines of unrelated code. Itís easier to help somebody who knows what their program should do than it is to help somebody who is copying and pasting random code to see what happens. And the easier it is to help you- the more likely it is youíll get help faster.