Write the code that will assign the variable \'myApple\' a new Apple with code \
ID: 3864934 • Letter: W
Question
Write the code that will assign the variable 'myApple' a new Apple with code 'pl102' but not specifying the other properties
//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;
private String code;
Fruit()
{
}
public Fruit(double pricePerPound, String code) {
this.pricePerPound = pricePerPound;
this.code = code;
}
public Fruit(String code) {
this.code = code;
}
public String FruitCode()
{
return this.code;
}
}
Apple.java
public class Apple extends Fruit {
private String variety;
public Apple(double pricePerPound, String code, String variety) {
}
public Apple(String code) {
Fruit obj = new Fruit("pl102");
String myApple= obj.FruitCode();
System.out.println(myApple);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.