Question: [Java] Please fix the problem. Don't just copy and paste the answer which already exists in the chegg system. It's wrong. Do not make any

[Java] Please fix the problem. Don't just copy and paste the answer which already exists in the chegg system. It's wrong.

Do not make any changes in the Product or SnackTester.

And PLEASE CHECK YOUR CODE AT THE LINK I PROVIDE BELOW. THE OUTPUT HAS TO BE EXACTLY SAME WITH THE PROGRAM.

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

/** * 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); } } 

SnackTester.java

import java.util.Arrays; public class SnackTester { public static void main(String[] args) { //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]]"); } 

[Here's the snack class]

It gives me an error

SnackTester.java:16: error: unreported exception CloneNotSupportedException; must be caught or declared to be thrown Snack cloned = (Snack)crunch.clone(); 

public class Snack extends Product implements Comparable,Cloneable{

private int gramsOfFat; public Snack(String theDescription, double thePrice,int gramsOfFat){ super(theDescription,thePrice); this.gramsOfFat=gramsOfFat; } public int getGramsOfFat(){ return gramsOfFat; } public boolean isHealthier(Snack snack){ return (gramsOfFat

// Overriding the compareTo method public int compareTo(Snack snack) { if((this.gramsOfFat-snack.getGramsOfFat())!=0) return (this.gramsOfFat-snack.getGramsOfFat()); else if((super.getPrice()-snack.getPrice())!=0) return ((int)(super.getPrice()-snack.getPrice())); else if (super.getDescription().compareToIgnoreCase(snack.getDescription())>0) return 1; else if(super.getDescription().compareToIgnoreCase(snack.getDescription())<0) return -1; else return 0; } public Object clone()throws CloneNotSupportedException { return super.clone(); }

@Override public String toString() { return super.toString()+"[gramsOfFat="+gramsOfFat+"]"; }

@Override public boolean equals(Object otherObject) { if (otherObject == null) {return false;} if (this.getClass() != otherObject.getClass()) {return false;} Snack other = (Snack)otherObject; return super.getPrice() == other.getPrice() && super.getDescription().equals(other.getDescription())&&this.gramsOfFat==other.getGramsOfFat(); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!