博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
multiple Rational objects
阅读量:5969 次
发布时间:2019-06-19

本文共 2710 字,大约阅读时间需要 9 分钟。

hot3.png

//Driver to exercise the use of multiple Rational objectspublic class RationalTester {public static void main(String[] args){	RationalNumber r1=new RationalNumber(6,8);	RationalNumber r2=new RationalNumber(1,3);	RationalNumber r3,r4,r5,r6,r7;	System.out.println("First rational number:"+r1);	System.out.println("Second rational number:"+r2);	if(r1.isLike(r2))		System.out.println("r1 and r2 are euqal.");	else		System.out.println("r1 and r2 are not equal.");	r3=r1.reciprocal();	System.out.println("The reciprocal of r1 is :"+r3);	r4=r1.add(r2);	r5=r1.subract(r2);	r6=r1.multiply(r2);	r7=r1.divide(r2);	System.out.println("r1+r2:"+r4);	System.out.println("r1-r2:"+r5);	System.out.println("r1*r3:"+r6);	System.out.println("r1/r2:"+r7);}}//represents one rational number with a numberator and denominatorpublic class RationalNumber {private int numerator,denominator;public RationalNumber(int numer,int denom){	if(denom==0)		denom=1;	if(denom<0){		numer=numer*-1;		denom=denom*-1;	}	numerator=numer;	denominator=denom;	reduce();}public int getDenominator(){	return denominator;}public RationalNumber reciprocal(){	return new RationalNumber(denominator,numerator);}public RationalNumber add(RationalNumber op2){	int commonDenominator=denominator*op2.getDenominator();	int numerator1=numerator*op2.getDenominator();	int numerator2=op2.getNumerator()*denominator;	int sum=numerator1+numerator2;	return new RationalNumber(sum,commonDenominator);}public RationalNumber subract(RationalNumber op2){	int commonDenominator=denominator*op2.getDenominator();	int numerator1=numerator*op2.getDenominator();	int numerator2=op2.getNumerator()*denominator;	int difference=numerator1-numerator2;	return new RationalNumber(difference,commonDenominator);}public RationalNumber multiply(RationalNumber op2){	int numer=numerator*op2.getNumerator();	int denom=denominator*op2.getDenominator();	return new RationalNumber(numer,denom);}public RationalNumber divide(RationalNumber op2){	return multiply(op2.reciprocal());}public boolean isLike(RationalNumber op2){	return(numerator==op2.getNumerator()&&denominator==op2.getDenominator());}public String toString(){	String result;	if(numerator==0)		result="0";		else			if(denominator==1)				result=numerator+" ";			else				result =numerator+"/"+denominator;		return result;}private void reduce(){	if(numerator!=0){		int common=gcd(Math.abs(numerator),denominator);		numerator=numerator/common;		denominator=denominator/common;	}}private int gcd(int num1,int num2){	while(num1!=num2)		if(num1>num2)			num1=num1-num2;		else			num2=num2-num1;	return num1;}private int getNumerator() {	// TODO Auto-generated method stub	return numerator;}}

转载于:https://my.oschina.net/yanjianhai/blog/127333

你可能感兴趣的文章
mac系统下git、mysql、nginx、php的环境搭建
查看>>
JavaScript面向对象编程——Array类型
查看>>
使用阿里云ACM简化你的Spring Cloud微服务环境配置管理
查看>>
【“探探”为例】手把手教你用最少的代码实现各种“机器人”
查看>>
js实现txt/excel文件下载
查看>>
mybatis学习笔记
查看>>
初学微信小程序 TodoList
查看>>
JDK10都发布了,nio你了解多少?
查看>>
Android P FAQ第一弹:非SDK管控特性
查看>>
node+express+mongDB实现简单登录注册
查看>>
个人博客 SEO 优化(2):站内优化
查看>>
从源码全面剖析 React 组件更新机制
查看>>
十个你需要在 PHP 7 中避免的坑
查看>>
spring-MVC源码解读(一)
查看>>
[译]php和curl_multi_exec
查看>>
java对象关系映射ROM
查看>>
Hystrix指标窗口实现原理
查看>>
【419天】跃迁之路——程序员高效学习方法论探索系列(实验阶段176-2018.03.31)...
查看>>
LeetCode 200. Number of Islands
查看>>
做一个合格的前端,gulp资源大集合
查看>>