Question

Adapt your Rational class : public class Rational { private int num; private int denom; public...

Adapt your Rational class :

public class Rational {

    private int num;
    private int denom;

    public Rational() {
        num = 0;
        denom = 1;
    }

    public Rational(int num, int denom) {
        this.num = num;
        this.denom = denom;
    }

    int getNum() {
        return num;
    }

    int getDenom() {
        return denom;
    }

    public Rational add(Rational rhs) {
        return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom);
    }

    public Rational subtract(Rational rhs) {
        return new Rational(num * rhs.denom - rhs.num * denom, denom * rhs.denom);
    }

    public Rational multiply(Rational rhs) {
        return new Rational(num * rhs.num, denom * rhs.denom);
    }

    public Rational divide(Rational rhs) {
        return new Rational(num * rhs.denom, rhs.num * denom);
    }

    public String toString() {
        return num + "/" + denom;
    }

    public static void main(String[] args) {
        Rational r1 = new Rational(1, 2); // 1/2
        Rational r2 = new Rational(3, 4); // 3/4
        Rational result = new Rational();
        result = r1.add(r2);
        System.out.println(result);
// etc
    }
}

to implement the Comparable interface :

public interface Comparable <T>

int compareTo(T rhs);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey here is your answer :

public class Rational implements Comparable<Rational>{

private int num;
private int denom;

public Rational() {
num = 0;
denom = 1;
}

public Rational(int num, int denom) {
this.num = num;
this.denom = denom;
}

int getNum() {
return num;
}

int getDenom() {
return denom;
}

public Rational add(Rational rhs) {
return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom);
}

public Rational subtract(Rational rhs) {
return new Rational(num * rhs.denom - rhs.num * denom, denom * rhs.denom);
}

public Rational multiply(Rational rhs) {
return new Rational(num * rhs.num, denom * rhs.denom);
}

public Rational divide(Rational rhs) {
return new Rational(num * rhs.denom, rhs.num * denom);
}

public String toString() {
return num + "/" + denom;
}
  
// compareto method should return a negative integer(usually -1), if the current triggering object is less than the passed one,
// and positive integer (usually +1) if greater than, and 0 if equal.
  
public int compareTo(Rational r){  
   Rational a= this;
   int lhs=a.num * r.num;
   int rhs= a.denom *r.denom;
  
   if(lhs<rhs)return -1;
   if(lhs>rhs)return +1;
   else return 0;
}

public static void main(String[] args) {
Rational r1 = new Rational(1, 2); // 1/2
Rational r2 = new Rational(3, 4); // 3/4
Rational result = new Rational();
result = r1.add(r2);
System.out.println(result);
// etc
}
}

Add a comment
Know the answer?
Add Answer to:
Adapt your Rational class : public class Rational { private int num; private int denom; public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

  • Having to repost this as the last answer wasn't functional. Please help In the following program...

    Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...

  • public class Rational { // PUT PRIVATE DATA FIELDS HERE /** * The default constructor for...

    public class Rational { // PUT PRIVATE DATA FIELDS HERE /** * The default constructor for objects of class Rational. Creates the rational number 1. */ public Rational() { // ADD CODE TO THE CONSTRUCTOR } /** * The alternate constructor for objects of class Rational. Creates a rational number equivalent to n/d. * @param n The numerator of the rational number. * @param d The denominator of the rational number. */ public Rational(int n, int d) { // ADD...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • Templates Apartment.java package hwk7; public class Apartment {    int numOfApartments; // the number of apartments...

    Templates Apartment.java package hwk7; public class Apartment {    int numOfApartments; // the number of apartments of this type    Room[] rooms; // rooms in this type of apartment       Apartment(int numOfApartments, Room[] rooms) {        this.numOfApartments = numOfApartments;        this.rooms = rooms;    }       // Return the window orders for one apartment of this type as TotalOrder object    TotalOrder orderForOneUnit() {        // TODO    }       // Return the window...

  • 2)- MyList Modification (Java) Modify the MyList class that you wrote for Programming Challenge 1 so...

    2)- MyList Modification (Java) Modify the MyList class that you wrote for Programming Challenge 1 so that the type parameter T should accept any type that implements the Comparable interface. Test the class in a program that creates one instance of MyList to store Integers, and another instance to store Strings. Mylist.java import java.util.*; import java.math.BigDecimal; public class MyList <T extends Number> {    private ArrayList <T> num = new ArrayList<>();    static MyList<Number> list = new MyList<>();    public...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap];...

    public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap]; index =-1; } public boolean isEmpty(){ return index == -1; } public boolean isFull(){ return index==data.length -1; } public NumStack pop(){ if(!isEmpty()) data[index--]=null; return this; } public int size(){ return index+1; } public Integer top(){ if(isEmpty()) return null; return data[index]; } public NumStack push(int num){ if(index < data.length-1) data[++index]=num; return this; } public int compareTo(NumStack s){} } public int compareTo(NumStack s) compares two stack...

  • class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...

    class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT