I am trying to make a calculator to preform arithmetic operations on rational numbers. For this i have a Rational
class. The program should be executed from commandline with args:
java (...) num/denom operator(+-./) num/denom
It seems like a get a compilation error when creating instances of the Rational
class, and this happens when I try to compile from cmd. I don't get this error when compiling in Eclipse. The main
method with the calculator logic is a bit messy at the moment, so I will paste an example from a test class where I am creating some instances of Rational. I will also paste the code for Rational
.
Test method below:
public class TestRational {
public static void main(String[] args) {
Rational r1 = new Rational(1, 2);
Rational r2 = new Rational(1, 2);
Rational result = new Rational();
result = r1.add(r2);
System.out.println("r1 + r2 = " + result);
}
}
The Rational
class:
public class Rational extends Number implements Comparable<Rational> {
private long numerator = 0;
private long denominator = 1;
private long[] r = new long[2];
// numerator: r[0]
// denominator: r[1]
public Rational() {
this(0, 1);
}
public Rational(long numerator, long denominator) {
long gcd = gcd(numerator, denominator);
this.r[0] = ((denominator > 0) ? 1 : -1) * numerator / gcd;
this.r[1] = Math.abs(denominator) / gcd;
}
private static long gcd(long n, long d) {
long n1 = Math.abs(n);
long n2 = Math.abs(d);
int gcd = 1;
for (int k = 1; k <= n1 && k <= n2; k++) {
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
}
return gcd;
}
public long getNumerator() {
return r[0];
}
public long getDenominator() {
return r[1];
}
public Rational add(Rational secondRational) {
long n = r[0] * secondRational.getDenominator()
+ r[1] * secondRational.getNumerator();
long d = r[1] * secondRational.getDenominator();
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
long n = r[0] * secondRational.getDenominator()
- r[1] * secondRational.getNumerator();
long d = r[1] * secondRational.getDenominator();
return new Rational(n, d);
}
public Rational multiply(Rational secondRational) {
long n = r[0] * secondRational.getNumerator();
long d = r[1] * secondRational.getDenominator();
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
long n = r[0] * secondRational.getDenominator();
long d = r[1] * secondRational.getNumerator();
return new Rational(n, d);
}
@Override
public String toString() {
if (r[1] == 1)
return r[0] + "";
else
return r[0] + "/" + r[1];
}
@Override
public boolean equals(Object other) {
return (((this.subtract((Rational)(other))).getNumerator() == 0));
}
@Override
public int intValue() {
return (int)doubleValue();
}
@Override
public float floatValue() {
return (float)doubleValue();
}
@Override
public double doubleValue() {
return r[0] * 1.0 / r[1];
}
@Override
public long longValue() {
return (long)doubleValue();
}
@Override
public int compareTo(Rational o) {
if (this.subtract(o).getNumerator() > 0)
return 1;
else if (this.subtract(o).getNumerator() < 0)
return -1;
else
return 0;
}
}
The error message looks like this:
TestRational.java:7: error: cannot find symbol
Rational r1 = new Rational(1, 2)
^
symbol: class Rational
location: class TestRational
I get one error message for each occurence of the Rational word, with a "^" pointing up against the "R".
I have read this post, but have not been able to solve the problem: link
Can anyone see what is causing the error, and why is it only caused when compiling the program for the commandlinde?