473,413 Members | 1,875 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,413 software developers and data experts.

Help with Fraction Project

1
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(numerator: int)
Fraction(numerator: 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
displayAsDecimal(decimalDisplay: boolean): void

Note: displayAsDecimal 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 displayAsDecimal for more details).
* displayAsDecimal 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.denominator = 1;}


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


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


public Fraction (String fracString) {

int theIndexOf= fracString.indexOf("/");
String newNumerator = fracString.substring(0, theIndexOf).trim();
String newDenominator = fracString.substring(theIndexOf+1).trim();
fracString.substring(0, theIndexOf);
int newerNumerator = Integer.parseInt(newNumerator);
int newerDenominator = Integer.parseInt(newDenominator);
this.numerator = newerNumerator;
this.numerator = newerDenominator;
char theChar = fracString.charAt(0);
Character.getType(theChar);
Character.getType(newerDenominator);

}

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 2265

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

Similar topics

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...
2
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...
6
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...
6
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...
1
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 ...
2
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...
4
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...
1
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...
2
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.