Skip to content

Commit

Permalink
Merge pull request #76 from LaunchCodeEducation/mike-issue-94
Browse files Browse the repository at this point in the history
Added steps to explain iterating and showing flavor sorting results i…
  • Loading branch information
mlambert125 authored Nov 26, 2024
2 parents 8f9c619 + d03cb4c commit f340497
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions content/interfaces/exercises/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,49 @@ public static void main(String[] args){
}
```

1. Iterating through the `flavors` list before and after the sort shows the results. (The output below displays just the `name` fields).
1. Next, add some code to iterate over the flavors list and print out the flavors before and after sorting.

```java{linenos=table,hl_lines=[],linenostart=6}
public static void main(String[] args){
Case menu = new Case();
ArrayList<Flavor> flavors = menu.getFlavors();
ArrayList<Cone> cones = menu.getCones();
Comparator comparator = new FlavorComparator();
System.out.println("Before:");
System.out.println();
for (Flavor flavor : flavors) {
System.out.println(flavor.getName());
}
flavors.sort(comparator);
System.out.println();
System.out.println("After:");
System.out.println();
for (Flavor flavor : flavors) {
System.out.println(flavor.getName());
}
}
```

1. Running the application, shows the list before and after the sort.
```bash
Before: After:
Before:

Vanilla
Chocolate
Red Velvet
Rocky Road
Strawberry Sorbet

After:

Vanilla Chocolate
Chocolate Red Velvet
Red Velvet Rocky Road
Rocky Road Strawberry Sorbet
Strawberry Sorbet Vanilla
Chocolate
Red Velvet
Rocky Road
Strawberry Sorbet
Vanilla
```

Note that `Main` does NOT have to implement the `Comparator` interface. This only needs to happen in the class that actually uses the `compare` method.
Expand Down Expand Up @@ -181,4 +215,4 @@ To fix this, use an `if/else if/else` block to evaluate `o1.getCost() - o2.getCo

## Next Steps

In these exercises, you practiced implementing existing interfaces. In the studio activity, you will design and implement your own.
In these exercises, you practiced implementing existing interfaces. In the studio activity, you will design and implement your own.

0 comments on commit f340497

Please sign in to comment.