我刚开始学习JAVA,请高手帮忙编个程序,谢谢

/*创建一个名为Rational(有理数)的类,以执行分数运算.使用整形变量表示该类的private实例变量:numerator(分子)和denominator(分母)。... /*创建一个名为Rational(有理数)的类,以执行分数运算.
使用整形变量表示该类的private实例变量:numerator(分子)和
denominator(分母)。提供一个构造函数,以便在声明该类时,该
函数能够对其进行初始化。该构造函数应存储简化型分数--分数2/4等于1/2,
所以在储存对象时,将1储存numerator中,将2存储denominator中。
提供一个带有默认值的无参构造函数,以防没有提供初始化值。为执行
如下操作提供共有方法:
1>两个分数相加:应一简化型分数储存相加结果
2>两个分数相减:应一简化型分数储存相加结果
3>两个分数相乘:应一简化型分数储存相加结果
4>两个分数相除:应一简化型分数储存相加结果
5>以a/b的形式打印有理数,其中a是分子,b是分母。
6>以浮点数的形式打印有理数*/
展开
 我来答
百度网友80cde99
2008-12-29 · 超过20用户采纳过TA的回答
知道答主
回答量:169
采纳率:0%
帮助的人:74.7万
展开全部
public class Rational {
private int numerator;//分子
private int denominator;//分母

public Rational()
{

}

public Rational(int numerator,int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}

public int getNumerator() {
return numerator;
}

public void setNumerator(int numerator) {
this.numerator = numerator;
}

public int getDenominator() {
return denominator;
}

public void setDenominator(int denominator) {
this.denominator = denominator;
}

public Rational add(Rational left,Rational right)
{
Rational result = new Rational();
int tempDenominator = this.getCommonMultiple(left.getDenominator(), right.getDenominator());
int tempNumerator = left.getNumerator()*(tempDenominator/left.getDenominator())+right.getNumerator()*(tempDenominator/right.getDenominator());
result.setDenominator(tempDenominator);
result.setNumerator(tempNumerator);
return result;
}
//得到公约数
private int getCommonDivisor(int a,int b)
{
int result = 1;
//System.out.println("----------getCommonDivisor");

//System.out.println("a:"+a+",b:"+b);
int primeNumber[] = getAllPrimeNumber(a>b?a:b);
//System.out.println("素数个数有:"+primeNumber.length);
for(int i=0;i<primeNumber.length;i++)
{
//System.out.println("素数有:"+primeNumber[i]);
if(primeNumber[i]==0)
{
break;
}
if((a%primeNumber[i]==0) && (b%primeNumber[i])==0)
{
result *=primeNumber[i];
}
}
//System.out.println("公约数为:"+result);
return result;
}
//得到小于某个数的所有素数
public int[] getAllPrimeNumber(int number)
{
//System.out.println("----------getAllPrimeNumber");
int arrays[] = new int[number];
int count = 0;
for(int i=2;i<=number;i++)
{
//System.out.println("当前数:"+i);
int flag=1;
for(int j=2;j<=Math.sqrt(i);j++)
{
//System.out.println("循环内部:"+j);
if(i%j==0&&i!=j)
{
//System.out.println("当前数:"+i);
flag = 0;
break;
}
}
if( flag == 1)
{
arrays [count++] = i;

//System.out.println("素数:"+i);
}
}
return arrays;
}
public int getCommonMultiple(int a,int b)
{
int commonDivisor = getCommonDivisor(a,b);
int commonMultiple = (a/commonDivisor)*b;
//System.out.println("公倍数为:"+commonMultiple);
return commonMultiple;
}
public Rational subtration(Rational left,Rational right)
{
Rational result = new Rational();
int tempDenominator = this.getCommonMultiple(left.getDenominator(), right.getDenominator());
int tempNumerator = left.getNumerator()*(tempDenominator/left.getDenominator())-right.getNumerator()*(tempDenominator/right.getDenominator());
result.setDenominator(tempDenominator);
result.setNumerator(tempNumerator);
return result;
}
public Rational multiplication(Rational left,Rational right)
{
Rational result = new Rational();
int tempDenominator = left.getDenominator()*right.getDenominator();
int tempNumerator = left.getNumerator()*right.getNumerator();
int cn = this.getCommonDivisor(tempDenominator, tempNumerator);
result.setDenominator(tempDenominator/cn);
result.setNumerator(tempNumerator/cn);
return result;
}
public Rational divisor(Rational left,Rational right)
{
Rational result = new Rational();
Rational temp = new Rational();
temp.setDenominator(right.getNumerator());
temp.setNumerator(right.getDenominator());
result = this.multiplication(left, temp);
return result;
}
}
public class TestMain {

public static void main(String[] args) {
Rational temp1 = new Rational(3,4);//第一个参数是分子,第二个是分母
Rational temp2 = new Rational(5,6);

Rational result = new Rational();
result = result.add(temp1, temp2);
System.out.println("-----------testMain");
System.out.println("加法:");
result = result.add(temp1, temp2);
System.out.println("结果为:"+result.getNumerator()+"/"+result.getDenominator());
System.out.println("结果为:"+(double)result.getNumerator()/result.getDenominator());

System.out.println("减法:");
result = result.subtration(temp1, temp2);
System.out.println("结果为:"+result.getNumerator()+"/"+result.getDenominator());
System.out.println("结果为:"+(double)result.getNumerator()/result.getDenominator());
System.out.println("乘法:");
result = result.multiplication(temp1, temp2);
System.out.println("结果为:"+result.getNumerator()+"/"+result.getDenominator());
System.out.println("结果为:"+(double)result.getNumerator()/result.getDenominator());
System.out.println("除法:");
result = result.divisor(temp1, temp2);
System.out.println("结果为:"+result.getNumerator()+"/"+result.getDenominator());
System.out.println("结果为:"+(double)result.getNumerator()/result.getDenominator());

}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式