Java教程

Java有理数类的设计

本文主要是介绍Java有理数类的设计,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

仿照BigDecimal类以面向对象的方式设计有理数类。

完整的有理数类的代码。

package RationalNumber;

public class RationalNumber {
	private int numerator;
	private int denominator;

	private boolean isInt(String str) {
		int i = 0;
		if (str.charAt(0) == '-')
			i++;
		for (; i < str.length(); i++) {
			if (str.charAt(i) < '0' || str.charAt(i) > '9')
				return false;
		}
		return true;
	}

	public RationalNumber(String str) {
		if (isInt(str)) {
			this.numerator = Integer.valueOf(str);
			this.denominator = 1;
		} else if (str.indexOf(".") != -1) {
			String str1 = str.substring(0, str.indexOf("."));
			String str2 = str.substring(str.indexOf(".") + 1, str.length());
			if (isInt(str1) && isInt(str2)) {
				this.numerator = Integer.valueOf(str1);
				this.denominator = Integer.valueOf(str2);
			}

			this.numerator *= Math.pow(10, str2.length());
			this.denominator *= Math.pow(10, str2.length());
			fractionReduction();

		} else if (str.indexOf("/") != -1) {

			String str1 = str.substring(0, str.indexOf("/"));
			String str2 = str.substring(str.indexOf("/") + 1, str.length());

			if (isInt(str1) && isInt(str2)) {
				this.numerator = Integer.valueOf(str1);
				this.denominator = Integer.valueOf(str2);
				if (this.denominator == 0) {
					System.out.print("分母不能为零");
				} else
					fractionReduction();
			} else {
				System.out.print("输入错误");

			}
		}

	}

	public int gcd(int a, int b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	public void fractionReduction() {
		int flag = 1;
		if (this.numerator < 0) {
			this.numerator *= -1;
			flag *= -1;
		}
		if (this.denominator < 0) {
			this.denominator *= -1;
			flag *= -1;
		}
		int mgcd = gcd(this.numerator, this.denominator);
		this.numerator = flag * this.numerator / mgcd;
		this.denominator = this.denominator / mgcd;
	}

	public RationalNumber add(RationalNumber x) {
		this.numerator = this.numerator * x.denominator + this.denominator * x.numerator;
		this.denominator = this.denominator * x.denominator;
		fractionReduction();
		return this;
	}

	public RationalNumber subtract(RationalNumber x) {
		this.numerator = this.numerator * x.denominator - this.denominator * x.numerator;
		this.denominator = this.denominator * x.denominator;
		fractionReduction();
		return this;
	}

	public RationalNumber multply(RationalNumber x) {

		this.numerator = this.numerator * x.numerator;
		this.denominator = this.denominator * x.denominator;
		fractionReduction();
		return this;
	}

	public RationalNumber divide(RationalNumber x) {
		this.numerator = this.numerator * x.denominator;
		this.denominator = this.denominator * x.numerator;
		return this;
	}

	public String toString() {
		if (this.denominator == 1) {
			return this.numerator + "";
		} else {
			return this.numerator + "/" + this.denominator;
		}
	}
}

给出你的测试代码。要在与有理数类不同包的其他类中调用有理数类。

package RationalNumber;

public class Main {

	public static void main(String[] args) {
		int n = 2;
		RationalNumber num[] = new RationalNumber[10];
		num[0] = new RationalNumber("1");
		num[1] = new RationalNumber("5/8");
		System.out.println("测试数据:\n");
		for (int i = 0; i < 2; i++) {
			System.out.println(num[i].toString() + "\n");
		}
		System.out.println("加法\n");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.println(num[i].toString() + "+" + num[j].toString() + "=" + num[0].add(num[1]) + "\n");
			}

		}
		System.out.println("减法\n");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.println(num[i].toString() + "-" + num[j].toString() + "=" + num[0].subtract(num[1]) + "\n");
			}

		}
		System.out.println("乘法\n");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.println(num[i].toString() + "*" + num[j].toString() + "=" + num[0].multply(num[1]) + "\n");
			}

		}
		System.out.println("除法\n");
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.println(num[i].toString() + "/" + num[j].toString() + "=" + num[0].divide(num[1]) + "\n");
			}

		}
	}
}

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

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

别人如何复用你的代码?

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

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

你的类里面有static属性或方法吗?如果有,为什么要设置为static的?

可选:简单说明你的设计的有理数类是否是不可变类?如果想要设计成不可变类,主要从哪几个方面着手?

说明:怎么查看BigDecimal的设计?

使用JDK文档查看BigDecimal的公共方法(入参、返回值、功能)。

进阶:在Eclipse查看BigDecimal的代码。先看其属性,再看其公共方法的声明。可以不用详细看代码实现。

这篇关于Java有理数类的设计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!