Java 有理数类 分数类 Rational类的设计与实现

本文介绍了一个Rational类的设计与实现,该类用于表示有理数并支持基本的算术运算,包括加减乘除及比较。文章详细展示了如何通过简化分数来保持数值的最简形式,并提供了toString和equals方法的重写示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要实现Rational类的加减乘除,要实现其可比较性,要覆盖toString()方法,要实现不同数据类型的转换等。

  1 package chapter14;
  2 
  3 public class Rational extends Number implements Comparable {
  4     private long numerator=0;
  5     private long denominator=1;
  6     
  7     public Rational(){
  8         this(0,1);
  9     }
 10     public Rational(long numerator, long denominator) {
 11         // TODO Auto-generated constructor stub
 12         long gcd=gcd(numerator,denominator);
 13         this.numerator=((denominator>0)?1:-1)*numerator/gcd;
 14         this.denominator=Math.abs(denominator)/gcd;
 15     }
 16 
 17     private static long gcd(long n, long d) {
 18         // TODO Auto-generated method stub
 19         long n1=Math.abs(n);
 20         long n2=Math.abs(d);
 21         int gcd=1;
 22         
 23         for(int k=1;k<=n1&&k<=n2;k++){
 24             if(n1%k==0&&n2%k==0)
 25                 gcd=k;
 26         }
 27         return gcd;
 28     }
 29     
 30     public long getNumerator(){
 31         return numerator;
 32     }
 33     public long getDenominator(){
 34         return denominator;
 35     }
 36     
 37     public Rational add(Rational secondRational){
 38         long n=numerator*secondRational.getDenominator()+
 39                 denominator*secondRational.getNumerator();
 40         long d=denominator*secondRational.getDenominator();
 41         return new Rational(n,d);
 42     }
 43     
 44     public Rational subtract(Rational secondRational){
 45         long n=numerator*secondRational.getDenominator()-
 46                 denominator*secondRational.getNumerator();
 47         long d=denominator*secondRational.getDenominator();
 48         return new Rational(n,d);
 49     }
 50     
 51     public Rational multiply(Rational sR){
 52         long n=numerator*sR.getNumerator();
 53         long d=denominator*sR.getDenominator();
 54         return new Rational(n,d);
 55     }
 56     
 57     public Rational divide(Rational sR){
 58         long n=numerator*sR.denominator;
 59         long d=denominator*sR.numerator;
 60         return new Rational(n,d);
 61     }
 62     
 63     public String toString(){
 64         if(denominator==1)
 65             return numerator+"";
 66         else
 67             return numerator+"/"+denominator;
 68     }
 69     
 70     public boolean equals(Object parm1){
 71         if((this.subtract((Rational)(parm1))).getNumerator()==0)
 72             return true;
 73         else 
 74             return false;
 75     }
 76     
 77     
 78     
 79     @Override
 80     public int compareTo(Object o) {
 81         // TODO Auto-generated method stub
 82         if((this.subtract((Rational)o)).getNumerator()>0)
 83                 return 1;
 84         else if((this.subtract((Rational)o)).getNumerator()<0)
 85             return -1;
 86         else
 87             return 0;
 88     }
 89 
 90     @Override
 91     public int intValue() {
 92         // TODO Auto-generated method stub
 93         return (int)doubleValue();
 94     }
 95 
 96     @Override
 97     public long longValue() {
 98         // TODO Auto-generated method stub
 99         return (long)doubleValue();
100     }
101 
102     @Override
103     public float floatValue() {
104         // TODO Auto-generated method stub
105         return (float)doubleValue();
106     }
107 
108     @Override
109     public double doubleValue() {
110         // TODO Auto-generated method stub
111         return numerator*1.0/denominator;
112     }
113 
114 
115     
116 
117 }

  有理数封装在Rational对象中。在机器内部,有理数总表示为它的最简形式,分子决定有理数的符号,分母总为正数。

  gcd()方法是私有静态的。

  Object类中的toString方法和equals方法在Rational类中被覆盖。toString()方法以numerator/denominator的形式返回一个Rational对象的字符串表示。

  

转载于:https://www.cnblogs.com/xingzhui/p/5720875.html

(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接口。+下页图中描述了Rational已将其Number和Comparable接口的关系。 –
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值