Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//The second constructor in Apple is not complete. //Write the code for that con

ID: 3864924 • Letter: #

Question

//The second constructor in Apple is not complete. //Write the code for that constructor

public Apple(String code) {

}

Information

public class Fruit {

    private double pricePerPound;

    private String code;

    public Fruit(double pricePerPound, String code) {

        this.pricePerPound = pricePerPound;

        this.code = code;

    }

    public Fruit(String code) {

        this.code = code;

    }

}

public class Apple extends Fruit {   

private String variety;    

public Apple(double pricePerPound, String code, String variety) {   

}    

public Apple(String code) {   

}  

}

//This program should be in java format.

Explanation / Answer

public class Fruit
{
   private double pricePerPound;
   private String code;

   public Fruit(double pricePerPound, String code)
   {
       this.pricePerPound = pricePerPound;
       this.code = code;
   }

   public Fruit(String code)
   {
       this.code = code;
   }

}


class Apple extends Fruit
{
   private String variety;

   // Fruit constructor will be called using super keyword to fill fruit values
   public Apple(double pricePerPound, String code, String variety)
   {
       super(pricePerPound, code);
       this.variety = variety;
   }  

   // Fruit method will be called using super method
   public Apple(String code)
   {
       super(code);
   }


   public static void main(String[] args)
   {
       // apple object named myAPple of class Apple
       Apple myApple = new Apple(10.45, "APBW", "INDIAN");  
   }
}