Question #59373

The abstract Fruit.java class contains some methods and variables that it takes care of, and one method called getCost() that every inheriting subclass must Override given that each fruit object will calculate the cost differently.

1-Apple: The apple class inherits from Fruit. It accepts also the number of apples to add in the constructor. The price returned is per dozen! You must use the price already given to you in file Consts.java. The getCost() method must return the correct price for all the applies. For example, if the price per dozen is 24.0, and you added 3 apples, then the total cost of this class should return 6.0 QR.

Expert's answer

Answer on Question #59373, Programming & Computer Science, Java, JSP, JSF

Condition

The abstract Fruit.java class contains some methods and variables that it takes care of, and one method called getCost() that every inheriting subclass must Override given that each fruit object will calculate the cost differently.

1-Apple: The apple class inherits from Fruit. It accepts also the number of apples to add in the constructor. The price returned is per dozen! You must use the price already given to you in file Consts.java. The getCost() method must return the correct price for all the applies. For example, if the price per dozen is 24.0, and you added 3 apples, then the total cost of this class should return 6.0 QR.

Code

Task.java

public class Task {
    public static void main(String[] args) {
        Apple apple = new Apple(3);
        System.out.println("Apple: " + apple.getCost());
    }
}

Fruit.java

public abstract class Fruit {
    public abstract double getCost();
}

Consts.java

public class Consts {
    public static final double applePrice = 24.0; // per dozen
}

Apple.java

public class Apple extends Fruit {
    private int applesNumber;
    public Apple(int number) {
        this.applesNumber = number;
    }
    @Override
    public double getCost() {
        return (Consts.applePrice / 12.0) * applesNumber;
    }
}


Output

Apple: 6.0

http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS