//********************************************************************
// RationalNumber.java Author: Lewis/Loftus
//
// Represents one rational number with a numerator and denominator.
//********************************************************************
public class RationalNumber
{
private int numerator, denominator;
//-----------------------------------------------------------------
// Constructor: Sets up the rational number by ensuring a nonzero
// denominator and making only the numerator signed.
//-----------------------------------------------------------------
public RationalNumber (int numer, int denom)
{
if (denom == 0)
denom = 1;
// Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
//-----------------------------------------------------------------
// Returns the numerator of this rational number.
//-----------------------------------------------------------------
public int getNumerator ()
{
return numerator;
}
//-----------------------------------------------------------------
// Returns the denominator of this rational number.
//-----------------------------------------------------------------
public int getDenominator ()
{
return denominator;
}
//-----------------------------------------------------------------
// Returns the reciprocal of this rational number.
//-----------------------------------------------------------------
public RationalNumber reciprocal ()
{
return new RationalNumber (denominator, numerator);
}
//-----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
//-----------------------------------------------------------------
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);
}
//-----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
//-----------------------------------------------------------------
public RationalNumber subtract (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);
}
//-----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
//-----------------------------------------------------------------
public RationalNumber multiply (RationalNumber op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new RationalNumber (numer, denom);
}
//-----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
//-----------------------------------------------------------------
public RationalNumber divide (RationalNumber op2)
{
return multiply (op2.reciprocal());
}
//-----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
//-----------------------------------------------------------------
public boolean equals (RationalNumber op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}
//-----------------------------------------------------------------
// Returns this rational number as a string.
//-----------------------------------------------------------------
public String toString ()
{
String result;
if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
//-----------------------------------------------------------------
// Reduces this rational number by dividing both the numerator
// and the denominator by their greatest common divisor.
//-----------------------------------------------------------------
private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
//-----------------------------------------------------------------
// Computes and returns the greatest common divisor of the two
// positive parameters. Uses Euclid's algorithm.
//-----------------------------------------------------------------
private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA例题详解 课例
共249个文件
java:166个
class:44个
gif:22个
需积分: 3 27 下载量 91 浏览量
2009-12-18
17:29:05
上传
评论
收藏 1.69MB ZIP 举报
温馨提示
适合JAVA初学者 全部实例编程 感觉不错 老师给的 据说是一美国教材的课例
资源推荐
资源详情
资源评论
收起资源包目录
JAVA例题详解 课例 (249个子文件)
jeopardy.au 268KB
KochSnowflake.class 3KB
BorderDemo.class 3KB
KochPanel.class 2KB
DotsPanel.class 2KB
RubberLinesPanel.class 2KB
LeftRightPanel.class 2KB
RubberLinesPanel$LineListener.class 2KB
LabelDemo.class 2KB
Purchase.class 2KB
IceCream.class 1KB
NestedPanels.class 1KB
RocketPanel.class 1KB
Circle.class 1KB
Quadratic.class 1KB
RandomNumbers.class 1KB
TiledPictures.class 1KB
CircleStats.class 1KB
DotsPanel$DotsListener.class 1KB
IceCream$Flavor.class 1KB
Authority.class 1KB
LeftRightPanel$ButtonListener.class 1KB
LayoutDemo.class 1KB
BoxPanel.class 1KB
SplatPanel.class 1KB
Snowman.class 1014B
BorderPanel.class 950B
GridPanel.class 899B
FlowPanel.class 884B
Rocket.class 812B
RubberLines.class 807B
LeftRight.class 797B
Splat.class 768B
Dots.class 764B
NameTag.class 750B
IntroPanel.class 688B
Einstein.class 682B
Roses.class 656B
Addition.class 581B
Advice.class 543B
Thought.class 539B
Magazine.class 449B
RubberLinesPanel$1.class 199B
LeftRightPanel$1.class 193B
DotsPanel$1.class 178B
test.dat 520B
aquarius.dat 430B
everest.gif 63KB
goat.gif 25KB
toucan.gif 11KB
world.gif 7KB
hammock.gif 4KB
worldmap.gif 2KB
duke.gif 2KB
circuit.gif 2KB
arrowDown.gif 1KB
arrowUp.gif 1KB
arrowLeft.gif 1KB
arrowRight.gif 1KB
decrease.gif 1KB
decreasePressed.gif 1KB
increasePressed.gif 1KB
increase.gif 1KB
play.gif 907B
stop.gif 873B
devil.gif 663B
lightBulbOn.gif 499B
lightBulbOff.gif 477B
happyFace.gif 241B
Einstein.html 594B
Snowman.html 540B
KochSnowflake.html 219B
TiledPictures.html 219B
urls.inp 142B
RationalNumber.java 6KB
JukeBoxControls.java 5KB
SlideColorPanel.java 4KB
PigLatinTranslator.java 3KB
KochPanel.java 3KB
DirectionPanel.java 3KB
Maze.java 3KB
RubberLinesPanel.java 3KB
KochSnowflake.java 3KB
Circle.java 3KB
QuoteOptionsPanel.java 3KB
BorderDemo.java 3KB
LightBulbControls.java 3KB
OffCenterPanel.java 2KB
DotsPanel.java 2KB
Account.java 2KB
CDCollection.java 2KB
Contact.java 2KB
Question.java 2KB
ReboundPanel.java 2KB
MagazineList.java 2KB
BoxesPanel.java 2KB
StyleOptionsPanel.java 2KB
Searching.java 2KB
TowersOfHanoi.java 2KB
FahrenheitPanel.java 2KB
共 249 条
- 1
- 2
- 3
资源评论
hejing521
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 微信自动抢红包APP.zip
- 微信抢红包神器-红包鸟(可以抢多种情况下的红包,支持手机息屏抢红包、屏蔽群抢红包).zip
- 微信抢红包,支持xposed与免root,xposed支持最新版,免root需自己实现.zip
- 一维卷积神经网络英语电影评论情感分类项目功能实现-Embedding层
- 微信 自动抢红包 插件外挂(Android),实现了监控通知栏,自动点击红包等功能,帮助大家快速抢到红包.zip
- 满满的干货:分享二十个Python学习资源材料.zip
- USD ISO14229-1-2013 中文版
- STM32单片机FPGA毕设电路原理论文报告模糊控制器在无线监控机群系统中的应用
- OpenWrt-Toolchain-ipq
- STM32单片机FPGA毕设电路原理论文报告模糊控制逆变弧焊电源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功