Assignment 2: Designing methods for complex data
Goals: Learn to design methods for complex class hierarchies. Practice designing the representation of complex data.
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,
You will submit this assignment by the deadlines using the course handin server. Follow A Complete Guide to the Handin Server for information on how to use the handin server. You may submit as many times as you wish. Be aware of the fact that close to the deadline the server 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.
Remember that you should avoid accessing fields of fields and using any type-checkers. Design your methods systematically using the Design Recipe as we have been doing in class!
As always, you may only use techniques that have been covered in lectures so far in your solutions.
Tuesday, May 13th, 9:00 pm
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 10.6 on page 102
Problem 11.2 on page 113
Problem 12.1 on page 125
Problem 14.7 on page 140
Problem 15.2 on page 149
Problem 15.3 on page 149
Problem 15.8 on page 171
Problem 1: “Painting”
Define the file Paints.java that will contain the entire solution to this problem. You will need to import java.awt.Color at the top of your file, just like you import the tester library.
Well, not precisely. The math in this problem will deal with additive colors, like mixing light, whereas paint is more properly subtractive color... If the nuances of color mixing intrigue you, there’s plenty more to learn about how computers model color.
There are three kinds of mixtures: Darken (darkens a paint color), Brighten (brightens a paint color), and Blend (mixes two paint colors together).
Design the classes (and interfaces) needed to represent the given information.
Draw the class diagram for the classes and interfaces you have designed. You do not have to submit this, but this is the first assignment with a complex class structure, and understanding it is essential before proceeding.
In the ExamplesPaint class define example data that represents the following painter’s palette (the colors of the words are irrelevant, they change only to aid readability):
You should define each blob of paint by its name (e.g. "red" or "dark purple") —
but pay attention to Java naming conventions! Our test program will use this data to test your methods.
Add to your examples one more example of paint for each of the possible mixtures. Do not modify the existing paints.
If you’re curious, the method brighter produces a color with red, green, and blue values increased by some arbitrary scale factor. The method darker similarly produces a color with red, green, and blue values decreased by an arbitrary scale factor. The documentation for these methods deliberately does not specify the amount, and it may vary between different versions of Java.
Design the method getFinalColor that computes the final color of this paint. Note that brighter and darker are two methods available on Color objects (which is available when you import java.awt.Color at the top of your file). For blending you should add half of the red, green, and blue values for each of the paint colors involved in order to produce the blended color. (This isn’t quite the same thing as taking the average of the two colors —why?) Hint: follow the design recipe... working through examples really helps.
Design the method countPaints that computes the number of solid paints involved in producing the final color of this Paint. For a darkened paint we assume we added solid black and for a brightened paint we assume we added solid white.
Note: Make sure you count every paint each time it is referenced.
For example, dark purple is created by blending red and blue and then adding solid black (to darken it). Therefore the result of calling countPaints on the dark purple paint is 3.
Design the method countMixes that computes the number of times this paint was mixed in some way. For example, calling this method on the dark purple paint would produce 2 since it is blended once and then darkened once. If we created a paint that blended dark purple with itself it would have 5 mixes since each dark purple paint requires the two mixes described above and then we have to blend the two together.
Design the method formulaDepth, that computes how deeply mixtures are nested in the formula for this Paint. For example, the formulaDepth of dark purple is 2. If we blend dark purple with itself, the resulting paint we created would have a formula depth of 3.
Design the method invert, which produces the paint as is, except all Darken mixtures should become Brighten mixtures and vice versa. The names of all of the paints and the base colors should remain the same.
Tricky! Design the method mixingFormula that takes an integer depth, and produces a String representing the contents of this paint, where the formula for the paint has been expanded only depth times. For example, the mixingFormula at depth 0 for pink paint is "pink", at depth 2 is "brighten(blend(purple, khaki))", and at depth 3 or more is "brighten(blend(blend(red, blue), blend(red, green)))".
In more detail: invoking mixingFormula on a paint produces its name if the given depth is less than or equal to 0, and the formula of its mixture (at that depth) otherwise.
Hint: If you get stuck, you may want to use a wish list to determine subproblems that may be of use to you, and that you can delegate to when needed.