CISC181 S2019 Lab6

From class_wiki
Jump to: navigation, search


  • Make a new project with n = 6 (following these instructions)
  • There is a NEW second part to the instructions to make it easier to use JUnit in Android Studio
  • This week you can choose a partner, or work alone. No partners will be assigned


In this lab you will develop a public Fraction class (in 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 which contains a suite of unit tests (aka methods) to test Fraction thoroughly.


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:


  • A two-parameter constructor that initializes the numerator and denominator
  • A one-parameter constructor that takes the numerator and sets the denominator to 1


  • 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


  • 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 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. Write comments to explain *why* each test is useful!

IGNORE THIS PART, IT WAS LEFT OVER FROM THE DESCRIPTION OF A PREVIOUS YEAR'S VERSION OF THE LAB: 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.

There should be at a minimum 20+ tests to get decent coverage of Fraction's functionality.


Submit your and on Canvas (be sure to add your name and section number to both). Do NOT submit or whatever you call the file containing main(). Make sure your name and your partner's name is in the comments in both files