In a language that supports operator overloading, build support for rational num
ID: 3883118 • Letter: I
Question
In a language that supports operator overloading, build support for rational numbers. Each number should be represented internally as a (numerator, denominator) pair in simplest form, with a positive denominator. Your code should support unary negation and the four standard arithmetic operators. Also, create a conversion routine that accepts two floating-point parameters - a value and an error bound - and returns the simplest (smallest denominator) rational number within the given error bound of the given value. The application must be submitted as a NetBeans project or Microsoft Visual Studio (VS 2013 or VS 2015) project which includes the source codes of your program. In addition to the NetBeans project or Microsoft Visual Studio project, use Microsoft Word to provide OLNY ONE assignment/user-documentation file. Documentation should include: A report which clearly describes the design and description of your software components. A logical flow diagram describes the logical flow structure of the application using any diagramming/graphic software such as Microsoft Visio. A logical flow diagram for every method, functions, and procedures defined in the application using any diagramming/graphic software such as Microsoft Visio. A description of any input files to test the application. (a file with valid data and a file with invalid data) A detailed instruction of how the program can be compiled and executed. Screenshots of execution sessions processing valid inputs and invalid inputs.Explanation / Answer
Main.java
public class Main
{
private int numer=0, denomin=0;
public Main (int numer, int denom)
{
if (denom == 0)
denom = 1;
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numer = numer;
denomin = denom;
reductions();
}
public int getNumer ()
{
return numer;
}
public int getDenomin ()
{
return denomin;
}
public Main reci ()
{
return new Main (denomin, numer);
}
public Main plus (Main r1)
{
int commDeno = denomin * r1.getDenomin();
int num1 = numer * r1.getDenomin();
int num2 = r1.getNumer() * denomin;
int addition = num1 + num2;
return new Main (addition, commDeno);
}
public Main minus (Main r1)
{
int commDeno = denomin * r1.getDenomin();
int num1 = numer * r1.getDenomin();
int num2 = r1.getNumer() * denomin;
int diff = num1 - num2;
return new Main (diff, commDeno);
}
public Main mult (Main r1)
{
int numer = numer * r1.getNumer();
int denom = denomin * r1.getDenomin();
return new Main (numer, denom);
}
public Main divisions (Main r1)
{
return mult (r1.reci());
}
public boolean eq (Main r1)
{
return ( numer == r1.getNumer() &&
denomin == r1.getDenomin() );
}
public String toString ()
{
String res;
if (numer == 0)
res = "0";
else
if (denomin == 1)
res = numer + "";
else
res = numer + "/" + denomin;
return res;
}
private void reductions ()
{
if (numer != 0)
{
int commn = GCD (Math.abs(numer), denomin);
numer = numer / commn;
denomin = denomin / commn;
}
}
private int GCD (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
}
Rate an upvote.....Thankyou
Hope this helps....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.