CS-2510 Assignment 3

Due: Sept 29th @ 10:00 pm

Purpose:

To practice designing classes and methods: taking problem and data descriptions and adding useful computations.




Practice Problems (Not Graded):

HtDCH Exercises: 10.4, 10.7, 10.9

Programming Problem (Fruit.java):

You've been hired by Half Foods Market to revamp their inventory system. As the name might suggest, the company has a limited offering of produce. Complete the problems below, saving them in a file, Fruit.java. Be sure to put your names and the assignment number at the top in a comment. Work together; pair programming means working as a pair, not dividing the work.

A Simple Class

  1. Design a class to represent Apples. Each apple has a kind (e.g., "Granny Smith"), weight (in grams), and a color, and price (per gram).
  2. Create a FruitExamples class and a few examples of Apples.

Some Simple Methods

Remember to follow the recipe! Don't forget your purposes and write test methods!

  1. Design a method cost that calculates the cost of this Apple.
  2. Design a method pounds that calculates the weight of this Apple in pounds. (Ask Google about the conversion...)
  3. Design a method label that computes a label (String) for this Apple. The label is something the store can print to identify items in the produce section, e.g., "Granny Smith Apples: $2.46 per pound". Note that the label is displayed in pounds, not grams.

A Union

  1. Design a union (interface and classes) to represent Fruit. In addition to apples, the union consists of two other kinds of fruits: Oranges and Grapes. Modify your Apple class so it's part of the union.

    Each new kind of fruit (just like Apple) has a kind, weight (in grams), and a price. In addition, Grapes contains a field that signifies whether or not they are sour.
  2. Add examples of each new fruit to your FruitExamples class.

Methods for Interfaces and Classes

Remember to follow the recipe! Don't forget your purposes (even in the interface) and write test methods!

  1. Design the method cost for the interface and new classes.
  2. Design the method pounds for the interface and new classes.
  3. Design the method label for the interface and new classes.
  4. Design the method makeHeavy for the interface and classes that increases this Fruit's weight by the given amount (in grams).
  5. Design the method isSour for the interface and classes that returns whether this Fruit is sour. Only Grapes can be sour, Apples and Oranges always return false.

Lists and Methods

Remember to follow the recipe! Don't forget your purposes (even in the interface) and write test methods!

  1. Design an interface and classes to represent lists of fruit. Name them: ILoF, ConsLoF, MtLoF.
  2. Create examples of lists. Make sure you have at least three instances: one with no fruit, one with a single element, and one with at least 3 elements.
  3. Design the method totalCost for your list interface and classes.
  4. Design the method makeHeavy for your list interface and classes that adds the given amount (in grams) to each of the Fruits in this list.
  5. Design the method removeSour for your list interface and classes that returns a new list containing only the non-sour Fruit from this list.