I have written two classes, Fraction and useFraction. Fraction holds all of the methods required to process the data from useFraction, which consists of private ints that are turned into separate fractions. The source for the classes looks like this:
useFraction looks like this:
Expand|Select|Wrap|Line Numbers
- public class useFraction
- {
- public static void main(String[] args)
- {
- Fraction first=new Fraction(); // assigns 0/1
- Fraction second=new Fraction(4); // assigns 4/1
- Fraction third = new Fraction(6,8); // assigns 6/8 in lowest form, so assigns 3/4
- Fraction last=new Fraction(9,12); // assigns 9/12 in lowest form, so assigns 3/4
- System.out.println(first); //ADDED
- System.out.println(second); //ADDED
- System.out.println(third); // outputs on screen: 3/4
- System.out.println(last); //ADDED
- if (third.equals(last))
- System.out.println("They are equal");
- else
- System.out.println("They are not equal");
- if (second.equals(third))
- System.out.println("They are equal");
- else
- System.out.println("They are not equal");
- if (third.value() >= 0.70)
- System.out.println("third is greater than 0.70 ");
- }
- }
Fraction looks like this:
Expand|Select|Wrap|Line Numbers
- public class Fraction
- {
- private int n=0;
- private int d=0;
- public Fraction()
- {
- n=0;
- d=1;
- }
- public Fraction(int exn)
- {
- n=exn;
- d=1;
- }
- public Fraction(int exn, int exd)
- {
- int GCD;
- //System.out.println("It is "+findGCD(exn,exd)+".");
- n=exn/(findGCD(exn,exd));
- d=exd/(findGCD(exn,exd));
- }
- public static int findGCD(int a, int b)
- {
- return ( b != 0 ? findGCD(b, a % b) : a );
- }
- public String toString ()
- {
- return (n+"/"+d);
- }
- public double value()
- {
- return (n/d);
- }
- //public boolean equals(Fraction otherFraction);
- }