Starting from:
$30

$24

Assignment 4: More methods, while-loops, Random Solution




How to hand in your work




Submit the requested file (Pig.java) through the Assignment #4 link on conneX.










Learning outcomes




When you have completed this assignment, you should understand:




How to pass parameters and return values using static methods.
How to use while loops.



How to use the Random class and its nextInt method.



How to logically decompose a program into smaller parts.
How to indent and document a Java program.









Write the code for a Java program named Pig.java. The instructions on how to play Pig, as well as the specification for the methods required in your program can be found in the Pig.html file linked here. Read the specification document carefully, as it describes what each method should do. Each of the methods must be present in the Pig class, with the headers (parameters and return values) exactly as described. (Note that for the Scanner and Random parameters, you can omit the java.util. prefix by writing import statements at the top of the source code.)




For this assignment, testing is crucial. All tests for the methods are implemented in the main method. During and after the writing of each method, call the current method from main, setting up enough tests to satisfy yourself that the method works. See Appendix A for examples of good testing output. As you progress, comment out the previous tests (don’t delete them; we’ll be checking), and carry on with the next test.































Page 1 of 9
Recommended steps to follow in order




The diceRoll method is a nice place to start. Focus on the simple responsibility this method has, without thinking about the rest of the game. It simply rolls a die and returns the value that of the roll.



The playerTurn and the computerTurn methods are similar and yet still different. One requires some user interaction and the other does not. Choose whichever one you would like to do first. Make sure you finish and test them both before moving to the next step.



The final method puts the whole game together and should be implemented only after the previous three are tested successfully. When you test this method in main and it produces output that is similar to the final output in Appendix A, you can play Pig!






Marking




Your mark will be based on the following criteria:




Your code must compile and run. It must prompt the user, generate Random numbers, and produce the expected output as demonstrated in Appendix A.



Your code must conform to all the requirements mentioned in the specification document.



The main method must show all of your testing code. It may be commented out once you have tested parts of it. The final call to gameLoop() should not be commented out.



Your code must follow the guidelines outlined in Style_Guidelines.pdf, found through the Lectures & Stuff link in the Lab Resources folder on conneX. You may note that the specifications provide some very nice comments you are welcome to borrow.


































































Page 2 of 9
Appendix A – Testing your code




As you work through a solution, it is recommended that you save, compile and test your code after every line or two of code that you write. This can be something as easy as printing out the value of a variable, or calling a method to print out the value returned. It is important to do this to confirm a component of your code works correctly, so you can be confident using that component throughout your code later.










Testing the diceRoll method:




One way to do this is to create a for-loop that loops 20 times. Inside the for-loop, call the diceRoll method and assign the value it returns to an integer variable roll. Then, print out the value of roll.








































What to look for:




 Is the minimum number a 1?




 Is the maximum number a 6?
























































































Page 3 of 9
Testing the playerTurn method: (user input underlined in red):































What to look for:




 Does it continually roll if the user chooses to roll again?




 Does the turn end if the user chooses to end the turn?




 Is the sum of all rolls returned at the end of a turn?




 If a 1 is rolled, does the turn end with a score of 0 returned































































































































Page 4 of 9
Testing the computerTurn method:

































































































What to look for:




 Does the computer’s turn always last 4 turns?




 Is the sum of all rolls returned at the end of a turn?




 If a 1 is rolled, does the turn end early with a score of 0 returned































































More products