On this page:
Problem 1: Abstracting over Data Definitions
Problem 2: Working with Custom Constructors, A Look at Equality
Problem 3: Playing with lists of numbers

Assignment 3: Abstracting over Data Definitions; Custom constructors; Accumulators🔗

Goals: Learn to use the String class, practice working with lists. Learn to design methods for complex class hierarchies.


This assignment is long. Start early.

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 system may have a long queue of submissions which means it takes longer for your code to be submitted - so try to finish early.

The submission will be organized as follows:

Due dates:
  • Examplar: Saturday, January 27th at 9:00pm

  • Implementations: Tuesday, January 30th at 9:00pm

Problem 1: Abstracting over Data Definitions🔗

Related files:

In this problem, our data will represent different kinds of drinks you might order at a coffee shop. Each drink has several attributes, some of which are common to them all and some of which differ.

Note: none of these methods are properly implemented. As given in the file, they are all 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.

Submit your work in a file named Beverages.java.

Problem 2: Working with Custom Constructors, A Look at Equality🔗

When dyers create pigment mixes to dye yarn, they combine amounts (in grams) of red, yellow, blue and black pigments. A perfect recipe for a dye mix ensures the relative amounts of the four colors are appropriate:

Design a DyeRecipe class. The fields should be of type double and represent the weight of the four dye colors in grams. A dye recipe is always scaled such that the total amount of dye is 1g. Provide three constructors for this class:

You should use an IllegalArgumentException with a helpful message if the above constraints cannot be enforced.

Once you’ve completed the above constructors, you should:
  • Remove as much duplicate code as possible from these constructors. (Hint: you may want a Utils class to help with computing or enforcing some of the constraints.)

  • Implement the method sameRecipe(DyeRecipe other) which returns true if the same ingredients have the same weights to within 0.001 grams.

Submit your work in a file named Dyes.java.

Problem 3: Playing with lists of numbers🔗

Consider a simple ILoInt with standard empty and cons classes. Design the following methods, and abstract and generalize as much as you can.

You may need to design several helper methods, and possibly helper data definitions. Think through all the parts of this problem before starting to code — you should hopefully find commonalities that make the problems easier.

Submit your work in a file named Numbers.java.

Examplar submissions

Related files:

For this problem, we are going to to supply an additional submission on Handins. You will write examples and test cases against a placeholder implementation of the classes and interface needed for this problem, and we will run your test cases against multiple implementations: both correct and buggy! Your goal is to write sufficient test cases to distinguish all the correct implementations from all the buggy ones. We call the correct implementations wheat, and the buggy ones chaff: your goal is to separate the wheat from the chaff.

To do this:
  • Create a separate project for this tests-only assignment.

  • Download the lists.zip file and unzip it into your src/ directory: it should create a lists subdirectory with five files in it.

  • Create a file ExamplesLoString.java in your src directory, with the following initial contents:
    import lists.*;
    import tester.Tester;
    class ExamplesLoInt {
    // your examples go here }

  • You must not modify the supplied Java files: they’re provided only to ensure that your tests match the types expected by our implementations.

  • You do not need to develop examples for any methods not given by these stubs.

You will submit only the ExamplesLoInt.java file; do not submit the support files we gave you.

The Examplar for this problem will be due before the coding assignment is due; pay close attention to the deadlines!