On this page:
Bagels
8.6

Assignment 6: Customizing Constructors

Goals: Practice overloading constructors, checking for data constraints, and utilizing static methods and constants.

You should submit one .java file containing the solution to this problem.

Be sure to properly test your code and write purpose statements for your methods. A lack of tests and documentation will result in a lower grade! Remember that testing requires you to make some examples of data in an examples class.

Bagels

A BagelRecipe can be represented as the amount of flour, water, yeast, salt, and malt in the recipe. A perfect bagel results when the ratios of the weights are right:

Design the BagelRecipe class. The fields should be of type double and represent the weight of the ingredients as ounces. Provide two constructors for this class:

Given that we are working with doubles, we need to be careful about number comparison. For this assignment, any two doubles within 0.001 of each other are considered equal. Math.abs will likely prove helpful.

  • Your main constructor should take in all of the fields and enforce all above constraints to ensure a perfect bagel recipe.

  • Provide another constructor that only requires the weights of flour and yeast, and produces a perfect bagel recipe.

Once you’ve completed and tested the above constructors, you should:
  • Implement the method boolean sameRecipe(BagelRecipe other) which returns true if the same ingredients have the same weights and test is.

  • Consolidate as much duplicate code as possible into appropriate static methods. If you have time, write explicit tests for these, but their thorough usage in other methods can count for test coverage if you don’t.