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

  • map-reduce-filter : 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

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 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

Functions and Algorithms

Functions are so important that we’re going to continue getting more practice with them. We’ll also discuss the connection between functions and algorithms, the conceptual heart of computer science.

Throughout this lesson we’ll return to bits of code that we previous implemented as snippets and reimplement them as methods. We’ll also use this as a chance to reinforce some important programming patterns that we’ve already been using.

Debugging Challenge
Debugging Challenge

But let’s start with some debugging practice!

Array Search
Array Search

Next, let’s revisit the code we wrote for array search, rewrite it is a method, and then test it using some sample inputs.

// Array Search

Array Is All Larger
Array Is All Larger

Our next practice problem has a tricky loop setup. Let’s review that together, and then you can attempt the problem itself.

// Array Is All Larger Setup
Created By: Geoffrey Challen
/ Version: 2022.2.0

Write a method arrayIsAllLarger that, given two Int arrays, returns true if all the values in the first array are larger than or equal to all the values in the same position in the second array. If the first array is longer than the second array, or the second array is longer than the first array, then the extra values can be ignored. If either array is empty, return false.

For example, given {1, 2, 4} and {0, 1, 7}, you should return false, since 7 is greater than 4. Given {1, 2, 4} and {0, 1} you should return true, since the first two values of the first array are greater than the first two values of the second array. Given {1, 2, 4} and {0, 1, 3, 8}, you should return true, since the first three values of the first array are greater than the first three values of the second array. Given {1, 2} and {1, 2}, you should return true, since all values in the two arrays are equal.

Array Sum
Array Sum

Now, let’s revisit the code we wrote for array sum, rewrite it is a method, and then test it using some sample inputs.

// Array Sum

Catchup Quiz Grading
Catchup Quiz Grading

As a bit of homework preparation support, let’s talk through how to approach this lesson’s homework problem, since it has another interesting loop setup. We’ll also demonstrate how we can modify the values of arrays that are passed to methods. And we’ll provide practice with a variant of the problem that you need to complete for today’s homework.

// Catchup Quiz Grading Setup
Created By: Geoffrey Challen
/ Version: 2022.2.0

Create a method named catchupGrading that accepts an array of Double values and implements the following catch-up grading policy. Given a score on Quiz N, if a student does better on the next quiz (Quiz N + 1), then you should replace their score on Quiz N with the average of their score on Quiz N and their score on Quiz N + 1. Otherwise, their score on Quiz N is unchanged. Return the number of times that the student does strictly better on the next quiz than they did on the previous one.

Scores are stored in the array in order. So, given an array with the scores {100.0, 80.0, 90.0}, you would modify the array to contain {100.0, 85.0, 90.0}, and return 1.

A few hints about how to approach this problem. You'll want to use a loop to go through each pair of scores in the array, so in the loop you'll be examining both one quiz and the next. This loop is similar to but a bit different from the one that we're used to writing. Your function should modify the values in the passed array.

Homework: Catchup Quiz Grading With Skips

Created By: Geoffrey Challen
/ Version: 2022.8.0

Create a method named catchupGradingWithSkips that accepts an array of Double values and implements the following catch-up grading policy. Given a score on Quiz N, if a student both took Quiz N and does better on the next quiz (Quiz N + 1), then you should replace their score on Quiz N with the average of their score on Quiz N and their score on Quiz N + 1. Otherwise, their score on Quiz N is unchanged. If a student does not take a quiz, their score is recorded as -1.0. Return the number of times that you apply the catchup grading policy—specifically that the student took the previous quiz and does strictly better on the next quiz than they did on the previous one.

Scores are stored in the array in order. So, given an array with the scores {100.0, 80.0, 90.0}, you would modify the array to contain {100.0, 85.0, 90.0}, and return 1. And, given an array with the scores {100.0, -1.0, 90.0}, you would not modify the array and return 0, because in this case the student did not take the second quiz.

A few hints about how to approach this problem. You'll want to use a loop to go through each pair of scores in the array, so in the loop you'll be examining both one quiz and the next. This loop is similar to but a bit different from the one that we're used to writing. Your function should modify the values in the passed array.

More Practice

Need more practice? Head over to the practice page.