A new era begins in CS 124.
We leave the familiar world of primitive types and Strings behind and strike out to new horizons.
This lesson begins our discussion of Java objects.
Objects represent both a significant conceptual step forward, and dramatically improve our ability to work with data.
So let’s get started!
Java is an object-oriented language. What does that mean, exactly?
We met our first object back when we started working with Strings, and defined one there as something that combines state and behavior.
An object’s state is the data that it holds, and its behavior is what it can do.
That is the definition we’ll keep using, and the rest of this lesson is an elaboration of it.
Other descriptions you’ll run across name the same pair by what Java builds it out of. Wikipedia defines an object as:
In the class-based object-oriented programming paradigm, “object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.
Variables store the state, and functions—which Java calls methods—provide the behavior. That definition and ours are the same definition at different levels of detail, so don’t let the extra words suggest there is a second idea here.
Let’s examine this together.
A class defines how an entire group of objects behaves.
For example, we might say that a person is a class of things where each has a name and an age.
Let’s look at our first class definition together:
Defining a class allows us to create instances of that class. Sometimes we use the term object and instance interchangeably, defining an object as an instance of a class.
If it helps understand the relationship between class and instance, here are some examples of this relationship between real-world things:
Now, let’s continue the example above using our Person class and create some instances.
Notice that each new instance starts out with its fields already holding a value, even though we never assigned one.
Numeric fields start at zero, boolean fields start at false, and fields of any object type—including arrays—start at null.
That last case is worth slowing down on, because null is not an empty String and it is not an empty array.
It means the field does not refer to an object at all yet:
So e.words.length would fail here, and will keep failing until we create an array and assign it to words.
newnewTo create a new instance of a class we use the new keyword:
For now the syntax of new will seem somewhat mysterious.
Why does it look like a function call?
That will make more sense soon.
But for now simply follow this template to create new class instances.
Objects allow us to create new Java types. These new types are in some ways just like the types that we’ve already been working with:
But Java classes allow us to include any mixture of different types of data by declaring one or more fields:
An important note: not everything about objects is going to make sense to you right away! That’s OK. We’re going to keep practicing, and things will become a bit more clear every day.
As a reminder: In CS 124, in addition to teaching you how to write correct code, we also want to teach you how to write good code: Code that’s beautiful and elegant and that you and those you work with will be proud of.
There are multiple aspects of code quality that we’ll be introducing throughout the semester.
Previously we talked about ensuring beautiful code formatting using checkstyle, and removing unnecessary debugging output.
checkstyle rules are now enforced on homework and quizzes, and starting today, there will be a one-point deduction for correct submissions that produce unnecessary debugging output.
The next aspect of code quality we’ll discuss is eliminating so-called dead code. What is dead code? It’s a portion of your program that is never executed during testing. Dead code usually indicates a misunderstanding of the problem specification, or of how control flows through your program.
We’ll be warning you about dead code starting today, and in a week you’ll lose one point on the graded homework if you don’t eliminate unneeded code. Let’s see an example of how that works using a familiar practice problem:
Write a method getExampleValue that, given a passed instance of an Example, returns that Examples value
property, which is an int.
The Example class looks something like this:
You should also assert that the passed Example is not null.
Write a method compareTwoPoints that, given two passed Point instances, return true if they
refer to the same position and false otherwise.
The Point class looks something like this:
Two Point instances refer to the same position if they have the same x and y values.
There was a theft of research hardware on campus last night. Based on eyewitness accounts, they figured out the suspect went through the Siebel Center for Computer Science, the Digital Computing Laboratory, and the Illini Union. Luckily, you have the lists of the people who entered each building from their I-Card ID swipes.
You've been given three non-null Set<String>s that represent the lists of people that entered each building
yesterday.
Your job is to create a function called calculateSuspects that when given these parameters returns the list of
suspects as a Set<String>.
This list should consist of all the names of all people who are included in at least two of the Sets.
You may find reading the documentation of addAll (union) and retainAll (intersection) helpful.
You may assume that no inputs will be null, however, given Sets may be empty.
Readings:
Note that you do not need to import Set or HashSet, since they are already available.
Need more practice? Head over to the practice page.