Hello I cannot figure out how to do this program. I need to create a Class \"Rat
ID: 3535707 • Letter: H
Question
Hello I cannot figure out how to do this program. I need to create a Class "Rational". The purpose is to use a provided "RationalTest" program that is supposed to take the rational class and output the results and they must match the provided results.
It also must
1. how zero (0) is handled as a numerator with a denominator that is not 1.
2. checking the proper use of equals() and compareTo().
3. verification that the construtor handles negative numerators correctly if simplification of the ratio will happen.
THIS IS THE RATIONALTEST,java
public class RationalTest
{
private static void report(String fmt, Object... args)
{ System.out.printf(fmt, args); }
public static void main(String[] args)
{
Rational r0 = new Rational(0);
report("r0: %s%n", r0);
Rational r5 = new Rational(5);
report("r5: %s%n", r5);
Rational r05 = new Rational(0, 5);
report("r05: %s%n", r05);
Rational rhalf = new Rational(1, 2);
report("rhalf: %s%n", rhalf);
Rational rhalf5 = new Rational(5, 10);
report("rhalf5: %s%n", rhalf5);
report("rhalf == rhalf5: %s%n", rhalf.equals(rhalf5));
Rational rtwo5 = new Rational(10, 5);
report("rtwo5: %s%n", rtwo5);
Rational rnegn = new Rational(-3, 4);
report("rnegn: %s%n", rnegn);
Rational rnegd = new Rational(3, -4);
report("rnegd: %s%n", rnegd);
Rational rneg2 = new Rational(-3, -4);
report("rneg2: %s%n", rneg2);
report("rden0: ");
try {
Rational rden0 = new Rational(3, 0);
report("%s%n", rden0);
}
catch (IllegalArgumentException iae)
{ System.err.println(iae.getMessage()); }
Rational recip1 = rnegn.reciprocal();
report("recip1: %s%n", recip1);
Rational recip2 = rhalf5.reciprocal();
report("recip2: %s%n", recip2);
Rational recip3 = rtwo5.reciprocal();
report("recip3: %s%n", recip3);
Rational rtwothirds = new Rational(2, 3);
int cmp = rneg2.compareTo(rtwothirds);
String reln = (cmp < 0 ? "<" :="" cmp=""> 0 ? ">" : "="));
report("%s %s %s%n", rneg2, reln, rtwothirds);
cmp = rtwothirds.compareTo(rneg2);
reln = (cmp < 0 ? "<" :="" cmp=""> 0 ? ">" : "="));
report("%s %s %s%n", rtwothirds, reln, rneg2);
Rational rfourth = new Rational(1, 4);
Rational rsixth = new Rational(1, 6);
Rational sum1 = rtwothirds.plus(rfourth);
report("sum1: %s%n", sum1);
Rational sum2 = rtwothirds.plus(rsixth);
report("sum2: %s%n", sum2);
Rational sum3 = rnegn.plus(rnegd);
report("sum3: %s%n", sum3);
Rational diff1 = rtwothirds.minus(rfourth);
report("diff1: %s%n", diff1);
Rational diff2 = rtwothirds.minus(rsixth);
report("diff2: %s%n", diff2);
Rational diff3 = rnegn.minus(rnegd);
report("diff3: %s%n", diff3);
Rational prod1 = rtwothirds.times(rfourth);
report("prod1: %s%n", prod1);
Rational prod2 = rtwothirds.times(rsixth);
report("prod2: %s%n", prod2);
Rational prod3 = rnegn.times(rhalf);
report("prod3: %s%n", prod3);
Rational quot1 = rtwothirds.divide(rfourth);
report("quot1: %s%n", quot1);
Rational quot2 = rtwothirds.divide(rsixth);
report("quot2: %s%n", quot2);
Rational quot3 = rnegn.divide(rhalf);
report("quot3: %s%n", quot3);
report("quot4: ");
try {
Rational quot4 = rnegn.divide(r0);
report("%s%n", quot4);
}
catch (Exception e) { System.err.println(e.getMessage()); }
}
}
THESE ARE THE CORRECT RESULTS.
r0: 0
r5: 5
r05: 0
rhalf: 1/2
rhalf5: 1/2
rhalf == rhalf5: true
rtwo5: 2
rnegn: -3/4
rnegd: -3/4
rneg2: 3/4
rden0: Cannot construct a Rational with a zero (0) denominator
recip1: -4/3
recip2: 2
recip3: 1/2
3/4 > 2/3
2/3 < 3/4
sum1: 11/12
sum2: 5/6
sum3: -3/2
diff1: 5/12
diff2: 1/2
diff3: 0
prod1: 1/6
prod2: 1/9
prod3: -3/8
quot1: 8/3
quot2: 4
quot3: -3/2
quot4: Cannot construct a Rational with a zero (0) denominator
HERE IS WHAT I CREATED SO FAR.
public class Rational implements Comparable
{
private static Rationalzero = new Rational(0,1);
private int num;
private int den;
public Rational(int num) {
this.num = num;
this.den = 1;
}
public Rational(int num, int den) {
this.num = num;
this.den = den;
}
}
I'm lost so getting me through this would be greatly appreciated.
Explanation / Answer
ANSWER
FOLLOW THIS
class Rational
{
private:
int numerator;
int denominator;
public:
Rational(int num = 0, int den = 1);
void add(Rational r);
void sub(Rational r);
void mul(Rational r);
void div(Rational r);
void print();
void printFloat();
private:
void reduce();
};
Rational::Rational(int num, int den) : numerator(num), denominator(den)
{}
void Rational::add(Rational r)
{
numerator = numerator + r.numerator;
denominator = denomiator + r.denominator;
reduce();
}
void Rational::sub(Rational r)
{
numerator = numerator - r.numerator;
denominator = denomiator - r.denominator;
reduce();
}
void Rational::mul(Rational r)
{
numerator = numerator * r.numerator;
denominator = denomiator * r.denominator;
reduce();
}
void Rational::div(Rational r)
{
numerator = numerator * r.denominator;
denominator = denomiator * r.numerator;
reduce();
}
void Rational::print()
{
cout << numerator << "/" << denominator;
}
void Rational::printFloat()
{
cout << a / static_cast<float>( b );
}
I will leave how to reduce the fractions for you to do.
void Rational::reduce()
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.