Rational – Mutable & Immutable

Goals:

1. Java programming practice. 2. Learn how to implement immutable classes and be able to differentiate mutable and immutable classes. 3. Learn now to create a java class . 4. Learn how to create java unit tests using Junit in Netbeans.

Tasks:

1. Create a Java class library project for the two rational classes. 2. Implement a Java mutable MutableRational class in Java in. 3. Implement a Java mutable ImmutableRational class in Java. 4. Implement a Junit test program to show that all of the operations for both classes work.

What to Submit: One zip file that contains your entire project with all of the classes and test programs.

Note: Generally these classes would not be prefixed with the words Mutable or Immutable. We are only naming them this way on this assignment so that their names will be different.

Rational Requirements:

1. Private data members (1 point) a. Numerator as an int b. Denominator as an int 2. Constructors (2 point) a. Default – numerator should become 0 and denominator should become 1 b. One int argument - numerator gets the value of the parameter and the denominator becomes 1. c. Two int arguments – numerator gets the value of the first argument and denominator the value of the second argument. The new value should be reduced. Throw an IllegalArgumentException if the denominator is 0. 3. Private methods (2 point) a. Reduce i. Divides both the numerator and denominator by the greatest common factor of the two. 4. Public methods (18 points) a. Operations - take a Rational or integer argument. Result should be reduced. These should behave differently in the mutable and immutable implementations. The mutable versions should change the current object. The immutable versions should return a new Rational that contains the result of the operation. i. Add ii. Subtract iii. Multiply iv. Divide (Throw an IllegalArgumentException exception if the r-value is 0) v. Set - takes a Rational or integer argument. Copies the value of the argument into the object. This method should only be implemented for the mutable version. vi. ToString – returns a String that contains the numerator followed by a forward slash followed by the denominator. Be sure to use the @Override annotation.

Test program requirements:

5. Test all public methods of both classes using Junit in Netbeans. Verify that they all work. See https://netbeans.org/kb/docs/java/junit-intro.html for help with the unit testing. (7 points)