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? 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
I need help. So my code is pretty much finished up but in my DessertTester when...