A Java question. Write a Snack class as a subclass of Product . In addition to d
ID: 3820154 • Letter: A
Question
A Java question. Write a Snack class as a subclass of Product. In addition to description and price, Snackhas integer instance variable gramsOfFat.
Write a method getGramsOfFat.
Write a method isHealther which returns true if this Snack has fewer grams of fat than the parameter Snack.
Include toString and equals.
Snack should implement Comparable interface. Snacks are ordered first by gramsOfFat with the Snackwith the fewer grams of fat coming first. If the grams of fat are the same order by price with the lowest price coming first, If prices are also equal, order alphabetically by description
Implement the Clonable interface
The Product and SnackTester was provided:
Product.java
SnackTester.java
please do no change the SnackTester, just do the Snack.java
Explanation / Answer
public class Snack extends Product implements Comparable<Snack> {
int gramsOfFat;
public Snack(String description, double price, int gramsOfFat) {
super(description, price);
this.gramsOfFat = gramsOfFat;
}
public int getGramsOfFat() {
return gramsOfFat;
}
public boolean isHealthier(Snack snack) {
if (gramsOfFat < snack.getGramsOfFat())
return true;
return false;
}
@Override
public int compareTo(Snack otherSnack) {
return gramsOfFat - otherSnack.getGramsOfFat();
}
}
Here you go champ
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.