Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following class: public class Fraction implements Comparable{ public int getNum() { return num; } public int getDenom(){ return denom; } public double doubleValue(){

Consider the following class:

public class Fraction implements Comparable{

public int getNum() {

return num;

}

public int getDenom(){

return denom;

}

public double doubleValue(){

return (double)num / denom;

}

< Other constructors and methods not shown >

private int num, denom;

}

Which of the following would appropriately implement a compareTo method, required by the Comparable interface?

I public int compareTo(Fraction other){

return getNum() * other.getDenom()

getDenom() * other.getNum();

}

II public int compareTo(Object other){

double x = doubleValue();

double y = ((Fraction)other).doubleValue();

if (x < y) return 1;

else if (x > y) return 1;

else return 0;

}

III public int compareTo(Object other){

return (int)(doubleValue()

((Fraction)other).doubleValue());

}

(A) I only

(B) II only

(C) I and II only

(D) II and III only

(E) I, II, and III

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions