All pastes #507660 Raw Edit

Anonymous

public java v1 · immutable
·published 2007-05-24 17:56 UTC
rendered paste body
// ITI 11121 Summer 2007, Assignment 1// Name: Mike Kelly, Student# 4676728import java.io.*;import javax.swing.JOptionPane;public class Fraction {  //instance fields   private boolean positive; //true if fraction is positive, false when negative.   private int numerator;    //a non-negative integer(ie, 0 is possible)   private int denominator;  //a positive integer(0 impossible)    //Constructors   //default constructor    public Fraction() {      numerator = 0;     denominator = 1;     positive = true;   }      //Numerator only constructor:    public Fraction(int n) {     denominator = 1;     numerator = Math.abs(n);     if (n>0){       positive = true;     }     else{ positive = false; }    }        //"main" constructor; accepts both num and denom integers and creates the object in reduced form.    public Fraction(int n, int d){      //now we will reduce the fraction      int gcd = gcd(Math.abs(n),Math.abs(d));      numerator = (n / gcd);      denominator = (d / gcd);       //First thing lets check if this fraction is positive or negative, then we will reduce it.       //its negative if the numbers have opposite sign and positive otherwise      boolean positive = true;      if((numerator > 0 && denominator < 0) || (numerator < 0 && denominator > 0) ) {       positive = false;       System.out.println("Well, im in the right constructor with n = " + n + " and d = " + d + " and positive = " + positive);         }      else if((numerator > 0 && denominator > 0) || (numerator < 0 && denominator < 0)){        positive = true;        System.out.println("Well, im in the right constructor with n = " + n + " and d = " + d + " and positive = " + positive);         }   System.out.println("Well, im in the right constructor with n = " + n + " and d = " + d + " and positive = " + positive);    }         //Class and instance methods    //gcd - accepts two integers and returns their greatest common divisor.  This will be found recursively.    //if the gcd is returned as 1 then the fraction cannot be reduced further.    private static int gcd(int n, int d){            int r = n % d;//remainder      int gcd; //The GCD to be returned.      //handle the 1st base case, where one of the two integers is 0. This will also handle the case where both are 0.      if(n == 0) { gcd = d; }      else if(d == 0) { gcd = n; }      //next the second base case - if either int is 1 then at most it may be divided by 1(gcd=1)      else if((n == 1) || (d == 1)){ gcd = 1; }      //and finally, the recursive part!!      else if(r == 0){ gcd = d; }      else { gcd = gcd(d,r); }           return gcd;          }        //toString method for printing the object    public String toString(){      System.out.println("Well, now im in the toString method with n = " + numerator + " and d = " + denominator + " and positive = " + positive);      String toReturn = "";      //if the fraction is negative we want a minus sign as the first character in our returned string.      if(positive == false){        toReturn = "-";      }      //check if we have a 0 fraction first.      if((numerator == 0) || (denominator == 0)){       toReturn = "0";      }      //next check if denominator is 1      else if(denominator == 1){       toReturn = toReturn + Math.abs(numerator);      }      else{        toReturn = toReturn + Math.abs(numerator) + "/" + Math.abs(denominator);      }    return toReturn;      }     //testing purposes..    public static void main(String [] args)    {    /* Fraction f1 = new Fraction(), f2 = new Fraction(2), f3 = new Fraction(-3), f4 = new Fraction(2,6), f5 = new Fraction(-6,3),       f6 = new Fraction(-12,-4), f7 = new Fraction(3,7), f8 = new Fraction(-2,5), f9 = new Fraction(4,-7), f10 = new Fraction(-2,-3);          System.out.println(f1 + "," + f2  + "," + f3  + "," + f4  + "," + f5  + "," + f6  + "," + f7  + "," + f8  + "," + f9  + "," + f10);     */     Fraction f4 = new Fraction(2,6);     System.out.println(f4);         }} //end of class Fraction