473,770 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Fraction Project

1 New Member
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows:

UML for the class:

Fraction()
Fraction(numera tor: int)
Fraction(numera tor: int, denominator: int)
Fraction(value: String)
add(frac: Fraction): Fraction
subtract(frac: Fraction): Fraction
divide(frac: Fraction): Fraction
multiply(frac: Fraction): Fraction
equals(frac: Fraction): boolean
compareTo(frac: Fraction): int
toString(): String
displayAsDecima l(decimalDispla y: boolean): void

Note: displayAsDecima l needs to be a class method.

* The default constructor which should create an object with the value 0/1.
* The second constructor should create an object with the value passed in as the numerator and a denominator of 1.
* The third constructor should create an object with the first value passed in as the numerator and the second value passed in as the denominator.
* The string passed to the fourth constructor should be of the form -9/20 or -9 / 20. If the string is not of this format, an error message should be displayed, and the object should be given a value of 0/1.
* add should return a Fraction object that is the sum of the calling object and the object passed in.
* subtract should return a Fraction object whose value is the result of the calling object minus the object passed in.
* divide should return a Fraction object whose value is the result of the calling object divided by the object passed in.
* multiply should return a Fraction object whose value is the result of the calling object multiplied by the object passed in.
* In the above four methods, the value of the calling object and object passed in should not be modified.
* equals should return true if the calling object and the object passed in represent the same value. Otherwise, the method should return false.
* compareTo should return 0 if the calling object and the object passed in represent the same value. If the calling object represents a smaller value than the object passed in, -1 should be returned. Otherwise, 1 should be returned.
* toString should return a string that displays the value of the fraction. The format of the string should be in one of two forms: -1/4 (not 1/-4) or 0.25. (see displayAsDecima l for more details).
* displayAsDecima l determines the format of the string returned by toString. If the method has was most recently called with true being passed to it, the format of the string returned by the toString method should be of a decimal form (0.25). Otherwise, the string returned by the toString method should be in fractional form.

My source code so far is as follows:

package packone;

public class Fraction {
private int numerator;
private int denominator;


public Fraction(){
this.numerator= 0;
this.denominato r = 1;}


public Fraction(int newNumerator){
this.numerator = newNumerator;
this.denominato r = 1;}


public Fraction(int newNumerator, int newDenominator) {
this.numerator = newNumerator;
this.denominato r = newDenominator; }


public Fraction (String fracString) {

int theIndexOf= fracString.inde xOf("/");
String newNumerator = fracString.subs tring(0, theIndexOf).tri m();
String newDenominator = fracString.subs tring(theIndexO f+1).trim();
fracString.subs tring(0, theIndexOf);
int newerNumerator = Integer.parseIn t(newNumerator) ;
int newerDenominato r = Integer.parseIn t(newDenominato r);
this.numerator = newerNumerator;
this.numerator = newerDenominato r;
char theChar = fracString.char At(0);
Character.getTy pe(theChar);
Character.getTy pe(newerDenomin ator);

}

Any help at all would be greatly appreciated, even if it's just a guess in the right direction. Thank you!
Oct 18 '06 #1
0 2287

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

Similar topics

1
8566
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 class called Rational. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator or denominator as a double. Include additional member function that outputs the value of the...
2
3342
by: Anan18 | last post by:
Use Integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 should be stored in the object as 1 in the numerator and 2 in the denominator....
6
13431
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input into most of the exercises I have done. This exercise is asking for a user to enter two fractionslike "1/3" or "5/8". Scanner doesn't work and I don't know of another way to get this done. I think that I will have to somehow strip the "/" out and...
6
1644
by: adoobi05 | last post by:
Hello ... Now , When I write this massege I feel with a huge disappointment becouse I waste 6 houers to konow what is the Problem with this code . The I didn't write the hole code , becouse every thing was Ok Until I want to OverLoad the + operation using friend function , the compiler give me this massege : INTERNAL COMPILER ERROR The binfit of the programe is to add Fraction using class and the overload all the artheamtic...
1
2214
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp #include <iostream> #include <string>
2
3021
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a class named fraction declared in a file named fraction.h, with two private data members: an int numerator and an int denominator. 2. Convert the four arithmetic functions named add, sub, mult, and div into public member functions, each accepting a...
4
14452
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not sure how the algorithm of reducing fractions works. Here is part of my code: //Header File
1
3410
by: jrw133 | last post by:
So i was given this program in class. i am supposed to create a four-function calculator for fractions using a fraction class. Heres what the requirements are:create a member function for each of the four arithmetic operations. For example fadd(), fsub(), fmul() and fdiv(). these member functions iwll each take 1 argument(of type fraction) and return an object of type fraction. your class should provide member functions for input and output....
2
5974
by: frozenfirefly | last post by:
okay, so the first class of this program is the Fraction class. and which I supposed to create: 1. method to input numerator & denominator of fraction 2. and a method to reduce the fraction to its smallest stage. example: input: 6/8 reduce to: 3/4 (both are devided by the gcd which is 2) I think the problem is around the reduce method... about the gcd. please tell me what's wrong.
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.