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....

  • 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...

  • 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...

  • 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 =...

  • public class Song { private String title; private String artist; private int duration; public Song() {...

    public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...

  • 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...

  • Java. What is the output of this code? Ace.java public class Ace { private double a;...

    Java. What is the output of this code? Ace.java public class Ace { private double a; public Ace ( double x) {       a = x; } public void multiply (double f) {       a *= x;         } public String toString () {       return String.format ("%.3f", x); AceDem.java public class AceDemo { public static void main(String args[]) {    Ace card = new Ace (2.133); card.multiply (.5); System.out.println (card); } }

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

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
ADVERTISEMENT