Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Java question. Write a Snack class as a subclass of Product . In addition to description and price, Snack has integer instance variable gramsOfFat.

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

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

---------------------------------------------------------------------------------------------------------------------

I get the code of Snack.java from a TA like this :

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

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 (originalSnkparamPrice) val=1; else if(origPrice

}

They show the output and it seen no problem. However, when I run it in CodeCheck, it found an error and cannot run correctly, the error is as follow:

image text in transcribed

Could somebody help me out with this? Thank you: - )

Running snackTesterjava Error: tmp code check 17041723472963869475381505372 submission snack. java: 1: error: class, interface, or erum expected Snack. java 1 error

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions