KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

MP1: Filtering and Search

Let’s wrap up MP1! We’ll complete a search method and use it to get the search bar displayed in the UI to work.

Quick Layout File Fix
Quick Layout File Fix

First up, let’s put in a quick fix to one of the layout files that shipped with your MP starter code. (Note that if you started the MP quite late you might already have this fix.)

Filter Method
Filter Method

Before we proceed, please make sure that the first two MP1 test cases are passing: test0_SummaryComparison and test2_SummaryView.

Completing the next test requires completing the Summary static method named filter. filter accepts a List<Summary> and String, in that order. filter both filters and sorts the passed list based on the passed String, as described below.

You should trim the passed String of leading and trailing whitespace, and all String comparisons should be case insensitive. Do not modify the List<Summary> that filter is called on. Filter the list to include only summaries where the search term appears somewhere in the title of the course, where the title String is defined by the toString method: for example, “CS 100: Computer Science Orientation”.

Next, you should sort the remaining summaries twice. First, use the default sort you completed earlier. Next, sort the summaries by the position of the search term, with earlier matches appearing first. Return the sorted list of summaries.

Example Filter Output
Example Filter Output

For example, consider the following list of summaries:

CS 101: Introduction to Computing
STAT 100: Introduction to Statistics
IS 100: A Gentle Introduction to Information Science
CS 107: Data Science Discovery

If the filter method was passed the String ” INTRO ”, here’s what you would do:

  1. Remove “CS 107: Data Science Discovery”, which does not match the search
  2. Sort the remaining courses by their number, followed by their subject—the default sort.

At this point we have the following ordered list of summaries:

IS 100: A Gentle Introduction to Information Science
STAT 100: Introduction to Statistics
CS 101: Introduction to Computing

Finally, sort the list by the position of the search term in the title. With the final result being:

CS 101: Introduction to Computing
STAT 100: Introduction to Statistics
IS 100: A Gentle Introduction to Information Science

Flexible Sorting
Flexible Sorting

Completing the filter method requires sorting the list of summaries in a way different than the default sort provided by compareTo. Let’s examine how to do that together.

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class Person implements Comparable<Person> {
private String name;
private int age = 0;
public Person(String setName, int setAge) {
if (setName == null || setAge < 0) {
throw new IllegalArgumentException("Invalid name or age");
}
name = setName;
age = setAge;
}
@Override
public int compareTo(Person other) {
return name.compareTo(other.name);
}
@Override
public String toString() {
return name + " (" + age + ")";

Search UI
Search UI

Once your filter method is working, let’s use it to fix the search bar in your app’s user interface! Doing this is not difficult, but it will serve as our first introduction to how Android’s UI works. So let’s take it step-by-step and not miss the opportunity to learn some new things along the way.

Android App Layout
Android App Layout

First, let’s examine the layout of our MainActivity, and begin discussing the design of our app. Why does it look the way it does? How could we change that? This will also provide us with a starting point for the work we’ll need to do in MainActivity.java to enable the search bar.

Handling Search Bar Changes
Handling Search Bar Changes

The next thing we need to do is figure out how we can respond to changes to the search bar. This is our first example of a UI event and a callback design pattern used to handle the event, and so we’ll also explore that idea in detail. Once we’re done, you’ll be in a position to wrap up this checkpoint!

Wrapping Up
Wrapping Up

At this point we’ve set up our MainActivity to respond to changes in the search bar. The last thing to do is to update the list of summaries shown in the list properly!

Here’s what to do. First, perform the search. Then update the list of displayed summaries accordingly.

Your Goal Today
Your Goal Today

As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.

Right now your goal should be to complete the filter method and UI changes and wrap up MP1! If you get stuck, find us for help on the tutoring site or forum.

MP1 Scores
MP1 Scores