***QUICK*** Java HELP! :D

mztykal

Diamond Member
Apr 21, 2000
6,709
48
91
This is for a friend. And since I took JAVA a year ago, I don't remember almost anything of it. So can you please help her out? Thanks!

Modify the rational class from chapter 4 so that it implements the comparable interface. To perform the comparison, compute an equivalent floating point value for the numerator and denominator for both rational objects, then compare them using a tolerance value of 0.0001. Write a main driver to test your modifications.

public class Rational
{
private int numerator, denominator;

public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;

if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}

numerator = numer;
denominator = denom;

reduce();
}

public Rational add (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return new Rational (sum, commonDenominator);
}

public Rational subtract (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;

return new Rational (difference, commonDenominator);
}

public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();

return new Rational (numer, denom);
}

public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}

public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}

public int getNumerator ()
{
return numerator;
}

public int getDenominator ()
{
return denominator;
}

public boolean equals (Rational op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}

public String toSrting ()
{
String result;

if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}

private void reduce ()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);

numerator = numerator / common;
denominator = denominator / common;
}
}

private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;

return num1;
}
}

Um...there you go...

:D

BTW, it's a friends friend, and he just left. So can ya'll help her out here? :D
 

mztykal

Diamond Member
Apr 21, 2000
6,709
48
91
Here's the example of the comparable interface:

if (obj1.compareto(obj2) < 0)

system.out.println ("obj1 is less than obj2");

Thanks again!
 

mztykal

Diamond Member
Apr 21, 2000
6,709
48
91
Ack! No one? She said it's due at 8 in the morning and it's 9pm now. She has a "C" and needs a "B" or she has to take the class over again. And before you post "she should do her own work". Be nice for a change and stop bein a ass. :D
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
public interface Comparable
{

boolean less(Object m);
boolean greater(Object m);
boolean lessEqual(Object m);
boolean greaterEqual(Object m);
}



so your class defintion needs to say:

public class Rational implements Comparable
{
...
}


then you need to define the methods above and write the code for them. Since you are in college i am assuming you know how to tell if 1 rational number is greater then another etc.


thats it, its very simple.
 

mztykal

Diamond Member
Apr 21, 2000
6,709
48
91
She said it's the wrong format. Blah, guess you can't please everyone. She's probably wrong or she didn't give the whole problem. Doesn't matter, my friend was the one mackin on her. LOL. Thanks again though. :D
 

mztykal

Diamond Member
Apr 21, 2000
6,709
48
91
I don't know java, hence why I asked. Everything I learned was forgotten trying to learn PHP. :(

I kinda get it, but not enough to explain to her. Plus I don't wanna argue with her. So I just told her I had to go. And left...PM me if you want her AIM name so you can explain it to her. :D
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: mztykal
I don't know java, hence why I asked. Everything I learned was forgotten trying to learn PHP. :(

I kinda get it, but not enough to explain to her. Plus I don't wanna argue with her. So I just told her I had to go. And left...PM me if you want her AIM name so you can explain it to her. :D

Man oh man is she a dumbass, soo thick headed, wont pay attention to what i am saying at all.

you shouldnt be friends with her your IQ is sure to diminish.
 

mztykal

Diamond Member
Apr 21, 2000
6,709
48
91
Hahahaha, I guess helping my friend out by helping her was a "no no" huh? :D

Oh well, thanks for trying man. I really appreciate it. :D