Welcome back to CS 124! You are not alone.
This lesson is a lot of fun. First, we’ll introduce you to the idea of writing tests, which is somewhat we’ll continue to train you on for the rest of the semester. Next we’ll spend some time on our first discussion of Java internals. Specifically, we’ll explain more about what happens when your code is run. Finally, we’ll discuss Java type inference.
On upcoming lessons you’ll encounter a new kind of homework problem. Instead of providing a solution, you’ll be challenged to write tests. For your tests to pass, they must accurately distinguish between incorrect and correct submissions. (Note that we will not include this kind of problem on quizzes this semester.)
Why are we teaching you to write tests? For several reasons. First, writing tests is a very common part of software development. Usually when you contribute code to a project, you’re not just expected to provide the code that implements a new feature, but also the tests that ensure that your code works correctly. Writing tests also ensures that you understand the problem description, in a more direct way than providing a solution.
But the most important reason we’re teaching you to write tests is that it trains you to think defensively about mistakes that you might make while solving the problem. Authoring an accurate test suite requires enumerating all the things that a programmer—such as yourself—might get wrong:
null?Test suites train that little voice in your head that can guide you away from common errors. Train it well, and you’ll avoid common pitfalls, make fewer mistakes, and spend less time debugging.
We’re going to work our way into writing tests incrementally. We’ll start by introducing some terminology to define what we mean by a test and by a test case. We’ll also explain the relationship between the method being tested and the test itself.
Next, let’s discuss how to write a test for a simple method that we’ve completed before.
Now let’s complete a practice test correctness problem.
Write a method named arrayAllMultiples that, given an array of int values greater than 0, returns whether the
values are all integer multiples of one of the values in the array, which we'll call the base.
For example, given the array {4, 2, 8} you would return true, since 4 and 8 are integer multiples of the base 2.
Given the array {2, 2, 5} you would return false, since there is no value that all others are integer multiples
of.
(If the array contains 1, you should always return true, but there's no need to handle this case specifically.)
You should approach this problem in two steps. First, identify the base—the value in the array that you are going to check whether others are multiples of. This may sound complicated, but it's straightforward. You do not need to and should not check whether every value is the base! Your solution should not contain a nested loop.
Next, check all the values in the array and look for a counterexample—a value that is not a multiple of the base.
The passed array may be null or empty.
In those cases you should return false.
Valid arrays will contain only integers strictly greater than 0.
Your challenge is to write tests for the problem described above.
Our focus in this class is on teaching you to think computationally and express those thoughts coherently in the Java programming language. Once you do that, you’ll be able to learn other programming languages easily and accomplish other computational tasks.
So we don’t spend a lot of time on exactly what is going on under the hood when Java executes the code that you submit. On some level, it doesn’t matter. As long as the core language constructs have a stable meaning, the internals of how things work don’t matter. When I write:
I expect to see a message displayed 8 times. Java could and might completely change exactly how that is accomplished, but as long as it is accomplished, I’m not going to notice. This is a powerful idea in computer science, even if at times the methods of implementation are somewhat less than practical.
But! In this case having a bit of an understanding of what is happening is actually useful. And, what is actually happening is quite interesting! So let’s peek under the hood…
When you run a Java program there are actually two distinct steps that are taking place:
Let’s talk a bit about when compilation happens, what is produced, and the kind of errors that the compiler can produce during this step.
Now let’s return to the same environment and discuss the execution step and the kind of errors that can occur at that point.
One of the reasons understanding these steps is so important is that they have a relationship to the difference between development and production:
Compiler errors generally only happen in development. That means that only developers see them! In contrast, runtime errors can happen in production! That means they might affect actual users.
Software developers acquire a high degree of patience with broken and crashy software. Users do not. That means that, if you can reduce runtime errors and catch them during development, you will produce better software. A new generation of compilers aim to do just that. Which would prevent you being able to do this, which always strikes me as insanely dumb:
Given a List of Strings, write a method toSet that converts the List to a Set of Strings that contains
all the same Strings in the original list.
So given a List containing "test", "me", and "test", you would return a Set containing "test" and "me".
assert that the passed List is not null.
Before we conclude, let’s look at a new Java feature: type inference.
Let’s return to the very beginning of our experiences with Java. As a reminder, Java requires that we provide a type when we declare a variable:
This is a variable named i that can store values of type int.
We know this.
But it’s more common to both declare and initialize a variable in the same statement:
This is a variable named i that can store values of type int that we initialize to 0.
We know this too.
But let’s look more carefully at this common and seemingly innocuous statement:
Notice something interesting?
We’ve actually told Java the type of the variable i twice.
Once through the declaration, but a second time through initialization.
Let’s split them apart:
So the Java compiler could determine the type based on how we initialize the variable. This is known as type inference, since the compiler infers the variable type based on how it is used. (Assignment is just one example of usage. We’ll look at another.)
The Java compiler is not particularly sophisticated. But, since Java version 10, it can infer types in certain situations. Here’s how to enable that:
Pretty cool! Note that this limited form of type inference is available in our playgounds, and for you to use on your homework problems. (It may not work when we get to the Android project, because Android is using an older version of Java.)
Please continue to work on the more challenging problem from the previous lesson. Good luck! Jump on the tutoring site or find us on the forum when you need some assistance. And keep in mind that this is just one problem…
Need more practice? Head over to the practice page.