I'm really really stuck and hope that someone can help me. I read and read and still don't understand how to fix my stackoverflow error. I know that its in my constructor, but i don't know how to fix it.
I am creating a derived class called FractionBottle that extends the Bottle class. The FractionBottle class as a private data memeber: Fraction myFraction = new Fraction(); Here is my constructor in my Bottle Class:
public class Bottle
private final int MAX_PILLS = 120;
private int pillsInBottle;
public Bottle()
{
pillsInBottle = 0;
}
Here's what I have in my FractionBottle class:
public class FractionBottle extends Bottle
{
Fraction myFraction = new Fraction();
public FractionBottle()
{
super();
myFraction.getNumerator();
myFraction.getDenominator();
}
public FractionBottle(int wholeValue, int num, int den)
{
super(wholeValue);
myFraction.set(num, den);;
}
public void read()
{
super.read();
System.out.println("Pleas enter value for fraction part:");
myFraction.read();
}
public FractionBottle add(FractionBottle other)
{
FractionBottle sumOfBottles = new FractionBottle();
sumOfBottles = this.add(other);
sumOfBottles.myFraction = this.myFraction.add(other.myFraction);
return (sumOfBottles);
}
Here's the demo I'm using:
public class FractionBottleDemo
{
public static void main (String args[])
{
FractionBottle fbl1 = new FractionBottle();
FractionBottle fbl2 = new FractionBottle();
FractionBottle fbl3 = new FractionBottle();
System.out.println("Enter info for whole value for fbl1: ");
fbl1.read();
System.out.println("Enter infor for whole value for fbl2: ");
fbl2.read();
System.out.println(fbl1);
System.out.println(fbl2);
fbl3 = fbl1.add(fbl2);
}
}
I am really stuck on this assignment for class, and I've been at it for a few days now. I'm getting the following error:
Exception in thread "main" java.lang.StackOverflowError
at FractionBottle.<init>(FractionBottle.java:7)
at FractionBottle.add(FractionBottle.java:32)
the last lin repeats several times...
Please tell me how to fix this! I know its going into a infinite recursive loop. but i don't know how to fix it. My add method in my FractionBottle class, must return a FractionBottle.
Thank you!!!!