470,818 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,818 developers. It's quick & easy.

Add, subtract, multiply, divide fractions...

Here is the assignment. I need some help on it. I'm clueless.

In this assignment you will write a class called Fraction to represent positive and negative fractions, i.e. numbers like -5/4 or Ĺ. Your class should be in a file called Fraction.java and needs to be declared as a public class. Create a file called TestFraction.java (in the same directory as Fraction.java) containing the public class TestFraction, and the main method. Use the main method in TestFraction to help test your Fraction methods and constructor. Write your own test code for each method in TestFraction as you complete each new method in Fraction, and run it with the command java TestFraction. Hand in only the file Fraction.java --- we will test it with our own test code.

Your Fraction class should represent a fraction by only two integers, as shown in the code here which you should copy into Fraction.java to get started. Similarly copy this into TestFraction.java and add your own tests to get started.

First write a constructor for the Fraction class. Its signature should be as follows.

public Fraction(int numerator, int denominator)

Store the fraction in its lowest terms by dividing out by the greatest common divisor (as provided in the sample code you saved from above). Ensure that the denominator is always stored as a positive number. If the constructor is given a negative denominator, then negate the denominator and the numerator before assigning them to instance variables. Don't worry unduly about the case where a denominator of zero is provided: just cause an error by executing the following statement.

throw new RuntimeError("attempt to create zero denominator Fraction");

Then write a toString() method with the following signature as discussed in class.

public String toString()

This should print out a fraction in the form e.g. -12/47 or 1/3

Finally write methods with signatures as follows to perform arithmetic.

public Fraction plus(Fraction f)
public Fraction minus(Fraction f)
public Fraction multiply(Fraction f)
public Fraction divide(Fraction f)

These will be called as e.g. answer = f1.minus(f2) //compute f1 - f2
Oct 14 '10 #1
1 6140
3,112 Expert 2GB
Well, it walks you through parts of the process quite well - it tells you, which files and classes to create and which functions these should have. So, if you haven't already, do that.

Next, think about how you add, subtract, multiply or divide fractions, when doing it "manually" (= by hand, without a computer or calculator). Then instruct your program to do the same. You will have to be very detailed in your instructions though, as computers are pretty stupid when it comes to figuring out things that are normal for humans.

If you have any further questions, feel free to ask.

Oct 19 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Eoin Mcloughlin | last post: by
6 posts views Thread by Nobody | last post: by
388 posts views Thread by maniac | last post: by
5 posts views Thread by per.nordlow | last post: by
1 post views Thread by Nathan | last post: by
8 posts views Thread by gavinstone007 | last post: by
7 posts views Thread by ALi Shaikh | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.