Workshop
3.2 Activity: Fix the coding standard violations
Someone has used the checkGuess method from the previous exercise to create a GuessingGame class that, when run:
- creates a GuessingGame object which sets a number between 1 and 10;
- asks the user to guess what that number is;
- reports whether their guess is correct, too high, or too low.
1. Try compiling and running the program. Does it seem to work?
2. In your groups, look through the code provided for the GuessingGame class. Is it easy to follow?
3. How many violations of the FIT9131 Coding Standards can you spot?
For each Coding Standards violation that you find:
- Make a note of what standard has been violated, and on what line of the code it appears.
- When you have noted all the violations, edit the code to fix the violations.
At the end of the workshop, we will check to see which group found the highest number of coding standards violations!
4. Try compiling and running the program again. Does it still seem to work? Did any of your changes change the logic of the program?
Homework/Extension exercises
Once you have fixed up the GuessingGame class so that it meets the Coding Standards, try running it to check that it works how you would expect. Now the code is easier to understand, see if you can adapt it to make it more advanced. Can you:
- add a non-default constructor that allows the user to select their own hidden number, before playing the game?
- give the user a choice to either set a pre-defined number as the hidden number, or select their own hidden number?
- expand your selection statement so that users read a different message if they guess close to the hidden number, e.g., "You guessed one less/more than the correct number. So close!"