A Java question. Write a Snack class as a subclass of Product . In addition to d
ID: 3819366 • 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
Explanation / Answer
Product.java
/**
* Models a Product that can increase and decrease in price
*/
public class Product
{
private double price;
private String description;
/**
* Constructs a Product with a price and a description
* @param thePrice the price of this Product
* @param theDescription - the description of this product
*/
public Product(String theDescription, double thePrice)
{
price = thePrice;
description = theDescription;
}
/**
* Gets the price
* @return the price of this Product object
*/
public double getPrice()
{
return price;
}
/**
* Gets the description
* @return the description of the Product object
*/
public String getDescription()
{
return description;
}
/**
* Reduces the price of this product by the give percentage
* @param percent the percentage to reduce the priice by
*/
public void reducePrice(double percent)
{
double reduction = price * percent / 100;
price = price - reduction;
}
/**
* Increases the price by the given percent
* @param percent the percent to increase the price by
*/
public void increasePrice(double percent)
{
double increase = price * percent / 100;
price = price + increase;
}
@Override
public String toString()
{
return getClass().getName()
+ "[description=" + description
+ ",price=" + price
+ "]";
}
@Override
public boolean equals(Object otherObject)
{
if (otherObject == null) {return false;}
if (this.getClass() != otherObject.getClass()) {return false;}
Product other = (Product)otherObject;
return price == other.price
&& description.equals(other.description);
}
}
____________________
Snack.java
public class Snack extends Product implements Cloneable,Comparable {
private int gramsOfFat;
public Snack(String theDescription, double thePrice, int gramsOfFat) {
super(theDescription, thePrice);
this.gramsOfFat = gramsOfFat;
}
public int getGramsOfFat() {
return gramsOfFat;
}
public void setGramsOfFat(int gramsOfFat) {
this.gramsOfFat = gramsOfFat;
}
public boolean isHealthier(Snack snk)
{
if(this.gramsOfFat<snk.getGramsOfFat())
return true;
else
return false;
}
public boolean equals(Snack snk) {
if (gramsOfFat==snk.getGramsOfFat() && getDescription().equals(snk.getDescription()) && getPrice()==snk.getPrice())
return true;
return false;
}
@Override
public String toString() {
return super.toString()+" [Grams Of Fat=" + gramsOfFat+"]";
}
@Override
public int compareTo(Object o) {
int val = 0;
int originalSnk = getGramsOfFat();
Snack s = (Snack) o;
int paramSnk = s.getGramsOfFat();
if (originalSnk > paramSnk)
val = 1;
else if (originalSnk< paramSnk)
val = -1;
else if (originalSnk== paramSnk)
{
double origPrice=getPrice();
double paramPrice=s.getPrice();
if(origPrice>paramPrice)
val=1;
else if(origPrice<paramPrice)
val=-1;
else if(origPrice==paramPrice)
{
String orgDes=getDescription();
String paramDes=s.getDescription();
orgDes.compareTo(paramDes);
}
}
return val;
}
@Override
protected Snack clone() throws CloneNotSupportedException {
return (Snack) super.clone();
}
}
_______________________
SnackTester.java
import java.util.Arrays;
public class SnackTester
{
public static void main(String[] args) throws CloneNotSupportedException
{
//test constructor
Snack crunch = new Snack("Nestle's Crunch", 1.75, 9);
Snack apple = new Snack("Apple", .75, 0);
Snack chips = new Snack("potato chips", 1.25, 10);
Snack snickers = new Snack("Snickers", 1.75, 9);
//test is subclass
Product testing = apple; //warning okay
Snack cloned = (Snack)crunch.clone();
//test equals
System.out.println("Test equals: " + cloned.equals(crunch));
System.out.println("Expected: true");
System.out.println("Test equals: " + snickers.equals(crunch));
System.out.println("Expected: false");
//test clone and toString
cloned.increasePrice(10);
System.out.println(crunch.toString());
System.out.println("Expected: Snack[description=Nestle's Crunch,price=1.75][gramsOfFat=9]");
System.out.println(cloned.toString());
System.out.println("Expected: Snack[description=Nestle's Crunch,price=1.925][gramsOfFat=9]");
//test getGramsOfFat
System.out.println(chips.getGramsOfFat());
System.out.println("Expected: 10");
//test isHealthier
System.out.println("crunch isHealthier: " + crunch.isHealthier(snickers));
System.out.println("Expected: false");
System.out.println("apple isHealthier: " + apple.isHealthier(snickers));
System.out.println("Expected: true");
System.out.println("snickers isHealthier: " + snickers.isHealthier(apple));
System.out.println("Expected: false");
//test compareTo
Snack[] snacks = {snickers, apple, chips, crunch};
Arrays.sort(snacks);
System.out.println(Arrays.toString(snacks));
System.out.println("Expected: [Snack[description=Apple,price=0.75][gramsOfFat=0], Snack[description=Nestle's Crunch,price=1.75][gramsOfFat=9], Snack[description=Snickers,price=1.75][gramsOfFat=9], Snack[description=potato chips,price=1.25][gramsOfFat=10]]");
}
}
_______________________
Test equals: true
Expected: true
Test equals: false
Expected: false
org.students.Snack[description=Nestle's Crunch,price=1.75] [Grams Of Fat=9]
Expected: Snack[description=Nestle's Crunch,price=1.75][gramsOfFat=9]
org.students.Snack[description=Nestle's Crunch,price=1.925] [Grams Of Fat=9]
Expected: Snack[description=Nestle's Crunch,price=1.925][gramsOfFat=9]
10
Expected: 10
crunch isHealthier: false
Expected: false
apple isHealthier: true
Expected: true
snickers isHealthier: false
Expected: false
[org.students.Snack[description=Apple,price=0.75] [Grams Of Fat=0], org.students.Snack[description=Snickers,price=1.75] [Grams Of Fat=9], org.students.Snack[description=Nestle's Crunch,price=1.75] [Grams Of Fat=9], org.students.Snack[description=potato chips,price=1.25] [Grams Of Fat=10]]
Expected: [Snack[description=Apple,price=0.75][gramsOfFat=0], Snack[description=Nestle's Crunch,price=1.75][gramsOfFat=9], Snack[description=Snickers,price=1.75][gramsOfFat=9], Snack[description=potato chips,price=1.25][gramsOfFat=10]]
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.