Difference between revisions of "CISC181 S2017 Lab7"

From class_wiki
Jump to: navigation, search
(Created page with "==Preliminaries== * Make a new project with ''n'' = 7 (following these instructions) * Name your main class "Lab7" (when creat...")
 
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
<p style="font-size:40px">[http://nameless.cis.udel.edu/class_data/181_s2017/Lab7_Tests.java Lab #7 tests]</p>
 +
 +
 
==Preliminaries==
 
==Preliminaries==
  
Line 7: Line 10:
 
==Instructions==
 
==Instructions==
  
Suppose you are writing a Poker game app
+
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 (see [https://drive.google.com/open?id=1DnSnRiRXdtWQTfcNAKarvhHifePBbj_2RGuVC9pORBk lecture 11] for code).  Obviously, a core function would be to ''compare'' two hands and determine which one is better.
In this lab you will develop a public '''Fraction''' class (in <tt>Fraction.java</tt>) with the arithmetic and output functionality listed below.  However, you are also required to use a [http://en.wikipedia.org/wiki/Test-driven_development#Test-driven_development_cycle "test-driven development"] approach based on unit testing.  Thus, as you start to implement the methods below, you must also create a public '''FractionTests''' class (in <tt>FractionTests.java</tt>) which contains a suite of unit tests (aka methods) to test <tt>Fraction</tt> thoroughly.
 
 
 
===Fraction===
 
 
 
<tt>Fraction</tt> must have two private ints: <tt>numerator</tt> and <tt>denominator</tt>.  If any arguments to the functions below have denominators with 0 value, you must throw an <tt>IllegalArgumentException</tt>.  '''<tt>Fraction</tt> should also implement the [https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html<tt>Comparable</tt>] 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====
 
* <tt>void add(Fraction F)</tt>: Add another fraction F to this one
 
* <tt>void subtract(Fraction F)</tt>: Subtract another fraction F from this one
 
* <tt>void multiply(Fraction F)</tt>: Multiply this fraction by another fraction F
 
* <tt>void divide(Fraction F)</tt>: Divide this fraction by another fraction F
 
* <tt>void pow(int n)</tt>: Raise this fraction to the ''n''th power
 
 
 
====Special====
 
* <tt>void reduce()</tt>: Reduces numerator and denominator to [http://en.wikipedia.org/wiki/Irreducible_fraction lowest terms]For example, "2 / 4" -> "1 / 2", or "10 / 15" -> "2 / 3".  You may look up pseudocode for how to reduce factors.
 
* <tt>String toString()</tt>: 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===
 
 
 
<tt>FractionTests</tt> must have tests that cover every method above, with multiple "normal" and "border" cases as input, including <tt>compareTo()</tt> as implied by <tt>Fraction's</tt> implementation of the <tt>Comparable</tt> 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 <tt>run()</tt> (see below) as your testing progresses.
 
 
 
Each test method should have the following form: "<tt>void xNTest() { ... }</tt>", such as "<tt>add1Test()</tt>" or "<tt>division2Test()</tt>", etc.  Each test method should make at least one <tt>Fraction</tt> 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 <tt>FractionException</tt> derived from <tt>Exception</tt>.  It should have a constructor that takes a String.  Throw an instance of <tt>FractionException</tt> in test method <tt>xNTest()</tt> constructed with the message string "Failed xNTest()" IF AND ONLY IF A TEST IS FAILED.
 
  
If you are NOT using JUnit, make a <tt>run()</tt> method in <tt>FractionTests</tt> which calls all of your unit test methodsIf you ARE using JUnit, simply annotate all of your test functions appropriatelyThere should be ''at a minimum'' 20+ tests to get decent coverage of <tt>Fraction's</tt> functionality.
+
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 weekThe 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." You '''must''' use the <tt>Card</tt> class provided -- do not make your own.
  
===main()===
+
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.  This will be a complicated function -- please make it as modular as possible by calling other support functions.
  
If you are NOT using JUnit, this is what your <tt>main()</tt> in <tt>Lab6.java</tt> should contain:
+
You should throw 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).
  
  FractionTests fracTests = new FractionTests();
+
This time, JUnit is required. Once again, you should create a suite of unit tests that make various hands and see if <tt>compare()</tt> works as it shouldAs, before, this is what your <tt>main()</tt> should contain:
try {
 
    fracTests.run();
 
  }
 
catch (FractionException fe) {
 
    System.out.println(fe.getMessage());
 
}
 
  
If you ARE using JUnit, this is what your <tt>main()</tt> should contain:
+
  Result result = JUnitCore.runClasses(HandComparisonTests.class);
 
 
  Result result = JUnitCore.runClasses(FractionTests.class);
 
 
  for (Failure failure : result.getFailures())
 
  for (Failure failure : result.getFailures())
 
   System.out.println(failure.toString());
 
   System.out.println(failure.toString());
 +
 +
We will grade your lab '''strictly''' by how many of the ''N'' unit tests in our grading suite are passed.  Since you can get <tt>compare()</tt> right 1/3 of the time just by chance, your lab score will be determined by a linear map from [N/3, N] tests passed to a score of [1, 4].
  
 
==Submission==
 
==Submission==
  
Submit your <tt>Fraction.java</tt> and <tt>FractionTests.java</tt> on Sakai (be sure to add your name and section number to both).  Do NOT submit <tt>Lab6.java</tt>.
+
Submit your <tt>ByHandValue.java</tt> and <tt>HandComparisonTests.java</tt> on Sakai (be sure to add your name and section number to both).  Do NOT submit <tt>Lab7.java</tt>.

Latest revision as of 13:50, 1 May 2017

Lab #7 tests


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 (see lecture 11 for code). 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." You must use the Card class provided -- do not make your own.

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. This will be a complicated function -- please make it as modular as possible by calling other support functions.

You should throw 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).

This time, JUnit is required. Once again, you should create a suite of unit tests that make various hands and see if compare() works as it should. As, before, this is what your main() should contain:

Result result = JUnitCore.runClasses(HandComparisonTests.class);
for (Failure failure : result.getFailures())
  System.out.println(failure.toString());

We will grade your lab strictly by how many of the N unit tests in our grading suite are passed. Since you can get compare() right 1/3 of the time just by chance, your lab score will be determined by a linear map from [N/3, N] tests passed to a score of [1, 4].

Submission

Submit your ByHandValue.java and HandComparisonTests.java on Sakai (be sure to add your name and section number to both). Do NOT submit Lab7.java.