473,420 Members | 4,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

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 6381
Nepomuk
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.

Greetings,
Nepomuk
Oct 19 '10 #2

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

Similar topics

5
by: Eoin Mcloughlin | last post by:
I have a perl script for calculating sequences that use numbers like 1/5908979186 however, I need to get more accuracy than the usual 15 decimal places, is this a limitation of 32-bit machines or...
6
by: Nobody | last post by:
I have some code that I am trying to optimize for speed... trying to squeeze every last CPU cycle out... I remembered an old trick where dividing & multiplying can be sped up by using bitshifts...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
5
by: per.nordlow | last post by:
Hey there! I want to multiply/divide (shift) a float with variable containing a power-of-two value: 2, 4, 8, 16, .... Is there such a function in the C standard library or the glibc library...
1
by: Nathan | last post by:
Is there a simple method of adding, subtracting, multiplying and dividing fractions in VB.NET, such as 4/9 - 1/3 = 1/9?
1
by: JWest46088 | last post by:
I am having trouble figuring out how to add, subtract, multiply, and divide fractions in my Java program. First off I will tell you what I already did. At first, the assignment was to: Define a...
8
by: gavinstone007 | last post by:
Who can solve this? Wrte a class number which represents all numbers. Implement member functions to carry out the following arithmetic operations on class instances, add- which returns an object...
3
by: Zach A and Cody C | last post by:
We are working on making a calculator and have found out how to add, subtract, and divide, but we can not think of any other ways to multiply besides 'x' but it does not appear in red, as does '-'...
1
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
7
by: ALi Shaikh | last post by:
I'm haivng a problems with these fractions, Whenever I run it, it returns the first one is smaller when it is not. I modified this file to include the compareTo method, please help Class ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.