How to use fractions?

I'm a beginner at Java and I'm currently doing it in school in a programming course. Over the weekend our class have got a homework which consists of some different things. One of them is to compare two fractions and then use compareTo to decide which one of them is the biggest. The problem is that we haven't learned yet how to use fractions.

The only thing I wonder is how to create a simple fraction. Whenever I try to do what I have read on the Internet in guides it doesn't work (it say: cannot resolve class) or the code is too advanced and long for a beginner like me.

I would really appreciate if someone could help me with how to create fractions.

P.S. English isn't my main language so I'm sorry if I have explained my question badly D.S.

[782 byte] By [_Benni_a] at [2007-11-27 6:13:18]
# 1

Does your teacher want you to make a Fraction class?

public class Fraction implements Comparable<Fraction>

{

private int numerator;

private int denominator;

public Fraction(int num, int den)

{

numerator = num;

denominator = den;

}

public int compareTo(Fraction frac)

{

//you need to implement this method

}

}

CaptainMorgan08a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 2
And here's a description of what the compareTo() method should do.[url= http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html#compareTo(T)]int compareTo()[/url]
CaptainMorgan08a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks for the help, but what should I type instead of <Fraction>?
_Benni_a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks for the help, but what should I type instead

> of <Fraction>?

You need that part because of [url=http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html]generics[/url]. It's basically telling the compareTo() method what it needs to compare against. Generics were added in version 1.5.0, so if you are using an older version than that you don't need it.

CaptainMorgan08a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 5
I have Java 1.5 and I use JGrasp, but when I try to compilate my program the compilate messages says:Fraction.java:1: '{' expectedpublic class Fraction implements Comparable<String>^
_Benni_a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 6
Oops, I meant <Fraction> not <String>.
_Benni_a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 7
> Fraction.java:1: '{' expectedMake sure you spelled everything correctly. Can you make a simple Hello World program compile with your IDE?
CaptainMorgan08a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...
# 8
Never mind, I have made it working now. I really appreciated all the help I got here and I hope I didn't sound annoying or anything like that.Thanks!Message was edited by: _Benni_
_Benni_a at 2007-7-12 17:21:35 > top of Java-index,Java Essentials,New To Java...