Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Defining a rational class. Simple but i cant figure it out to save my life

1. ## Defining a rational class. Simple but i cant figure it out to save my life

Define 3 constructors and a toString method, setters, and getters
Define arithmetic methods for add, subtract, multiply, divide, and negate. The negate method will return the negative of the calling Rational object – return new Rational (-num,denom);
Define logical methods for equals, notEquals, lessThan, lessThanOrEqual, greaterThan, and greaterThanOrEqual.

All negative values should be represented with the negative sign in the numerator – do this in the setDenom method and call it in the constructor.

Extra Credit: add a greatestCommonDivisor method to reduce a Rational number to its lowest terms. If you write this method, add the reduced values to your output. For example:
½ + ¾ = 10/8 reduced to 5/4

Run three times with the following data: (read in the data then use one of the constructors or the setter methods to fill objects with data)

Input 1: 1/2, 3/4 - entered as 1 2 3 4

Input 2: 1/2, 5/10 – entered as 1 2 5 10

Input 3: 3/-5, 2 – entered as 3 -5 2 1

Output (using the first set of numbers):

Rational 1 –>1/2 reduced to _______
Rational 2 -> 3/4 reduced to_______
½ + ¾ ______ reduced to_______
½ - ¾ ______ reduced to_______
½ * ¾ ______ reduced to_______
½ / ¾ ______ reduced to_______
- ½ ______ reduced to_______
½ == ¾ ______ print true or false
½ != ¾ ________ print true or false
½ > ¾ ______ print true or false
½ >= ¾ ______ print true or false
½ < ¾ ______ print true or false
½ <= ¾ ______ print true or false

2. ## Re: Defining a rational class. Simple but i cant figure it out to save my life

Dumping homework assignments is unproductive at best. Please see the link in my signature entitled getting help, as well as the following:
http://www.javaprogrammingforums.com...e-posting.html

3. ## Re: Defining a rational class. Simple but i cant figure it out to save my life

im not dumping it. i literally have no idea what to do to get started

4. ## Re: Defining a rational class. Simple but i cant figure it out to save my life

Originally Posted by spetillo3
im not dumping it. i literally have no idea what to do to get started
Did you read the links I provided? Without knowing any context - from whether you know how to make a hello world program to whether you know loops to whether you know object oriented programming - we will have a hard time pointing you in the right direction. Break the problem down, and concentrate on one requirement at a time...ask a specific question about that requirement.