Alicia Perry wrote:However, I'm realizing how important it is to think before you write the code:...
And just one final point: there's a simple exercise you can go through to help with "what not how".
1. When you get given a complex set of instructions, don't start thinking about Java, or even what needs to be done. Clear your mind.
2. Sit down with your instructions and read through them. Several times; until you know them back to front. Make notes about anything you don't understand, and make sure you get clarification on them before you proceed any further.
3. Read through them a few more times, noting down all the major verbs and nouns (keep two separate lists), and keep doing that until you're fairly sure you have them all. If, as in your case, the instructions contain stuff about actual classes or methods you need to implement, put the classes and interfaces in the "nouns" column, and methods in the "verbs" column. You might even annotate them with a "(C)", "(M)" or "(I)" to remind yourself that they're required exactly as specified, but don't forget to include all the otherEnglish nouns and verbs you find as well.
It's called "lexical analysis" (or some silly term like that), and it can be a really good 'first cut' in the "what" process, because it helps give you an idea of what you're likely to need. It's not foolproof, by any means, and you'll find that you get better at it with practise. But the important thing is NOT to think about how these "things" are going to work while you're doing it. It's a simple extraction process; nothing else.
Once you have your lists, the nouns will generally equate to classes or events, and verbs to methods or actions. It's not a 100% rule, but it's usually pretty close. What you'll also find is that many nouns will fit with an existing Java class: for example, a "number" will probably equate to an Integer or a Double, and a "list" to some sort of Java Collection, but don't start thinking about that yet. You're simply gathering information at this stage.
Then start linking verbs with their associated noun(s), which may mean reading through your instructions again to understand the context. You can even start writing rudimentary method signatures based on whether nouns are the subject or object of a verb, so a phrase like "put the value in the list" might become something like:
list.put(value) but again: don't make them look too much like Java yet. You're analysing, not coding.
It's inevitable of course that some "how will I do this?" thoughts will be going through your mind, but try to keep them at bay for as long as possible. What you'll find is that the simple act of making these lists, and refining them, and connecting the verbs and nouns, gradually starts to give you the structure of your problem, so that when you start the process of translation to Java, you'll have a much better idea of the classes you're going to need and the methods they'll need to implement.
And when you DO start that process, you may well find the WhereDoIStart page worth a read, because it describes a technique called "stubbing" which is really useful to know. Basically, it allows you to write a class that compiles, but does absolutely nothing; rather like an interface. Sound stupid? Well it isn't, because it allows you to build up that class gradually, and concentrate (more or less) on one thing at a time.
Hope it helps, and good luck to you.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here