0

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!!!!

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
  • 2
    What does your `add` method do? Describe it in words. – Sotirios Delimanolis Mar 05 '14 at 16:02
  • Your code does not match the call stack. Please post the full code – Stephane Nicoll Mar 05 '14 at 16:03
  • So, the bottle class has a int value (pillsInBottle), the add method is suppose to add the pillsInBottle, and add the myFraction portion of each FractionBottle. It then is suppose to return a FractionBottle. – user3384292 Mar 05 '14 at 16:04
  • Looks like you have an infinite loop inside the add method – Mason T. Mar 05 '14 at 16:04
  • In your `add(FractionBottle)`-method you are doing a recursive call to `add(FractionBottle)` with the same argument. This looks dangerous... – slartidan Mar 05 '14 at 16:04
  • The pillsInBottle value is just added to the pillsInBottle value of the other FractionBottle. And the myFraction value of this FractionBottle is added to the myFraction value of the other FractionBottle. THe whole and fraction parts are not combined. Thank you so much! @user3384292 – user3384292 Mar 05 '14 at 16:06
  • @slartidan I understand...but how do i fix it!?! :/ please help! – user3384292 Mar 05 '14 at 16:06
  • possible duplicate of [What is a stack overflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error) –  Mar 27 '14 at 05:50

2 Answers2

1

Looks like you have an infinite recursive call inside the add method.

sumOfBottles = this.add(other);

All recursive functions require a check to break out of the recursive calls.

Since, you are wanting to call the Bottle's add method.

Replace above line with

sumOfBottles = super.add(other);
Mason T.
  • 1,567
  • 1
  • 14
  • 33
0

Obviously the error lies here:

public FractionBottle add(FractionBottle other){
    ...
    sumOfBottles = this.add(other);
    ...
}

because you are calling to the add method from this very method, and that causes an infinite number of recursive callings and then, your StackOverflowError

Maybe what you want to do is to call Bottle's add method that is:

public FractionBottle add(FractionBottle other){
        ...
        Bottle sumOfBottles = new FractionBottle();
        sumOfBottles = super.add(other);
        ...
    }

that is much safer and wouldn't cause infinite loops

Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
  • oh, that makes sense!i guess What i meant to do was call the bottle add method. How can I do that? Here is my full Bottle class: – user3384292 Mar 05 '14 at 16:09
  • public Bottle add(int amount) { Bottle b = new Bottle(); b.pillsInBottle = this.pillsInBottle + amount; if ((b.pillsInBottle > MAX_PILLS) || (b.pillsInBottle < 0)) { System.out.println("This amount cannot be added to this bottle."); System.exit(0); } return (b); } @user3384292 – user3384292 Mar 05 '14 at 16:10
  • @user3384292 You're Bottle class doesn't have an `add` method – Mason T. Mar 05 '14 at 16:11
  • @user3384292 If it does have an `add` method just say `super.add(parameters)` – Mason T. Mar 05 '14 at 16:12
  • @NappaTheSaiyan i guess not in the snippet, but it should be implemented in the super class – Amin Abu-Taleb Mar 05 '14 at 16:13
  • @AminAbu-Taleb - I feel really really silly now! I read that over and over, but i was just looking in the wrong place in my code! Thank you soooooooo much!!! You are a lifesaver! I really appreciate it! – user3384292 Mar 05 '14 at 16:18
  • @AminAbu-Taleb I understand the idea- But actually when I do it that way, it says: "mismatch, can't convert from a bottle to a fractionbottle...? I thought I fractionbottle is a bottle (since its a derived class..do I need to type cast it? – user3384292 Mar 05 '14 at 16:25
  • @user3384292 the thing is...if add method in Bottle returns a Bottle, you'll have that conversion error, you should use Bottle in the variable declaration to avoid it. I'll edit it for you – Amin Abu-Taleb Mar 05 '14 at 16:27
  • @AminAbu-Taleb Thank you so much for taking the time to help me! Now I can finish writing the rest of the code :) – user3384292 Mar 05 '14 at 16:46
  • @user3384292 don't forget to accept the answer in case it solved your question ;) – Amin Abu-Taleb Mar 05 '14 at 16:47