java-00设计-有理数类
1.有理数类代码展示
package Rational;
public class Rational {
private int numrator;//分子
private int denominator;//分母
public int getNumerator() {
return numrator;
}
public int getDenominator() {
return denominator;
}
public Rational()
{
this.denominator=1;
this.numrator=0;
}
public Rational (int num) {//整数
if(num<0)
{
this.denominator=1;
this.numrator=-num;
}
else
{
this.denominator=1;
this.numrator=num;
}
}
public Rational(int numrator,int denominator) {//分数
if(denominator == 0) {
System.out.println("错误输入!分母不能为0!");
System.exit(1);
}
int gcd = getGcd(numrator, denominator);
this.denominator=denominator/gcd;
this.numrator=numrator/gcd;
}
private int getGcd(int v1,int v2)
{
int temp=0;
if(v1<v2) {
temp=v1;
v1=v2;
v2=temp;
}
temp=v1%v2;
while(temp!=0)
{
v1=v2;
v2=temp;
temp=v1%v2;
}
return v2;
}
public Rational add(Rational a) {//相加
int num=a.numrator*this.denominator+a.denominator*this.numrator;//分子通分相加
int den=a.denominator*this.denominator;//分母通分
Rational now=new Rational(num,den);
return now;
}
public Rational subtract(Rational a){//相减
int num=this.numrator*a.denominator-this.denominator*a.numrator;
int den=a.denominator*this.denominator;
Rational now=new Rational(num,den);
return now;
}
public Rational multply(Rational a) {//相乘
int num=a.numrator*this.numrator;
int den=a.denominator*this.denominator;
Rational now=new Rational(num,den);
return now;
}
public Rational divide(Rational a)//相除
{
int num=a.numrator*this.denominator;
int den=a.denominator*this.numrator;
Rational now=new Rational(num,den);
return now;
}
public Rational abs()//绝对值
{
int num,den;
if(this.numrator<0)
num=-this.numrator;
else
num=this.numrator;
if(this.denominator<0)
den=-this.denominator;
else
den=this.denominator;
Rational now=new Rational(num,den);
return now;
}
public int compare(Rational a) {//比大小
if(this.denominator<0&&a.denominator>0)
this.denominator=-this.denominator;
if(this.denominator>0&&a.denominator<0)
{
a.numrator=-a.numrator;
a.denominator=-a.denominator;
}
int num1=this.numrator*a.denominator;
int num2=this.denominator*a.numrator;
if(num1>num2)
return 1;
else if(num1<num2)
return 2;
else
return 0;
}
public String toString() {
if(this.denominator == 1) {
return " " + this.numrator;
}
else {
return " " + this.numrator + "/" + this.denominator;
}
}
}
2.测试代码
package temp;
import Rational.Rational;
public class test {
public static void main(String[] args)
{
Rational r1 = new Rational(1,2);
Rational r2 = new Rational(3,5);
System.out.println("r1 = " + r1.toString() + " ; " + "r2 = " + r2.toString());
System.out.println("r1 + r2 = " + r1.add(r2).toString());
System.out.println("r1 - r2 = " + r1.subtract(r2).toString());
System.out.println("r1 * r2 = " + r1.multply(r2).toString());
System.out.println("r1 / r2 = " +r1.divide(r2).toString());
System.out.println("abs(r1) = " + r1.abs().toString());
System.out.println("abs(r2) = " + r2.abs().toString());
System.out.println("r1 compare r2 = " + r1.compare(r2)+" "+"1表示r1大,2表示r2大,0表示一样大");
}
}
测试结果
3.类名、变量名命名需符合Java规范
代码中变量命名时都尽量采用驼峰命名法,并且整段代码经过eclipse的视图调整,且每个需要返回值的函数都有返回值
尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
首先,我们在进行c语言编程代码时如果存在如上面代码一样多的函数调用时我们会考虑一个问题,就是参数传递,我们一般都是一层套一层传参,或者使用全局变量,导致代码的在使用时就非常复杂,但是我们的面向对象设计着重在于“面向对象”,只要拥有有理数类的主体代码存在,可以任意输入各种测试数据,并且整个代码关系并不紧紧相连,不会出现一个函数出问题(非主要)整个代码出问题的情况
尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。
别人如何复用你的代码?
只需要下载我的package Rational,导入后自己使用测试数据就好
别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?
依赖,我的有理数类的属性存在局限性,所以当我去修改而不是扩大它的包含范围,那么别人使用也只能随着我的修改而变
有理数类的public方法是否设置合适?为什么有的方法设置为private?
我觉得合适,我的public方法都是为了让人能够调用这些函数的来实现他们的目的,而在约分的getDcd中,我将之设为private是因为它是我用来辅助目的达成的存在,并不需要被他人使用,只需要我自己知道就好