Difference between revisions of "CISC181 S2017 Lab7"
(→Instructions) |
(→Instructions) |
||
Line 9: | Line 9: | ||
Suppose you want to write a Poker game app based on the <tt>Card</tt> and <tt>Deck</tt> classes introduced earlier in the semester. Obviously, a core function would be to ''compare'' two hands and determine which one is better. | Suppose you want to write a Poker game app based on the <tt>Card</tt> and <tt>Deck</tt> classes introduced earlier in the semester. Obviously, a core function would be to ''compare'' two hands and determine which one is better. | ||
− | In this lab you will write a <tt>Comparator</tt> to compare hands in ''5-card draw'' Poker, again using test-driven development like last week. The comparison function will be <tt>int compare(ArrayList<Card> H1, ArrayList<Card> H2)</tt>, where a return value < 0 indicates that H1 | + | In this lab you will write a <tt>Comparator</tt> class <tt>ByHandValue</tt> to compare hands in ''5-card draw'' Poker, again using test-driven development like last week. The comparison function will be <tt>int compare(ArrayList<Card> H1, ArrayList<Card> H2)</tt>, where a return value < 0 indicates that H1 would lose to H2, a return value > 0 means that H1 would beat H2, and a return value of 0 means H1 and H2 are "tied." |
− | + | The ranking of hands is detailed [https://en.wikipedia.org/wiki/List_of_poker_hands here]. Assume that cards come from a standard 52-card deck with no wildcards, and aces can be high or low. | |
+ | |||
+ | You should thrown a <tt>PokerException</tt> if either H1 or H2 does not have exactly 5 cards, or if any of the cards are duplicates (either within or between hands). | ||
===Fraction=== | ===Fraction=== |
Revision as of 14:18, 10 April 2017
Contents
Preliminaries
- Make a new project with n = 7 (following these instructions)
- Name your main class "Lab7" (when creating a new module in the instructions above, in the Java class name field)
Instructions
Suppose you want to write a Poker game app based on the Card and Deck classes introduced earlier in the semester. Obviously, a core function would be to compare two hands and determine which one is better.
In this lab you will write a Comparator class ByHandValue to compare hands in 5-card draw Poker, again using test-driven development like last week. The comparison function will be int compare(ArrayList<Card> H1, ArrayList<Card> H2), where a return value < 0 indicates that H1 would lose to H2, a return value > 0 means that H1 would beat H2, and a return value of 0 means H1 and H2 are "tied."
The ranking of hands is detailed here. Assume that cards come from a standard 52-card deck with no wildcards, and aces can be high or low.
You should thrown a PokerException if either H1 or H2 does not have exactly 5 cards, or if any of the cards are duplicates (either within or between hands).
Fraction
Fraction must have two private ints: numerator and denominator. If any arguments to the functions below have denominators with 0 value, you must throw an IllegalArgumentException. Fraction should also implement the Comparable interface. Here are other the public methods that Fraction must implement:
Constructors
- A two-parameter constructor that initializes the numerator and denominator
- A one-parameter constructor that takes the numerator and sets the denominator to 1
Operators
- void add(Fraction F): Add another fraction F to this one
- void subtract(Fraction F): Subtract another fraction F from this one
- void multiply(Fraction F): Multiply this fraction by another fraction F
- void divide(Fraction F): Divide this fraction by another fraction F
- void pow(int n): Raise this fraction to the nth power
Special
- void reduce(): Reduces numerator and denominator to lowest terms. For example, "2 / 4" -> "1 / 2", or "10 / 15" -> "2 / 3". You may look up pseudocode for how to reduce factors.
- String toString(): Returns the fraction as a string, written as "numerator / denominator" -- e.g. "1 / 4", or "3 / 5". It should always be reduced first. If the numerator is 0, just return "0". If the denominator is 1, just return the numerator -- e.g. "5 / 1" -> "5". Also, handle negatives properly! E.g., -3 / -5 should be written as "3 / 5", and 1 / -2 should be written as "-1 / 2".
FractionTests
FractionTests must have tests that cover every method above, with multiple "normal" and "border" cases as input, including compareTo() as implied by Fraction's implementation of the Comparable interface. You should also test that denominator = 0 exceptions are also thrown as specified. If you are not using JUnit, these will cause your program to end, so you may comment them out in run() (see below) as your testing progresses.
Each test method should have the following form: "void xNTest() { ... }", such as "add1Test()" or "division2Test()", etc. Each test method should make at least one Fraction object, set some values, perform an operation or more, and test whether the correct output was obtained. Write comments to explain *why* each test is useful!
To declare that a test has been failed, define a new public class called FractionException derived from Exception. It should have a constructor that takes a String. Throw an instance of FractionException in test method xNTest() constructed with the message string "Failed xNTest()" IF AND ONLY IF A TEST IS FAILED.
If you are NOT using JUnit, make a run() method in FractionTests which calls all of your unit test methods. If you ARE using JUnit, simply annotate all of your test functions appropriately. There should be at a minimum 20+ tests to get decent coverage of Fraction's functionality.
main()
If you are NOT using JUnit, this is what your main() in Lab6.java should contain:
FractionTests fracTests = new FractionTests(); try { fracTests.run(); } catch (FractionException fe) { System.out.println(fe.getMessage()); }
If you ARE using JUnit, this is what your main() should contain:
Result result = JUnitCore.runClasses(FractionTests.class); for (Failure failure : result.getFailures()) System.out.println(failure.toString());
Submission
Submit your Fraction.java and FractionTests.java on Sakai (be sure to add your name and section number to both). Do NOT submit Lab6.java.