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!
