When I run my main method I get the following exception: Exception in thread "main" java.lang.StackOverflowError, followed by this: at Rational.doubleValue(Rational.java:62) like 100+ times. This is for an assignment where the main class is provided for me, so I can't change anything in there. Here is the my code:
public class Rational extends Number implements Comparable<Rational>
{
private int num;
private int denom;
public Rational(int num, int denom)
{
this.num = num;
this.denom = denom;
}
public int compareTo(Rational r)
{
if ((num/denom) - (r.num/r.denom) > 0)
{
return +1;
}
else if ((num/denom) - (r.num/r.denom) < 0)
{
return -1;
}
return 0;
}
public Rational add(Rational r)
{
return new Rational(num + r.num, denom + r.denom);
}
public Rational sub(Rational r)
{
return new Rational(num - r.num, denom - r.denom);
}
public Rational mul(Rational r)
{
return new Rational(num * r.num, denom * r.denom);
}
public Rational div(Rational r)
{
return new Rational(num * r.denom, denom * r.num);
}
public int gcd(Rational r)
{
int i = 1;
while(i != 0)
{
i = denom % r.denom;
denom = r.denom;
r.denom = i;
}
return denom;
}
public String toString()
{
String a = num + "/" + denom;
return a;
}
public double doubleValue()
{
return (double)doubleValue();
}
public float floatValue()
{
return (float)floatValue();
}
public int intValue()
{
return (int)intValue();
}
public long longValue()
{
return (long)longValue();
}
}
And here is the main class(which I cannot modify)
public class Main {
public static void main(String[] args) {
Rational a = new Rational(2, 4);
Rational b = new Rational(2, 6);
System.out.println(a + " + " + b + " = " + a.add(b));
System.out.println(a + " - " + b + " = " + a.sub(b));
System.out.println(a + " * " + b + " = " + a.mul(b));
System.out.println(a + " / " + b + " = " + a.div(b));
Rational[] arr = {new Rational(7, 1), new Rational(6, 1),
new Rational(5, 1), new Rational(4, 1),
new Rational(3, 1), new Rational(2, 1),
new Rational(1, 1), new Rational(1, 2),
new Rational(1, 3), new Rational(1, 4),
new Rational(1, 5), new Rational(1, 6),
new Rational(1, 7), new Rational(1, 8),
new Rational(1, 9), new Rational(0, 1)};
selectSort(arr);
for (int i = 0; i < arr.length-1; ++i) {
if (arr[i].compareTo(arr[i+1]) > 0) {
System.exit(1);
}
}
// for (Rational r : arr) {
// System.out.println(r);
// }
Number n = new Rational(3, 2);
System.out.println(n.doubleValue());
System.out.println(n.floatValue());
System.out.println(n.intValue());
System.out.println(n.longValue());
}
public static <T extends Comparable<? super T>> void selectSort(T[] array) {
T temp;
int mini;
for (int i = 0; i < array.length - 1; ++i) {
mini = i;
for (int j = i + 1; j < array.length; ++j) {
if (array[j].compareTo(array[mini]) < 0) {
mini = j;
}
}
if (i != mini) {
temp = array[i];
array[i] = array[mini];
array[mini] = temp;
}
}
}
}
答案 0 :(得分:2)
These methods
public double doubleValue()
{
return (double)doubleValue();
}
public float floatValue()
{
return (float)floatValue();
}
public int intValue()
{
return (int)intValue();
}
public long longValue()
{
return (long)longValue();
}
all result in stack overflow because they're recursively calling themselves with no exit. This is not what you intended.
For each of these methods you need to write code that converts the number represented by your Rational
into a double
, float
, int
or long
.
By the way, you also have other problems in your definitions of add()
, subtract()
, and compare()
but those have to do with the algorithm you are using vis-a-vis the arithmetical rules that apply to those operations.
public class Rational extends Number implements Comparable<Rational> {
private int num;
private int denom;
public Rational(int num, int denom) {
this.num = num;
this.denom = denom;
}
public int compareTo(Rational r) {
if ((num / denom) - (r.num / r.denom) > 0) {
return +1;
} else if ((num / denom) - (r.num / r.denom) < 0) {
return -1;
}
return 0;
}
public Rational add(Rational r) {
return new Rational(num + r.num, denom + r.denom);
}
public Rational sub(Rational r) {
return new Rational(num - r.num, denom - r.denom);
}
public Rational mul(Rational r) {
return new Rational(num * r.num, denom * r.denom);
}
public Rational div(Rational r) {
return new Rational(num * r.denom, denom * r.num);
}
public int gcd(Rational r) {
int i = 1;
while (i != 0) {
i = denom % r.denom;
denom = r.denom;
r.denom = i;
}
return denom;
}
public String toString() {
String a = num + "/" + denom;
return a;
}
public double doubleValue() {
return (double)num / (double)denom;
}
public float floatValue() {
return (float)num / (float)denom;
}
public int intValue() {
return Math.round(num / denom);
}
public long longValue() {
return Math.round(num / denom);
}
}