Step by Step Instructions for coding pickACard.
1) The problem tells you that the solution will involve 2 methods in one class
plus a test class. Start by creating a skeleton:
- Create a Game class
- Create a main method (no input and no return) with curly braces
- Create a pickACard method (input die value and card value and output score
value). Include the method's curly braces. Because there is a return value
expected, add the statement return 1 at the bottom of your method so that
it will compile. Later, you will return the value you calculated.
- Compile and see no errors (and fix all errors).
- Right click to create a test class.
- Remove extends junit ... after the class name.
- Compile to verify there are no errors.
- You now have a complete skeleton.
2) Code the tests for pickACard
- It helps to code the tests before you code the method. Once you have thought
about the tests, the method code will be easier to write.
- Write a list of possible die value and card value combinations that would
test all the possible answer paths. While you could create very many combinations,
you just need one for each path, and then 2 more for good measure. A path
is a row on the table in the definition of pickACard.
- Open the test class and look at the testSomething method. Add a test for
each die value/ card value combination you listed above.
- To test, call the method checkExpect on your tester object and pass it both
the method call and the expected result. To do that, use this format:
- t.checkExpect(Game.pickACard(10,1),1);
- Repeat that line for each of your tests.
- Then compile and run the tests by choosing test everything.
- They should all fail because your method is currently returning 1 every
time. That is okay now.
3) Code the pickACard method
- Remember have you have the dice value and card value passed as parameters.
Just use those values, without creating the variables.
- Based upon the table of choices in the spec, you will examine the card value
to determine what you will print and what you will return. Making a flow chart
of the questions you need to ask and the actions you will take may help you.
- Create a rValue variable to hold the return value. You can then set the
value inside later if statements and return it at the end.
- Write the if statement structures you expect to use. Remember to put parentheses
after every "if" and curly braces on every if / else if or else
row. Use comments to describe what is being tested and what happens inside
every branch.
- Fill in the tests, and the actions. The actions will include printing to
the screen.
- Return the rValue you set.
- Compile.
- Run the tests by choosing test everything.
- If the test does not pass, fix either the pickACard method or the test depending
upon what is wrong.
4) Code the main method.
- This method has to create 2 random numbers, so import the java.util.Random
class at the top of your class. Create a Random object with: Random your_guy's_name_here
= new Random();
- Ask your random object to create 1 random numbers with 6 choices. Put the
result into a dice variable you create, and add 1 so that the six choices
are from 1 to 6.
- Ask the same random object for another 1 random number with 4 choices. Put
that result ino a card variable you create.
- Call the pickACard method.
- Compile and run your main method.
5) Submit to moodle.
- Upload the java file for your Game class as you usually do.
- Upload the java file for your GameTest class. You will find it in the same
folder as the game class and will find it is a java file.