Junilu Lacar wrote:Great! So the first thing I look for in code is duplication. Duplication is where the same, or very similar, commands are repeated throughout your program. Find something that has been duplicated in your code. You will then refactor that duplicated code by extracting it to its own method.
Refactoring is covered by these screencasts:
https://www.youtube.com/watch?v=OrsdvCndArY - introduction to refactoring
https://www.youtube.com/watch?v=JZn4hlMvP60 - refactoring - extracting methods
Watch these screencasts and tell me which duplicated instructions in your program you'd like to refactor first.
Junilu Lacar wrote:Ok, create a new Java project. Here's a step-by-step guide that you can follow: https://www.youtube.com/watch?v=zezOt235nSc. Feel free to change the names where appropriate.
For example, when the screencast tells you to create a new project "Whats UP program" just give it whatever name you like. When it tells you to give a name for the new class you're creating, just give it the name of your class. Then copy your code and paste it over whatever Eclipse creates for you by default. Remember that spelling and capitalization matter, so be sure to get that right. Save the file and run your program. You'll need to stop the program manually since you didn't write any code to do that from the program. To do that, you need to find a red square icon on the menu bar. It will look like the STOP button on your video player, and it should be near a green circle with a white triangle that points to the right (like a PLAY button). Click the STOP button to stop your program.
Once you get this done, we should be ready to start refactoring.
Junilu Lacar wrote:
Damien O Sullivan wrote:Should I carry on using jGrasp or should I download Eclipse? If so, what do I need to download exactly?
I'm not familiar with jGrasp and looked around but couldn't tell if it had any features to help in refactoring. If you're up for trying Eclipse, you can download and install the Eclipse IDE for Java Developers from http://www.eclipse.org/downloads/
Junilu Lacar wrote:
Damien O Sullivan wrote:We haven't gone through creating different classes as of yet in the tutorials so and I don't know what helper methods are either.
I would be up for you guiding me through it if it's not too much trouble for you
Not a problem, I'd be happy to.
A couple of things before we start:
1. What IDE or text editor are you using? I think you know enough to use Eclipse now.
2. Because we're not sitting side-by-side, this won't take 15 minutes, so just be ready for this to drag on a bit. Also, there may be some delays in my responses as I am preparing for a minor medical procedure scheduled for tomorrow. Nothing serious, just need to get scoped out "where the sun don't shine" if you get what I mean...The prep is a b:censored:, though. Sorry if this is TMI.
Junilu Lacar wrote:As an exercise, I tried refactoring your code and I got a pretty decent solution in about 15 minutes. I even kept a try-catch block in there but it only spanned 5 lines. As a bonus, the program exits when the user hits the "Cancel" button in the input dialog box. And it does everything else that you wanted it to do. Solution has 74 lines of code including a main method with 2 lines of code, a proper Quiz class, three constants, and four helper methods. The main while loop is only about 30 lines, including whitespaces and there's no duplicate code.
I'll extend my previous offer to guide you through the refactoring of your code, if you're up for it. I really think it would be worth your while.
fred rosenberger wrote:
Damien O Sullivan wrote:The program does what I want it to do at the moment, are there any other problems you can notice with the code?
Post the current code (with code tags, please). It's kind of hard to comment on code we can't see.
Knute Snortum wrote:The braces at lines 20 and 39 are superfluous. The while loop at 17 has only one statement, the try block, and therefore doesn't need braces. However, this is confusing and they should be put in.
Greg Charles wrote:Your indentation is confusing, but it looks like what you are doing there is putting the try block inside the (outer) loop and the associated catch outside that loop. That's violating nesting rules, and I assume it won't compile. Is that what you mean by not working as you want it to? It's better to be specific about problems like that when you are asking questions. "I expected A, but got B," for example. It helps us focus our efforts. In any case, if I've diagnosed it right, you either need both try and catch together inside the block defined by the while loop, or you can put the while loop inside the try block. Although it is legal to use extra braces to define blocks of code that aren't loops, try-catch, if-else, etc., there's usually no reason to do that, and you run the risk of making your code harder to read.
Knute Snortum wrote:You're in a do/while loop, so put something in the catch block that will continue the loop.
Other observations about your code:
* Don't start variable names with a capital letter, doThis instead of DontDoThis.
* You don't need the outer parentheses in the line above.
* You have an extra set of braces inside your do/while loop
* Your formatting, especially your indentation, is inconsistent
Joanne Neal wrote:
Damien O Sullivan wrote:Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.
Your program is crashing because you don't catch the exception thrown by the Integer.parseInt method.
You can't add validation handling to the JOptionPane dialog, so the only way to validate the input (both empty boxes and invalid numbers) is to catch that exception or write your own input dialog that you can add validation to. The first option is the easiest.