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 }

运行结果:

时间: 2024-08-27 02:44:56

lab2打卡的相关文章

执行kubelet卡、解决

现象: 执行kubectl get po -o wide 非常卡.慢 原因: 修改node名称造成的, 解决: https://my.oschina.net/u/3390908/blog/1649764 1.查看原来node [[email protected] ~]# kubectl get no NAME STATUS ROLES AGE VERSION 10.1.1.111 Ready node 8d v1.11.0 10.1.1.68 Ready node 8d v1.11.0 10.1

iOS开发——项目实战总结&amp;UITableView性能优化与卡顿问题

UITableView性能优化与卡顿问题 1.最常用的就是cell的重用, 注册重用标识符 如果不重用cell时,每当一个cell显示到屏幕上时,就会重新创建一个新的cell 如果有很多数据的时候,就会堆积很多cell.如果重用cell,为cell创建一个ID 每当需要显示cell 的时候,都会先去缓冲池中寻找可循环利用的cell,如果没有再重新创建cell 2.避免cell的重新布局 cell的布局填充等操作 比较耗时,一般创建时就布局好 如可以将cell单独放到一个自定义类,初始化时就布局好

制作SD(8G)卡Linux镜像,使得ZC706开发板可以从SD卡启动进入Linux系统

转自网络,供学习记录使用,红色部分是我实验时,这篇文章和网站稍有出入的地方. 目的:制作SD(8G)卡Linux镜像,使得ZC706开发板可以从SD卡启动进入Linux系统 在http://wiki.analog.com/resources/eval/user-guides/ad-fmcomms2-ebz/quickstart/zynq(姑且把这个链接成为链接1吧)链接中找到 图1 点击绿色字体的链接,下载镜像原始文件.这里有不同时期的版本,本说明中选择 图2 下载的原始文件为:2014_R2-

cocos Creator js 房卡麻将/血战/H5四川麻将源码下载搭建

房卡麻将/血战/H5四川麻将 源码 支持iOS/Android/H5 完整源码 1.基于NODEJS+MYSQL的服务器,成熟的技术方案,高效稳定,且方便Windows开发,Linux平台布署,节约服务器运转成本. 2.采用最新版本的cocos引擎,cocos creator开发,可快速的进行界面调整.且能够快速地发布iOS,Android版本. 3.如需H5版本,只需针对H5平台进行资源优化即可. 4.成熟可靠的房卡式设计,能满足大部分用户使用体验. 5.产品经过大量测试,可以运转稳定. 测试

Cocos2d-x山西推倒胡+扣点房卡麻将下载架设教程

1.数据库还原修改后台数据库jeefwtwo 表:sys_user8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 密码(123456)修改游戏数据库 QPPlatformDB表:DataBaseInfo  SQL的地址端口账号密码表:GameRoomInfo  修改服务器的机器标示 PS,搭建的源码必须完整,本文测试源码来自 Cocos2d-x山西推倒胡+扣点房卡麻将maliwl.com 2.执行脚本每个脚本都要执

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

穿越300km沙漠:卡萨帝用7星服务做实体经济升级样板

3月31日,家住新疆克拉玛依戈壁沙漠"无人区"公路守护站的刘女士购买了1台卡萨帝洗衣机,在第2天中午,卡萨帝服务工程师就将洗衣机准时送达用户家.穿越300多公里的沙漠公路.冒着零下20多度的严寒,安装师傅的汽车也已经被沙石砸损,刘女士不禁为卡萨帝的七星级服务标准竖起大拇指,而这只是卡萨帝七星级服务的其中一个缩影.其背后,是由卡萨帝完备的服务链条作为支撑的7星级高端服务体系. 七星级服务:基于传统服务的"超级服务" 在过去的很长一段时间里,产业对"服务&qu

Windows2012上如何查光纤卡HBA的WWN

解决办法有: 1.使用MS utility "fcinfo.exe" (Fibre Channel Information Tool (fcinfo))去查看HBA卡的WWN号. fcinfo工具安装到需要查看WWN号的Windows系统上,接下来在开始菜单的"运行"中键入"CMD",进入命令行模式后, 输入fcinfo就可以看到主机HBA的WWN号了. 2.经过搜索, 找到文章Find HBA and WWN Information on Wi

canvas实现移动端和PC端刮刮卡效果

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"