博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lab2打卡
阅读量:6271 次
发布时间:2019-06-22

本文共 5495 字,大约阅读时间需要 18 分钟。

主要熟悉了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 nonnegative100    * @return the gcd of x and y101    */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     else107         return gcd(y,x%y);//PART IV:Conditionals and Recursive Functions108   }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 objects135    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

运行结果:

 

转载于:https://www.cnblogs.com/jxtang/p/7191102.html

你可能感兴趣的文章
Vue实例初始化的选项配置对象详解
查看>>
PLM产品技术的发展趋势 来源:e-works 作者:清软英泰 党伟升 罗先海 耿坤瑛
查看>>
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
软件测试(二)之 Failure, Error & Fault
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>
CSS基础知识(上)
查看>>
PHP中常见的面试题2(附答案)
查看>>
角色权限分配
查看>>
明小子动力上传拿webshell.zip
查看>>
ES6 Module export与import复合使用
查看>>
第三篇、image 设置圆角的几种方式
查看>>
关于Vs2010 C#使用DirectX的问题
查看>>
EPP(Eclipse PHP)语法高亮仿EditPlus配置
查看>>
OA账号架构权限的问题
查看>>
030——VUE中鼠标语义修饰符
查看>>
python编辑csv
查看>>
sql游标的使用与exec的两种用法
查看>>
数据结构
查看>>