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

Imports and Libraries

import java.util.Random;
import java.util.Date;
import java.util.Arrays;
Date date = new Date();
Random random = new Random();
if (random.nextBoolean()) {
System.out.println(date.getTime());
} else {
boolean[] values = new boolean[8];
Arrays.fill(values, false);
System.out.println(Arrays.toString(values));
}

This may be the most important lesson of the entire semester! All kinds of other people are creating and freely sharing useful Java code! And now you’ll learn how to build on top of those contributions. Don’t try to do it yourself! You are not alone…

Objects Beyond String
Objects Beyond String

So far our understanding of Java objects has focused on Strings. We’ve seen how to create them using new:

String s = new String("test");

And how to access useful String methods, like length, substring, and split. And we’ve used these methods to solve problems:

String s = new String("test@illinois.edu");
String[] parts = s.split("@");
System.out.println(parts[0]);

Strings are built in to Java, meaning that we don’t need to use import statements to access them. But Java allows you to access lots of other kinds of objects that might be useful for solving other kinds of problems! This lesson looks at how to access these other objects in your own code and provides some examples of useful objects and libraries.

import and Libraries
import and Libraries

Computer science is a remarkably collaborative field. In no other pursuit are millions of people all across the world so freely willing to share their creations!

Because of this, changing the world through your code has never been easier. Let’s see how!

First, let’s see how we find interesting and useful Java code that is part of the Java standard library.

Next, let’s see how to actually load those library classes into our project.

// import

Practice: Format Date Method

Created By: Geoffrey Challen
/ Version: 2020.10.0

Complete the method below formatDate. It should accept a positive long representing milliseconds since 1970 and return a String containing the ISO-8601 representation of this time. Here is one example for a recent timestamp: given 1602106609897 your function should return "2020-10-07T21:36:49.897Z".

Do not overthink this. The solution we are after is a single line of code. We suggest that you explore the various built-in Java libraries for working with dates and times.

We have provided starter code so that you can tell where the import statements should go. Ignore the public class Question stuff, since we haven't covered that yet.

// Import statements here
public class Question {
String formatDate(long millis) {
return "";
}
}

Example Libraries
Example Libraries

Now let’s look at a few useful parts of the Java Standard Library. No claim that these are the most useful parts! They’re just a few examples of code that is already out there for you to use!

java.util.Random
java.util.Random

Looking to make your Java programs more interesting? Try introducing some randomess! Let’s see how.

// java.util.Random

java.util.Arrays
java.util.Arrays

Arrays are useful, but they seem to be missing a few features. Let’s explore where they are hiding out!

// java.util.Arrays

java.math
java.math

Computers can do math, right? That might be useful! Let’s get past +, -, *, and /.

// java.util.Arrays

Practice: Compare Date Strings

Created By: Geoffrey Challen
/ Version: 2021.9.0

Completed the method below called compareDates that, given two non-null Strings containing datetimes in ISO-8601 format, returns -1 if the first time is before the second, 1 if the second is before the first, and 0 if they are equal.

When you are done, here is how your method should work:

You will want to approach this in two steps. First, convert each String into some kind of datetime representation. We suggest that you explore the various built-in Java libraries for working with dates and times. Don't attempt to do this yourself!

Next, use the resulting object to compare the two datetimes. We might suggest that you explore the java.time.Instant class and its parse and other methods. We have provided some starter code so that you can identify where the import statements should go. Ignore the public class Question stuff, since we haven't covered that yet.

// Import statements here
public class Question {
int compareDates(String first, String second) {
return 0;
}
}

Collections
Collections

One of the most important part of the Java Standard Library is the collections framework. It provides a variety of different ways of store things.

We certainly couldn’t do it justice here. But we’ll be covering several of these incredibly useful classes over the next few lessons!

Homework: Binary Search with Library

Created By: Geoffrey Challen
/ Version: 2022.9.0

Complete a method named findValue which accepts an int[] and an int value and returns whether the array contains the passed value. Your method should not modify the passed array. You can assume the array passed to findValue is not null.

However, you should complete this problem without using a loop, and entirely using Java's built-in array methods found in java.util.Arrays. There are several methods there that you will find helpful. But, as always, it is good to formulate your algorithm first.

More Practice

Need more practice? Head over to the practice page.