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

Write a Snack class as a subclass of Product. In addition to description and pri

ID: 3815047 • Letter: W

Question

Write a Snack class as a subclass of Product. In addition to description and price, Snack has 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 Snack with 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.

Use the following files:

**************************Product.java***********************************

*********************SnackTester.java**************************

Explanation / Answer

Note: If we compare two Snacks objects whether they are equal or not by comparing grams of fat,then Nestle Crunch and Snickers both are same.As they have 9 grams of fat each.So they are same.Then Why the result expect as No equal (false).

______________________

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())
           return false;
       return true;
   }

   @Override
   public String toString() {
       System.out.println(super.toString());
       return "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]]");
  
}
}

_______________________

Output:

Test equals: true
Expected: true
Test equals: true
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]
org.students.Snack[description=Snickers,price=1.75]
org.students.Snack[description=Nestle's Crunch,price=1.75]
org.students.Snack[description=potato chips,price=1.25]
[Grams Of Fat=0, Grams Of Fat=9, Grams Of Fat=9, 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote