Assignment 4: Constructors; Structural Equality; Non-structural Equality
Goals: Learn to use custom constructors, determine equality via dynamic-dispatch, and determine non-structural equality.
Instructions
the names of classes,
the names and types of the fields within classes,
the names, types and order of the arguments to the constructor,
the names, types and order of arguments to methods, or
filenames,
Make sure you follow the style guidelines that we imposed. For now the most important ones are: using spaces instead of tabs, indenting by 2 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.
You will submit this assignment by the deadline using the handin submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the handin system may slow down to handle many submissions - so try to finish early.
There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem you work on.
The submissions will be organized as follows:
Homework 4 Problem 1: The BagelRecipe.java file
Homework 4 Problem 2: The CampusTour.java file
Homework 4 Problem 3: The Polynomial.java file
Problem 1: Thursday, October 26th, 10:00pm
Problem 2: Thursday, October 26th, 10:00pm
Problem 3: Thursday, October 26th, 10:00pm
Practice Problems
Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.
Problems 18.1 - 18.4 on page 225
Problem 18.5 on page 229
Problem 18.6 on page 234
Problem 19.4 on page 263
1 Problem 1 — Working with Custom Constructors, A Taste of Equality
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:
the weight of the flour should be equal to the weight of the water
the weight of the yeast should be equal the weight of the malt
the weight of the salt + yeast should be 1/20th the weight of the flour
Design the BagelRecipe class. The fields should be of type double and represent the weight of the ingredients as ounces. Provide three constructors for this class:
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.
Provide another constructor that takes in the flour, yeast and salt as volumes rather than weights, and tries to produce a perfect recipe. Here, too, a perfect recipe should be enforced.
Flour and water volumes are measured in cups, while yeast, salt, and malt volumes are measured in teaspoons.
48 teaspoons = 1 cup
1 cup of yeast = 5 ounces
1 cup of salt = 10 ounces
1 cup of malt = 11 ounces
1 cup of water = 8 ounces
1 cup of flour = 4 and 1⁄4 ounces
Remove as much duplicate code as possible from these constructors.
Implement the method sameRecipe(BagelRecipe other) which returns true if the same ingredients have the same weights to within 0.001 ounces.
2 Problem 2 — Bailey Hall Is Like The North Star!
Download and complete the CampusTour.java file (look for the // TODO: comments).
Sameness of tours is slightly subtler than structural sameness:
When a tour branches, the option1 and option2 destinations are arbitrary: one BranchingTour is the same as another BranchingTour if their two sub-tours are the same in either order, and their speeches are the same.
Two Quads are the same only when their names are the same and their locations are the same in the same order, as their ordering is specified in the data representation.
3 Problem 3 — Pesky Polynomials
To learn about polynomials in much greater depth, consider taking the Rings and Fields course offered by the math faculty.
Mathematically, polynomials are written as: a0x0 + a1x1 + ... + an-1xn-1 + anxn
Each expression of the form aixi is called a Monomial. Every monomial has a degree, a non-negative integer that x is raised to (i), as well as a coefficient, which xi is multipled by (ai). For this problem, our polynomials will be over the integers, which means all of the coefficients must also be integers; you do not have to worry about doubles in this problem. Make sure that only monomials with non-negative degrees can be constructed.
A Polynomial has just one field, monomials, which is an ILoMonomial. You should ensure that a Polynomial cannot be constructed if any of the monomials given to it have the same degree. However, the monomials can be in any order, e.g. 3x2 + 5 is the same polynomial as 5 + 3x2. Finally, monomials might be present with coefficients of zero, and these should not affect sameness: both the polynomials in the previous sentence are the same as 5 + 0x + 3x2.
Design the method samePolynomial which determines if two Polynomials represent the same polynomial.