On this page:
Instructions
Practice Problems
Problem 1: Cake recipe
4.1 What to submit
Problem 2: Abstracting over Data Definitions
7.2

Assignment 4: Abstraction; Constructors

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 Bottlenose enforces. 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 Bottlenose submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the Bottlenose 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: Thursday, February 6th, 9: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.

Problem 1: Cake recipe

A cake recipe can be represented as the amount of flour, sugar, eggs, butter and milk in the recipe. A perfect cake is made when the ratios of the weights of the ingredients are right:
  • the weight of flour should be equal to the weight of sugar

  • the weight of the eggs should be equal to the weight of the butter

  • the weight of the eggs + the weight of the milk should be equal to the weight of the sugar (or flour)

Design the CakeRecipe class. The fields should be of type double and represent the weights of the ingredients as ounces. Provide three constructors for this class:

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

Provide another constructor that only requires one to enter the amount of flour, eggs and milk, in that order.

Provide another constructor that takes in the flour, eggs and milk in that order, but as volumes rather than weights and a boolean flag areVolumes.

You may assume that flour and milk volumes are measured in cups and each egg is measured as one unit of volume.

To convert from volumes to weights:
  • 1 cup of sugar = 7 ounces

  • 1 cup of flour = 4 and 1⁄4 ounces

  • 1 egg = 1 and ¾ ounces

  • 1 cup of butter = 8 ounces

  • 1 cup of milk = 8 ounces

Remove as much duplicate code as possible from these constructors.

Implement the method sameRecipe(CakeRecipe other) which returns true if the same ingredients have the same weights to within 0.001 ounces.

4.1 What to submit

Please submit your work in a file named CakeRecipe.java and put your examples and tests in ExamplesCakes.

Problem 2: Abstracting over Data Definitions

Related files:
  Entertainment.java  

Entertainment media are elements of the media that focus on delivering entertainment to the general public. These include magazines, television series, and podcasts. Every medium has a name and price. Different kinds of entertainment can also contain additional information as shown in the supplied Entertainment.java file.

Note: only the totalPrice() method is properly implemented. The others are stubs that currently return a dummy value, so the code will compile but not yet work.

Warmup: Download the file and work out the following problems:

Once you have finished these methods and are confident that they work properly, save the work you have done to a separate file. Do not submit the code as written so far. The problems below are the main point of this exercise, and it will be helpful for you to preserve the code written so far as a reference against which to compare your revised code below. Again, submit only the work below.