分数的加减乘除运算Rational Numbers

时间:2015-04-11 09:33:02
【文件属性】:

文件名称:分数的加减乘除运算Rational Numbers

文件大小:2KB

文件格式:RAR

更新时间:2015-04-11 09:33:02

Rational Numbers 分数的加减乘除运算

8.17 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations: a. Add two Rational numbers: The result of the addition should be stored in reduced form. b. Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. c. Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. d. Divide two Rational numbers: The result of the division should be stored in reduced form. e. Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. f. Print Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.) – 提示: – 有理数是有分子、分母以形式a/b表示的数,其中a是分子,b是分母。例如,1/3,3/4,10/4。 – 有理数的分母不能为0,分子却可以为0。每个整数a等价于有理数a/1。有理数用于分数的精确计算中。例如1/3=0.0000…,它不能使用数据类型double或float的浮点格式精确表示出来,为了得到准确结果,必须使用有理数。 – Java提供了整数和浮点数的数据类型,但是没有提供有理数的类型。 – 由于有理数与整数、浮点数有许多共同特征,并且Number类是数字包装的根类,因此,把有理数类Rational定义为Number类的一个子类是比较合适的。由于有理数是可比较的,那么Rational类也应该实现Comparable接口。


【文件预览】:
RationalNumbers
----Mainclass.java(157B)
----Rationaltest.java(1KB)
----Rational.java(3KB)

网友评论

  • 很好的资源
  • 很好的资源 参考价值高
  • 大概的需要都出来了,但是中间有些错误还要多加修改呀!
  • 看一下,学习下
  • 很好用的,跟老师留的题目一样,正好参考一下
  • 虽然看不太懂 挺好的以后用
  • 说实话不是太好
  • 基本都完成了
  • 太好了。。。和老师布置的作业一样的!
  • 很好~完全符合我作业的需求,学习了!