![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
The following program is a basic review on
following the design-recipe, specific to lists.
I'll give 15pts of extra-credit (homework) points.
As you complete it, think about how the same design-recipe
steps guide you when writing code for other types of data (e.g.
Write a function to sort a list, using the design recipe.
Remember, the only non-rote thinking is: “what is the general rule I want, to combine the particular pieces given to me by the template?”. The inventory-with-values gives you a concrete example to help you, when you try to formulate that rule in the following step.
Hint: for the helper's inventory-with-values, use7 and(list 2 4 8 9) as your example-inputs.
1 abstract class SList { 2 } 3 4 5 6 class EmptyList extends SList { 7 // No fields. 8 9 // Java's default constructor does what we want, so we won't provide one. 10 11 @Override 12 public String toString() { return "new EmptyList()"; } 13 } 14 15 16 class ConsList extends SList { 17 int n; 18 SList rest; 19 20 21 // Provide the boilerplate constructor: 22 ConsList( int _n, SList _rest ) { 23 this.n = _n; 24 this.rest = _rest; 25 } 26 27 @Override 28 public String toString() { 29 return "new ConsList( " + this.n + ", " + this.rest.toString() + ")"; 30 } 31 } 32 |
1Well, non-decreasing order — we want to allow duplicates. ↩
2
Of course, this is shorthand for
3
For processing a list, the pertinent pieces are:
the input, the desired-output, and:
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2014.Dec.07 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |