Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • How to Program (stuck on homework? READ THIS BEFORE POSTING)

    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.
    How to Program (stuck on homework? READ THIS BEFORE POSTING) KevinWorkman
    2
    1. helloworld922 -
      To add to step 4: If this is for a class, consult your fellow classmates/teaching staff. They're dealing with the same assignments/projects and will likely be able to provide you with quicker and better/more specific help than an online forum (though we do try our best, and there are quite a few bright programmers online that are willing to help you).
    1. KevinWorkman -
      That's a great addition. Too bad I can't figure out how to edit the article!