Difference between revisions of "CISC181 S2017 Lab6"

From class_wiki
Jump to: navigation, search
Line 1: Line 1:
===Preliminaries===
+
==Preliminaries==
  
  
Line 5: Line 5:
 
* Name your main class "Lab6" (when creating a new module in the instructions above, in the ''Java class name'' field)
 
* Name your main class "Lab6" (when creating a new module in the instructions above, in the ''Java class name'' field)
  
===Instructions===
+
==Instructions==
  
 
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.
 
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====
+
===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 and a non-zero numerator, you must throw an <tt>IllegalArgumentException</tt> ("0 / 0" should be treated here as "0" and so not illegal).  <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:
 
<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 and a non-zero numerator, you must throw an <tt>IllegalArgumentException</tt> ("0 / 0" should be treated here as "0" and so not illegal).  <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=====
+
====Constructors====
 
* A two-parameter constructor that initializes the numerator and denominator  
 
* A two-parameter constructor that initializes the numerator and denominator  
 
* A one-parameter constructor that takes the numerator and sets the denominator to 1  
 
* A one-parameter constructor that takes the numerator and sets the denominator to 1  
=====Operators=====
+
====Operators====
 
* <tt>void add(Fraction f)</tt>: Add another fraction f to this one
 
* <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 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 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 divide(Fraction f)</tt>: Divide this fraction by another fraction f
=====Special=====
+
====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>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".
 
* <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".
  
====FractionTests====
+
===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.   
 
<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.   
Line 35: Line 35:
 
Finally, make a <tt>run()</tt> method in <tt>FractionTests</tt> which calls all of your unit test methods.  There should be ''at a minimum'' 20+ tests to get decent coverage of <tt>Fraction's</tt> functionality.
 
Finally, make a <tt>run()</tt> method in <tt>FractionTests</tt> which calls all of your unit test methods.  There should be ''at a minimum'' 20+ tests to get decent coverage of <tt>Fraction's</tt> functionality.
  
====main()====
+
===main()===
  
 
This is all your <tt>main()</tt> in <tt>Lab6.java</tt> should contain:
 
This is all your <tt>main()</tt> in <tt>Lab6.java</tt> should contain:
Line 47: Line 47:
 
  }
 
  }
  
===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>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>.

Revision as of 09:54, 3 April 2017

Preliminaries

  • Make a new project with n = 6 (following these instructions)
  • Name your main class "Lab6" (when creating a new module in the instructions above, in the Java class name field)

Instructions

In this lab you will develop a public Fraction class (in Fraction.java) with the arithmetic and output functionality listed below. However, you are also required to use a "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 FractionTests.java) which contains a suite of unit tests (aka methods) to test Fraction thoroughly.

Fraction

Fraction must have two private ints: numerator and denominator. If any arguments to the functions below have denominators with 0 value and a non-zero numerator, you must throw an IllegalArgumentException ("0 / 0" should be treated here as "0" and so not illegal). 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

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".

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.

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.

To declare that a test has been failed, define a new class (in FractionTests.java but outside of FractionTests) 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.

Finally, make a run() method in FractionTests which calls all of your unit test methods. There should be at a minimum 20+ tests to get decent coverage of Fraction's functionality.

main()

This is all your main() in Lab6.java should contain:

FractionTests fracTests = new FractionTests();
try {
    fracTests.run();
}
catch (FractionException fe) {
    System.out.println(fe.getMessage());
}

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.