有理数类 Java

public class Rational extends Number implements Comparable {

private long numerator;// 分子
private long denominator;// 分母

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
Rational rational1 = new Rational(1, 10);
Rational rational2 = new Rational(1, -10);
System.out.println(rational1.add(rational2));
System.out.println(rational1.subtract(rational2));
System.out.println(rational1.multiple(rational2));
System.out.println(rational1.divide(rational2));
}

public Rational() {

// TODO Auto-generated constructor stub
this(0, 1);

}

public Rational(long numerator, long denominator) {

long gcd = gcd(numerator, denominator);
this.numerator = (denominator > 0 ? 1 : -1) * numerator / gcd;
this.denominator = Math.abs(denominator) / gcd;
}

public static long gcd(long a, long b) {

long n1 = Math.abs(a);
long n2 = Math.abs(b);
long remainder = n1 % n2;
while (remainder > 0) {
n1 = n2;
n2 = remainder;
remainder = n1 % n2;
}
return n2;
}

public long getNumerator() {

return numerator;
}

public long getDenominator() {

return denominator;
}

public Rational add(Rational secondRational) {

long n = numerator * secondRational.getDenominator() + denominator
* secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational subtract(Rational secondRational) {

long n = numerator * secondRational.getDenominator() - denominator
* secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational multiple(Rational secondRational) {

long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational divide(Rational secondRational) {

long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.getNumerator();
return new Rational(n, d);
}

@Override
public boolean equals(Object obj) {

// TODO Auto-generated method stub
if (this.subtract((Rational) obj).getNumerator() == 0) {
return true;
}
else {
return false;
}
}

@Override
public String toString() {

// TODO Auto-generated method stub
if (denominator == 1) {
return String.valueOf(numerator);
}
else {
return numerator + "/" + denominator;
}
}

@Override
public int intValue() {

// TODO Auto-generated method stub
return (int) doubleValue();
}

@Override
public long longValue() {

// TODO Auto-generated method stub
return (long) doubleValue();
}

@Override
public float floatValue() {

// TODO Auto-generated method stub
return (float) doubleValue();
}

@Override
public double doubleValue() {

// TODO Auto-generated method stub
return numerator * 1.0 / denominator;
}

@Override
public int compareTo(Object o) {

// TODO Auto-generated method stub
if (this.subtract((Rational) o).getNumerator() > 0) {
return 1;
}
else if (this.subtract((Rational) o).getNumerator() < 0) {
return -1;
}
else {
return 0;
}
}
}

时间: 2024-10-27 09:29:08

有理数类 Java的相关文章

有理数类 Java BigInteger实现

import java.math.BigInteger; public class Rational extends Number implements Comparable { private BigInteger numerator;// 分子 private BigInteger denominator;// 分母 /** * @param args */ public static void main(String[] args) { // TODO Auto-generated met

实现有理数类(Java)

/*Java中没有提供表示有理数的类. 有理数类 */ public class Rational { private long numerator = 0; //分子 private long denominator = 1; //分母 //构造函数 public Rational() { this(0,1); } public Rational(long numerator, long denominator) { long gcd = gcd(numerator,denominator);

第四届蓝桥杯javaC组_有理数类

/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: 蓝桥杯赛题 * 作    者:   彭俊豪 * 完成日期:   2016   年 04月 01日 * 版 本 号:      001 * 对任务及求解方法的描述部分 * 问题描述: 有理数就是可以表示为两个整数的比值的数字.一般情况下,我们用近似的小数表示.但有些时候,不允许出现误差,必须用两个整数

类 java.util.Scannar方法

类 java.util.Scannar方法 ·Scannar (InputStream ln):用给定的输人流创建一个Scanner对象. ·String nextLlne():读取输入的下一行内容. ·String next():读取输入的下一个单词(以空格作为分隔符). ·int nextint() ·double nextDauble():读取并转换下一个表示整数或浮点数的字符序列. ·boolean hasNext():检测输入中是否还有其它单词. ·boolean hasNextint

加密解密工具类(Java,DES)

一个Java版的DES加密工具类,可以用来进行网络数据传输加密,保存密码的时候进行加密. import java.security.Key; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.I

有理数类

1 public class Rational { 2 3 private int numerator; 4 private int denominator; 5 6 public Rational(int aNumerator, int aDenominator){ 7 int r = gcd(aDenominator, aNumerator); 8 numerator = aNumerator / r; 9 denominator = aDenominator / r; 10 sign();

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

要实现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 publi

Java学习记录(补充八:Date类;Java流(Stream),文件(File)和IO)

Date类,Calendar类package Box1; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; //Date类 public class DateTest { public static void main(String[] args) { Date

二维码生成工具类java版

注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Shape; import java.awt.geom.RoundRectangle2D; import java.a