A Class for Working with Fractions

 Background.  This next class is starting to get more involved and useful.  You should remember working with fractions from your earlier schooling.  Remember, fractions are represented by a/b = numerator/denominator You learned to do things such as add, subtract, multiply and divide fractions.  You also learned to reduce them to lowest terms so that the numerator and denominator did not have any common factors.  These are the operations that will concern us in this webpage. But the other thing to remember, is that you learned a very visual approach to working with fractions.  Solutions were written on paper and blackboards.  Doing these same sorts of things on a computer requires some modifications to your thinking.  We are not going to represent fractions in their usual form within the computer.  This would require significantly more overhead and ability than we have at present. Since we are presently limited to making use of methods and their notation, we will use a two-tuple to represent a fraction in our user defined class. Rational(numerator, denominator) Then we will construct methods for adding subtracting multiplying dividing reducing writing them to a string Now you want to copy the following code into its own directory.  Notice we are going to create a package so you need to have a RationalClasses subdirectory of your javaPackages directory.  You should call the following file Rational.java.

 package javaPackages.RationalClasses; public class Rational extends Object { private int numerator; private int denominator; public Rational( ) { setRational(0,0); } public Rational(int num, int denom) { setRational(num, denom); } public void setRational(int num, int denom) { numerator = num; denominator = denom; } public void setNumerator(int num) { numerator = num; } public void setDenominator(int denom) { denominator = denom; } public int getNumerator( ) { return numerator; } public int getDenominator( ) { return denominator; } public void ReduceForm(int a, int b) { // set array with prime numbers int primes[] = {2,3,5,7,11,13,17,19,23}; for(int counter = 0; counter < primes.length; counter++) { //  while a particular prime divides into both the numerator and //  denominator divide it out of both while ((a % primes[counter] == 0) && (b % primes[counter] == 0)) { a = (a / primes[counter]); b = (b / primes[counter]); } } setRational(a, b); } public Rational Add( Rational firstRational, Rational secondRational) { Rational result = new Rational( ); result.setNumerator((firstRational.getNumerator() * secondRational.getDenominator()) + (secondRational.getNumerator() * firstRational.getDenominator())); result.setDenominator(firstRational.getDenominator() * secondRational.getDenominator()); result.ReduceForm(result.getNumerator(), result.getDenominator()); // now set all the stuff numerator = result.getNumerator(); denominator = result.getDenominator(); return result; } public Rational Subtract( Rational firstRational, Rational secondRational) { Rational result = new Rational( ); result.setNumerator((firstRational.getNumerator() * secondRational.getDenominator()) - (secondRational.getNumerator() * firstRational.getDenominator())); result.setDenominator(firstRational.getDenominator() * secondRational.getDenominator()); result.ReduceForm(result.getNumerator(), result.getDenominator()); // now set all the stuff numerator = result.getNumerator( ); denominator = result.getDenominator( ); return result; } public Rational Multiply( Rational firstRational, Rational secondRational) { Rational result = new Rational( ); result.setNumerator(firstRational.getNumerator( ) * secondRational.getNumerator( )); result.setDenominator(firstRational.getDenominator( ) * secondRational.getDenominator( )); result.ReduceForm(result.getNumerator(), result.getDenominator()); numerator = result.getNumerator( ); denominator = result.getDenominator( ); return result; } public Rational Divide( Rational firstRational, Rational secondRational) { Rational result = new Rational( ); result.setNumerator(firstRational.getNumerator( ) * secondRational.getDenominator( )); result.setDenominator(firstRational.getDenominator( ) * secondRational.getNumerator( )); result.ReduceForm(result.getNumerator( ), result.getDenominator( )); numerator = result.getNumerator( ); denominator = result.getDenominator( ); return result; } public String toString( ) { return numerator + "/" + denominator; } }

 You do not need to specify any special path in the compilation command. javac RationalTestGUI.java You should run this code and play with the GUI some before we dig into it.The HTML file follows and should be called RationalTestGUI.html.



 If you try to click on the command button and don't have an appropriate input you should see something like the following.  For this example I put a/4 and 3/0 in for the first and second fractions.

 With acceptable numbers in the length and width text fields you should see something more like the following.