On this page:
Instructions
Practice Problems
1 Problem 1 — Working with Custom Constructors, A Taste of Equality
2 Problem 2 — Bailey Hall Is Like The North Star!
3 Problem 3 — Pesky Polynomials
8.13

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🔗

Be very, very careful with naming! Again, the solution files expect your submissions to be named a certain way, so that they can define their own Examples class with which to test your code. Therefore, whenever the assignment specifies:
  • 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,

...be sure that your submission uses exactly those names.

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:

Due Date:
  • Problem 1: Thursday Friday, October 4th, 10:00pm

  • Problem 2: Thursday Friday, October 4th, 10:00pm

  • Problem 3: Monday, October 7th, 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.

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:

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:

Flour and water volumes are measured in cups, while yeast, salt, and malt volumes are measured in teaspoons.

Helpful conversion factors:
  • 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

Once you’ve completed the above constructors, you should:
  • 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!🔗

Related files:
  CampusTour.java  

Download and complete the CampusTour.java file (look for the // TODO: comments).

Sameness of tours is slightly subtler than structural sameness:

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.