Question

I need help. So my code is pretty much finished up but in my DessertTester when...

I need help. So my code is pretty much finished up but in my DessertTester when I try to run it I get the following error, " Exception in thread "main" java.lang.StackOverflowError   at Fruit.getPricePerPound(Fruit.java:15)." Below I have pasted my dessert class, and my fruit class.

Fruit :

public class Fruit extends Dessert {
    protected final double weightInPounds;
    protected final double pricePerPound;
    public Fruit(String name, double weightInPounds, double pricePerPound){
        super(name);

        this.weightInPounds = weightInPounds;
    this.pricePerPound = pricePerPound;

    }
    public final double getWeightInPounds(){
        return weightInPounds;
    }
    public final double getPricePerPound(){
        return getPricePerPound();
    }
    public double getPrice(){
        return getWeightInPounds() * getPricePerPound();

    }

}

Dessert:

public abstract class Dessert {
    /** Name of the dessert item. */
    protected final String name;

    /**
     * Constructs a new dessert item.
     * @param name Name of the dessert.
     */
    public Dessert(String name) {
        this.name = name;
    }

    /**
     * Get name of the dessert.
     * @return dessert name
     */
    public final String getName() {
        return name;
    }

    /**
     * Get the price of the dessert.
     * @return Dessert price
     */
    public abstract double getPrice();
}

In my IDE, in the fruit class at the beginning of "public double getPrice();" my IDE states, " Implements method in Desserts" and also, at the bottom of my  public final double getPricePerPound, my IDE states," recursive call". Can you help me fix this? 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

this is the mistake in your code

public final double getPricePerPound(){
        return getPricePerPound();
    }

it should be

public final double getPricePerPound() {
    return pricePerPound;
}

//final code is here

public abstract class Dessert {
    /** Name of the dessert item. */
    protected final String name;

    /**
     * Constructs a new dessert item.
     * @param name Name of the dessert.
     */
    public Dessert(String name) {
        this.name = name;
    }

    /**
     * Get name of the dessert.
     * @return dessert name
     */
    public final String getName() {
        return name;
    }

    /**
     * Get the price of the dessert.
     * @return Dessert price
     */
    public abstract double getPrice();
}
public class Fruit extends Dessert {
    protected final double weightInPounds;
    protected final double pricePerPound;

    public Fruit(String name, double weightInPounds, double pricePerPound) {
        super(name);

        this.weightInPounds = weightInPounds;
        this.pricePerPound = pricePerPound;

    }

    public final double getWeightInPounds() {
        return weightInPounds;
    }

    public final double getPricePerPound() {
        return pricePerPound;
    }


    @Override
    public double getPrice() {
        return getWeightInPounds() * getPricePerPound();
    }
}
public class DessertTester {
    public static void main(String[] args)
    {
        // creating object of fruit class
        Fruit fruit = new Fruit("Apple",78.9,23.45);
        //display object's details
        System.out.println(fruit.getName()+" "+fruit.getPricePerPound()+" "+fruit.getWeightInPounds()+" "+fruit.getPrice());

    }
}

//output

//if you need any help regarding this solution .......... please leave a comment ............. thanks

Add a comment
Know the answer?
Add Answer to:
I need help. So my code is pretty much finished up but in my DessertTester when...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT