RU beehive logo ITEC dept promo banner
ITEC 380
2014fall
ibarland

homelecturesrecipeexamshwsD2Lbreeze (snow day)

hw-ec-sort
sorting by design recipe
extra-credit review/practice problem

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. Exprs).

Write a function to sort a list, using the design recipe.


 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 (cons 7 (cons 4 (cons 2 (cons 9 (cons 8 empty))))). Thus, if using the Java class SList, this would be new ConsList(7, new ConsList(4, new ConsList(2, new ConsList(9, new ConsList(8, new EmptyList()))))).      

3 For processing a list, the pertinent pieces are: the input, the desired-output, and: (first your-input) (rest your-input) (sort (rest your-input)).      

homelecturesrecipeexamshwsD2Lbreeze (snow day)


©2014, Ian Barland, Radford University
Last modified 2014.Dec.07 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.