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

Write the code that will assign the variable \'myApple\' a new Apple with code \

ID: 3864930 • Letter: W

Question

Write the code that will assign the variable 'myApple' a new Apple with code 'pl102', variety 'Pink Lady' and price per pound 1.49

//using this code

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) {   

}  

}

//the code should be written in java.

Explanation / Answer

Fruit.java

public class Fruit {
   private double pricePerPound;
   //Declaring instance variables
   private String code;
  
       //Parameterized constructor
       public Fruit(double pricePerPound, String code) {
           super();
           this.pricePerPound = pricePerPound;
           this.code = code;
       }
      
       //getters and setters
       public double getPricePerPound() {
           return pricePerPound;
       }
       public void setPricePerPound(double pricePerPound) {
           this.pricePerPound = pricePerPound;
       }
       public String getCode() {
           return code;
       }
       public void setCode(String code) {
           this.code = code;
       }
      
       //toString method is used to display the contents of an object inside it
       @Override
       public String toString() {
           return "Fruit # PricePerPound=" + pricePerPound + ", code=" + code;
       }
     
  
}

___________________

Apple.java

public class Apple extends Fruit {
   //Declaring instance variables
   private String variety;

   //Parameterized constructor
   public Apple(double pricePerPound, String code, String variety) {
       super(pricePerPound, code);
       this.variety = variety;
   }

   //getters and setters
   public String getVariety() {
       return variety;
   }

   public void setVariety(String variety) {
       this.variety = variety;
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return super.toString()+" Variety=" + variety;
   }
     

}

__________________

package org.students;

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {
       //Declaring variables
       double price_per_pound;
       String code,variety;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //getting the code entered by the user
       System.out.print("Enter the code:");
       code=sc.next();
       sc.nextLine();
      
       //Getting the Variety entered by the user
       System.out.print("Enter the variety:");
       variety=sc.nextLine();
      
       //Getting the Price entered by the user
       System.out.print("Enter the price:");
       price_per_pound=sc.nextDouble();
      
       //Creating the Apple class object by passing the user inputs as arguments
       Apple myApple=new Apple(price_per_pound, code, variety);
      
       //Displaying the output
       System.out.println(myApple.toString());
      

   }

}

_______________

Output:

Enter the code:pl102
Enter the variety:Pink Lady
Enter the price:1.49
Fruit #
PricePerPound=1.49, code=pl102 Variety=Pink Lady

____________Thank You