Question
I'm having trouble implenting what this project is asking for. There are 4 required .java files to be submitted. The Assigment: Given Rational class, implement
I'm having trouble implenting what this project is asking for. There are 4 required .java files to be submitted.
The Assigment:
Given Rational class, implement the MixedNumber class with the following specification.
A MixedNumber has a long and a fraction as data fields.
MixedNumber() default constructor should create 0 0/1
MixedNumber(int i, int n, int d) overloaded constructor should create a MixedNumber object with given parameters in reduced form. ie.
MixedNumber m = new Mixedumber(5,10,4);
Should return ( 7 )
MixedNumber(int i, Rational r) overloaded constructor should create a MixedNumber object with given parameters in reduced form. ie.
MixedNumber m = new Mixedumber(5,new Rationale(10,4));
Should return ( 7 )
Add, subtract, multiply, and divide methods. All methods should return a MixedNumber object in reduced form
toString() method should return a string in format of ( 7 )
convert() method should return a MixedNumber argument as a Rational number (fraction). ie.
7 converted to a fraction is 15/2
STAGE 2 | Test Program
Test your class with the given TestMixedNumbers,java program. Output should be:
m1 is: 9
m2 is: 6
m1 + m2 is: 16
m1 - m2 is: 2
m1 * m2 is: 64
m1 / m2 is: 1 11/27
9
STAGE 3 | Driver Program
Create a driver program called SortMixNumbers.java
Create an array of 5 MixedNumber objects and initialize them with random integer values as follows:
Data field integer: 1 <= integer <= 9
Data field fraction: ( 5 <= numerator <= 20)
( 1 <= denominator <= 10)
3) print the list
4) sort the list in ascending order
5) print the list
Rational.java:
public class Rational implements Comparable
// Data fields for numerator and denominator
private long numerator = 0;
private long denominator = 1;
/** Construct a rational with default properties */
public Rational() {
this(0, 1);
}
/** Construct a rational with specified numerator and denominator */
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;
}
/** Find GCD of two numbers */
private static long gcd(long n, long d) {
long n1 = Math.abs(n);
long n2 = Math.abs(d);
int gcd = 1;
for (int k = 1; k <= n1 && k <= n2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}
return gcd;
}
/** Return numerator */
public long getNumerator() {
return numerator;
}
/** Set numerator */
public void setNumerator(long n)
{
numerator = n;
}
/** Return denominator */
public long getDenominator() {
return denominator;
}
/** Set denominator */
public void setDenominator(long d)
{
denominator = d;
}
/** Add a rational number to this rational */
public Rational add(Rational secondRational) {
long n = numerator * secondRational.getDenominator() +
denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}
/** Subtract a rational number from this rational */
public Rational subtract(Rational secondRational) {
long n = numerator * secondRational.getDenominator()
- denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}
/** Multiply a rational number to this rational */
public Rational multiply(Rational secondRational) {
long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}
/** Divide a rational number from this rational */
public Rational divide(Rational secondRational) {
long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.numerator;
return new Rational(n, d);
}
@Override
public String toString() {
if (denominator == 1)
return numerator + "";
else
return numerator + "/" + denominator;
}
@Override // Override the equals method in the Object class
public boolean equals(Object other) {
if ((this.subtract((Rational)(other))).getNumerator() == 0)
return true;
else
return false;
}
@Override // Implement the compareTo method in Comparable
public int compareTo(Rational o) {
if (this.subtract(o).getNumerator() > 0)
return 1;
else if (this.subtract(o).getNumerator() < 0)
return -1;
else
return 0;
}
}
TestMixedNumbers.java:
public class TestMixedNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
MixedNumber m1=new MixedNumber(7,5,2);
MixedNumber m2=new MixedNumber(6,3,4);
System.out.println("m1 is: " + m1);
System.out.println("m2 is: " + m2);
System.out.println("m1 + m2 is: " + m1.add(m2));
System.out.println("m1 - m2 is: " + m1.subtract(m2));
System.out.println("m1 * m2 is: " + m1.multiply(m2));
System.out.println("m1 / m2 is: " + m1.divide(m2));
if(m1.compareTo(m2) >0 )
System.out.println(m1);
else
System.out.println(m2);
}
I need help implenting MixedNumbers.java and SortMixedNumbers.java. Any help would be appreciated!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started