CIT 590 Homework 6 Fractions Purposes of this assignment: Get you started in Java and Eclipse Get you comfortable using objects in Java Start looking at some common object uses in Java. General Idea of the Assignment In this assignment, you will implement two Java classes, Fraction.java and FractionCalculator.java. Create a project Fraction and within it a package called fraction. Your classes, Fraction and FractionCalculator, will be within this package. The Fraction API Your task will be to implement the API (Application Programmer s Interface) and write a small program that uses it. The API is basically a collection of all methods defined in a given class if another programmer would like to use the class, they have to refer to the API to see what methods are available. In other words, Fraction isn't a program in itself; it is designed to be used by programmers who, for whatever reason, need to use fractions in their programs. This means Fraction.java should have no main method. The main method will be in FractionCalculator.java. The following lists some information about your Fraction class. Instance Variables How printed Restrictions Normalization Two private integers, known as the numerator and the denominator. n/d, where n is the numerator and d is the denominator. This is achieved in the tostring() method defined below. The denominator may not be zero. The fraction is always kept in the lowest terms, that is, the Greatest Common Divisor (GCD) of n and d is 1 (use Euclid's algorithm you can look this up; I also showed an example in the Python overview lecture). The denominator is never negative. (You can give a negative number for
the denominator to the constructor when you create the fraction, but a negative fraction should be represented internally with a negative numerator.) Represent zero as 0/1. You should have the following constructors. public Fraction(int numberator, int denominator) public Fraction(int wholenumber) Parameters are the numerator and the denominator. Normalize the fraction as you create it. For instance, if the parameters are (8, -12), create a Fraction with numerator -2 and denominator 3. The parameter is the numerator, and denominator is implicitly 1. The parameter is a String such as "5/ -10", or "2" (allow blanks around the integers, but not within). public Fraction(String fraction) You can use split("/") to get the two parts, then trim() both parts, then use Integer.parseInt(part) to convert each part from type String to type int. Note that Java allows you to have more than one constructor, so long as they have different numbers or types of parameters. For example, if you call new Fraction(3) you will get the second constructor listed above. Fractions should be constructed in normalized form. For example, if the user says new Fraction(6, 8), the fraction should be created with a numerator of 3 and a denominator of 4 (not 6 and 8). Similarly, if the user says new Fraction(1, -3), the fraction should be created with a numerator of -1 and a denominator of 3 (not 1 and -3). Finally, you should implement the following methods. Public Fraction add(fraction f) public Fraction subtract(fraction f) the sum of this and that: a/b + c/d is (ad + bc)/bd the difference of this minus that:
a/b - c/d is (ad - bc)/bd public Fraction multiply(fraction f) public Fraction divide(fraction f) public Fraction abs() public Fraction negate() public Fraction inverse() public int compareto(fraction f) public String tostring() the product of this minus that: (a/b) * (c/d) is (a*c)/(b*d) the quotient of this minus that: (a/b) / (c/d) is (a*d)/(b*c) the absolute value of this fraction. Returns a new Fraction that has the same numeric value as this fraction but the opposite sign. Inverse of a/b is b/a. Any negative int if this is less than f. Zero if this is equal to f. Any positive int if this is greater than f. Returns a String of the form n/d, unless d is 1, in which case it returns just n. Notes Fractions should be immutable, that is, there should be no way to change their components after the numbers have been created. Most of your methods simply return a new number. One way to achieve this is to keep instance variables private (without any methods available to modify them). Depending on how fast we go, we may not cover the tostring() method before the assignment is due. All you need to know for this assignment is that it is defined as shown above and that it is called internally when you want to print an object. For example, the following two print statements will print the same thing: Fraction f = new Fraction(6,11); System.out.println(f); System.out.println(f.toString());
We haven t covered unit testing in Java yet, so for this assignment you are not required to submit any test files. However, for your own benefit (and to make sure your program is correct) you should still write some tests. To test your files, you can write a FractionTest.java file. This file should contain a public static void main(string[] args) method to test your methods. You should not submit this file. The FractionCalculator Write a FractionCalculator class (containing a main method) that does calculations with Fractions. The first thing this program does is print a zero (indicating the current contents of the calculator), and a prompt (print some message to the user). It then accepts commands from the user, and after each command, prints the result, and a new prompt. It should accept exactly the following commands, and nothing else: a To take the absolute value of the number currently displayed. c To clear (reset to zero) the calculator. i To invert the number currently displayed. s n To discard the number currently displayed and replace it with n. q To quit the program. + n To add n to the number currently displayed. - n To subtract n from the number currently displayed. * n To multiply the number currently displayed by n. / n To divide the number currently displayed by n. In each case, the user may enter either a whole number or a fraction for n. Fractions may be written with or without spaces, as for example 27 / 99 or 27/99. You can require that there be no space after a unary minus, so for example -3 is legal, but - 3 is not. You don't have to handle unary +, i.e., assume + if always followed by a fraction (and space). You can require at least one space after the initial +, -, *, /, or s; for example a - -3/5 is legal, but --3/5 is not. If the user enters a different command than the ones defined above, your program should not crash. Instead, the program should print a short error message and keep running. Illegal input should not affect the state of the computation. You can assume that fractions are well formatted, i.e., in any command that contains a fraction you can just parse the fraction assuming that it is indeed a fraction (though you need to handle the case where the fraction is a whole number). In other words, you don t need to worry about checking whether the string contains a fraction (which requires exception handling in java, and we haven t covered that yet). You will need to use the Scanner. You can find a description (skip over the confusing details) in the Scanner page in the Java API. Recommendation: Just use the Scanner's nextline() method, and use String methods to work with the input. Some useful methods are trim(), length(),
and substring(beginindex). I found this somewhat easier that working with the Scanner methods to read the input in smaller pieces. Submission and Due Date Zip your Eclipse project and turn it in to Canvas before 11:59pm Tuesday, October 31.