Loading...

有理数类的设计

有理数类的设计

有理数类代码

package Realization;

public class Test {

	private int molecule;    //分子
	private int denominator; //分母
	private int Test(int mol,int den)           //找最大公约数
	{
		int result = 0;
		if(mol>0&&den>0)
		{
			while (den != 0) {
		        result =mol % den;
		        mol = den;
		        den = result;
		    }
		}
		else if(mol<0&&den>0)
		{
			mol=-mol;
			while (den != 0) {
		        result =mol % den;
		        mol = den;
		        den = result;
		    }
		}
		else if(mol>0&&den<0)
		{
			den=-den;
			while (den != 0) {
		        result =mol % den;
		        mol = den;
		        den = result;
		    }
		}
		else
		{
			mol=-mol;
			den=-den;
			while (den != 0) {
		        result =mol % den;
		        mol = den;
		        den = result;
		}
			mol=-mol;
		}
	    return mol;
		
	}
	public Test(int mol,int den)                               //两个整数构造有理数
	{
		int Rational;
		int divisor;
		if(den==0)
		{
			System.out.println("Input error: denominator must not be 0");
			System.exit(0);
		}
		divisor=Test(mol,den);
		this.molecule=mol/divisor;
		this.denominator=den/divisor;
		
			
		
	}
	public Test(Double x)                                //一个Double数转换为有理数
	{
		String s=x.toString();
		int index=s.indexOf(".");
		int minus=s.length()-index;
		int den=(int) Math.pow(10, minus);
		int mol=(int) (den*x);
		int divisor = Test(mol,den);
		this.molecule=mol/divisor;
		this.denominator=den/divisor;
		
	} 
	public Test(String x)                                  //一个字符型数转化为有理数
	{
		Double s=Double.parseDouble(x);
		int index = x.indexOf(".");
		if(index==-1)
		{
			this.denominator=1;
			this.molecule=Integer.parseInt(x);
			return;
		}
		int minus=x.length()-index;
		int den=(int) Math.pow(10, minus);
		int mol=(int) (den*s);
		int divisor = Test(mol,den);
		this.molecule=mol/divisor;
		this.denominator=den/divisor;
	}
	public String toString()                       //以字符串形式输出
	{
		Test x=new Test(0,1);
		String s=new String();
		x.molecule=this.molecule;
		x.denominator=this.denominator;
		if(x.molecule*x.denominator<0)
		{
			s=s+"-";
		}
		x=abs(x);
		if(x.denominator==1)
		{
			s+=x.molecule;
		}
		else
		{
			s+=x.molecule+"/"+x.denominator;
		}
		
		return s;
		
	}
	public static Test abs(Test a) {                //转成绝对值
		// TODO Auto-generated method stub
		Test x=new Test(0,1);
		x.molecule=a.molecule;
		x.denominator=a.denominator;
		if(x.molecule<0)
		{
			x.molecule=-x.molecule;
		}
		if(x.denominator<0)
		{
			x.denominator=-x.denominator;
		}
		
		return x;
	}
    public Test add(Test x)                //加法运算
    {
    	int mol;
    	int den;
    	Test m;
    	if(this.molecule*x.molecule<0)
    	{
    		m=contrast(this,x);
    	}
    	mol=this.molecule*x.denominator+this.denominator*x.molecule;
    	den=this.denominator*x.denominator;
    	Test n=new Test(mol,den);
		return n;
    	
    }
    public Test minus(Test x)                 //减法运算
    {
    	int mol;
    	int den;
    	Test m;
    	mol=this.molecule*x.denominator-this.denominator*x.molecule;
    	den=this.denominator*x.denominator;
    	Test n=new Test(mol,den);
		return n;
    	
    }
    public Test multiplication(Test x)              //乘法运算
    {
    	int mol;
    	int den;
    	Test m;
    	mol=this.molecule*x.molecule;
    	den=this.denominator*x.denominator;
    	Test n=new Test(mol,den);
		return n;
    	
    }
    public Test division(Test x)              //除法运算
    {
    	int mol;
    	int den;
    	Test m;
    	mol=this.molecule*x.denominator;
    	den=this.denominator*x.molecule;
    	Test n=new Test(mol,den);
		return n;
    	
    }
	private static Test contrast(Test test, Test x) {                 //比大小,返回较大值
		// TODO Auto-generated method stub
		int mol1,mol2;
		int den;
		int big;
		if(test.denominator<=x.denominator&&test.molecule>x.molecule)
		{
			return test;
		}
		if(test.denominator>=x.denominator&&test.molecule<x.molecule)
		{
			return x;
		}
		if(test.denominator>x.denominator&&test.molecule>x.molecule)
		{
			den=test.denominator*x.denominator;
			mol1=test.molecule*x.denominator;
			mol2=x.molecule*test.denominator;
			if(mol1>mol2)return test;
			else return x;
		}
		return null;
	}
	public  Test big(Test x) {             //取较大数
		// TODO Auto-generated method stub
		int index=this.minus(x).toString().indexOf("-");
		if(index==-1) {
			return this;
		}else {
			return x;
		}
	
	}
	public boolean equal(Test x)      //判断俩个数是否相等
	{
		if(this.minus(x).toString().equals("0"))return true;
		return false;
	}
}
    

实现及测试代码

package main;

import Realization.Test;

public class main {
	public static void main(String args[])
	{
		int x = 1;
		int y = -1;
		int x1 = 2;
		int y1 = 5;
		double m = 1.3;
		String n = "2654.02";
		
		Test a=new Test(x,y);
		Test b=new Test(x1,y1);
		Test c=new Test(m);
		Test d=new Test(n);
		
		System.out.println(x+"/"+y+" Change to a rational number string: "+a.toString());
		System.out.println(x1+"/"+y1+" Change to a rational number string: "+b.toString());
		System.out.println(m+" Change to a rational number string: "+c.toString());
		System.out.println(n+" Change to a rational number string: "+d.toString());
		System.out.println();
		
		
		System.out.println("The sum of a and B is: "+a.add(b));
		System.out.println("The minus value of a and B is: "+a.minus(b));
		System.out.println("The result of a times B is: "+a.multiplication(b));
		System.out.println("The result of a divided by B is: "+a.division(b));
		System.out.println();
		System.out.println("Output the larger numbers of "+a.toString() +" and "+ b.toString()+" : "+a.big(b));
		System.out.println("Is"+ b.toString()+" equals to "+c.toString()+" ? : "+b.equals(c));
	}
}

结果如下图所示

尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?

C语言的更加偏向于整个过程,而Java则是更多的编写类,要调用的时候方便,更加倾向于面向对象

尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。

别人如何复用你的代码?

在写有或复制有我的代码或导入有理数包的电脑中直接调用函数即可

别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?

我认为会影响,因为我每一个调用函数都是固定调用,相当于对输入数据的属性有一定的要求,所以是依赖我的有理数类的属性的。

有理数类的public方法是否设置合适?为什么有的方法设置为private?

当外界函数不需要调用或者说没必要调用某一个函数时,即设为private,如此代码中的找最大公约数的代码,就不需要外界调用,在构造有理数是调用此函数,而外界直接调用构造有理数的函数即可

posted @ 2020-10-04 11:03  孤海  阅读(105)  评论(0编辑  收藏  举报