首页 > 代码库 > lab2打卡

lab2打卡

主要熟悉了JAVA之中this的用法,以及按照提示的scheme写出辗转相除法的递归算法。

代码如下:

技术分享
  1 package lab2;
  2 /* Fraction.java */
  3 
  4 import java.io.*;
  5 
  6 /** The Fraction class implements nonnegative fractions--rational numbers.
  7  */
  8 class Fraction {
  9 
 10   /* private fields within a Fraction. */
 11   private int numberOfFractions = 0;
 12 
 13   private int numerator;
 14   private int denominator;
 15 
 16   /** Constructs a Fraction n/d. 
 17    *  @param n is the numerator.  Must be nonnegative.
 18    *  @param d is the denominator.  Must be positive.
 19    */
 20   public Fraction(int n, int d) {
 21     if (n < 0) {
 22       System.out.println("Fatal error:  Negative numerator.");
 23       System.exit(0);
 24     }
 25     if (d < 1) {
 26       System.out.println("Fatal error:  Non-positive denominator.");
 27       System.exit(0);
 28     }
 29     numberOfFractions++;
 30     numerator = n; 
 31     denominator = d;
 32   }
 33 
 34   /** Constructs a Fraction n/1. 
 35    *  @param n is the numerator.  Must be nonnegative.
 36    */
 37   public Fraction(int n) {
 38     this(n, 1);
 39   }
 40 
 41   /** Constructs a Fraction 0/1. 
 42    */
 43   public Fraction() {
 44     this(0, 1);//Part I:Constructor
 45   }
 46 
 47   /** Copies the Fraction "original".
 48    */
 49   public Fraction(Fraction original) {
 50     numberOfFractions++;
 51     numerator = 0;
 52     denominator = 1;
 53   }
 54 
 55   /** Converts this Fraction to a string format:  "numerator/denominator."
 56    *  Fractions should be printed in reduced form (part of your assignment is
 57    *  to make this true).
 58    *  @return a String representation of this Fraction.
 59    */
 60   public String toString() {
 61     int thisGcd = gcd(numerator, denominator);
 62 
 63     return (numerator / thisGcd + "/" + denominator / thisGcd);
 64   }
 65 
 66   /** Return the sum of two fractions.
 67    *  @param f2 is the Fraction to be added.
 68    *  @return the result of adding f2 to this Fraction.
 69    */
 70   public Fraction add(Fraction f2) {
 71     Fraction r = new Fraction((numerator * f2.denominator) +
 72                   (f2.numerator * denominator),
 73                   denominator * f2.denominator);
 74     return r;
 75   }
 76 
 77   /** Replaces this Fraction‘s numerator with a new value.
 78    *  @param numerator is the new numerator.  Must be nonnegative.
 79    */
 80   public void changeNumerator(int numerator) { // DO NOT CHANGE THIS SIGNATURE!
 81     // Fix the bug that prevents this method from working correctly.
 82     if (numerator < 0) {
 83       System.out.println("Fatal error:  Negative numerator.");
 84       System.exit(0);
 85     }
 86     this.numerator = numerator;//PART III: Defining Classes; 
 87   }
 88 
 89   /** Returns the number of Fraction objects in existence.
 90    *  @return the number of Fraction objects in existence.
 91    */
 92   public int fracs() {                         // DO NOT CHANGE THIS SIGNATURE!
 93     // Fix the bug that prevents this method from working correctly.
 94     return numberOfFractions;
 95   }
 96 
 97   /** Computes the greatest common divisor (gcd) of the two inputs.
 98    * @param x must be nonnegative
 99    * @param y must be nonnegative
100    * @return the gcd of x and y
101    */
102   static private int gcd (int x, int y) {
103     /* Replace the following line with your solution. */
104     if(y==0) 
105     return x;
106     else
107         return gcd(y,x%y);//PART IV:Conditionals and Recursive Functions
108   }
109 
110   /** Put the Fraction class through some tests.
111    * @param argv is not used.
112    */
113   public static void main(String[] argv) {
114 
115     /* Test all four contructors and toString. */
116     Fraction f0 = new Fraction();
117     Fraction f1 = new Fraction(3);
118     Fraction f2 = new Fraction(12, 20);
119     Fraction f3 = new Fraction(f2);
120 
121     System.out.println("\nTesting constructors and toString():");
122     System.out.println("The fraction f0 is " + f0.toString());
123     System.out.println("The fraction f1 is " + f1);    // toString is implicit.
124     System.out.println("The fraction f2 is " + f2);
125     System.out.println("The fraction f3 is " + f3 + ", which should equal f2");
126 
127     /* Test the add method. */
128     System.out.println("\nTesting add:");
129     Fraction sumOfTwo = f1.add(f2);              // Sum of f1 and f2.
130     Fraction sumOfThree = f0.add(f1).add(f2);             // Sum of f0, f1, and f2.
131 
132     System.out.println("The sum of " + f1 + " and " + f2 + " is " + sumOfTwo);
133     System.out.println("The sum of " + f0 + ", " + f1 + " and " + f2 + " is " +
134                        sumOfThree);  //PART II:Using objects
135    
136     /* Test the methods used in Part III. */
137     System.out.println("\nTesting changeNumerator and fracs:");
138 
139     f3.changeNumerator(7);
140     System.out.println("Now f3 is " + f3 + ", which should be 7/20");
141     System.out.println("The total number of Fraction objects is " +
142                        f3.fracs());
143 
144     /* Test gcd function (static method). */
145     System.out.println("\nTesting gcd:");
146     System.out.println("The gcd of 2 and 10 is: " + gcd(2, 10));
147     System.out.println("The gcd of 15 and 5 is: " + gcd(15, 5));
148     System.out.println("The gcd of 24 and 18 is: " + gcd(24, 18));
149     System.out.println("The gcd of 10 and 10 is: " + gcd(10, 10));
150     System.out.println("The gcd of 21 and 400 is: " + gcd(21, 400));
151   }
152 }
View Code

运行结果:

技术分享

 

lab2打卡