05-07-2011, 05:45 PM
Here is the code I need to write, I Already completed the first part, but I need help with Problem 2:
Problem 1: Write a RationalNumber class having public methods for
public RationalNumber() // Initializes to 0/1
public RationalNumber(int n, int d) // Initializes to n/d
public RationalNumber(RationalNumber x) // Initializes to x’s values
public void add(RationalNumber other) // this = this + other
public void subtract(RationalNumber other) // this = this - other
public void multiply(RationalNumber other) // this = this * other
public void divide(RationalNumber other) // this = this / other
public String toString() // Creates string representation
public boolean equals(RationalNumber other) // Is this == other?
Notes:
- Any methods not listed above and all data fields should be private.
- r.equals(s) should return true if r and s have the same value. You know enough to do a decent job of
this method already, but if you want to do an even better job, read Pages 573-578 before writing equals.
- toString should create a String representation of the rational, in reduced form. If a number is negative
the string should begin with -. If it is a whole number (including zero) it should be shown as over 1. For
example. If I initialize with n=4 and d=-2, toString should return "-2/1".
- If d=0 is passed to the constructor, throw an IllegalArgumentException.
Problem 2: Write a rational number calculator that repeatedly waits for an input line and then prints
the result of the calculation using your RationalNumber class. The calculator should stop once the user
types “q”. Your program should behave like this:
> 1 / 2 + 1 / 2
1/1
> 1 / 2 - 1 / 2
0/1
> 10 / 1 * -1 / 10
-1/1
> -64 / 128 / 128 / -64
1/4
> 2 / -4 = -3 / 6
true
> q
Each line has the following format <int> / <int> <op> <int> / <int>, so it should be easy to parse using
Scanner methods. <int> / <int> represents a rational number, and <op> is a single character indicating
the desired operation. You may assume that every input follows this format without error.
Problem 1: Write a RationalNumber class having public methods for
public RationalNumber() // Initializes to 0/1
public RationalNumber(int n, int d) // Initializes to n/d
public RationalNumber(RationalNumber x) // Initializes to x’s values
public void add(RationalNumber other) // this = this + other
public void subtract(RationalNumber other) // this = this - other
public void multiply(RationalNumber other) // this = this * other
public void divide(RationalNumber other) // this = this / other
public String toString() // Creates string representation
public boolean equals(RationalNumber other) // Is this == other?
Notes:
- Any methods not listed above and all data fields should be private.
- r.equals(s) should return true if r and s have the same value. You know enough to do a decent job of
this method already, but if you want to do an even better job, read Pages 573-578 before writing equals.
- toString should create a String representation of the rational, in reduced form. If a number is negative
the string should begin with -. If it is a whole number (including zero) it should be shown as over 1. For
example. If I initialize with n=4 and d=-2, toString should return "-2/1".
- If d=0 is passed to the constructor, throw an IllegalArgumentException.
Problem 2: Write a rational number calculator that repeatedly waits for an input line and then prints
the result of the calculation using your RationalNumber class. The calculator should stop once the user
types “q”. Your program should behave like this:
> 1 / 2 + 1 / 2
1/1
> 1 / 2 - 1 / 2
0/1
> 10 / 1 * -1 / 10
-1/1
> -64 / 128 / 128 / -64
1/4
> 2 / -4 = -3 / 6
true
> q
Each line has the following format <int> / <int> <op> <int> / <int>, so it should be easy to parse using
Scanner methods. <int> / <int> represents a rational number, and <op> is a single character indicating
the desired operation. You may assume that every input follows this format without error.
