Conditional Expressions and Statements : 08/24/2023
Operations on Variables : 08/23/2023
Variables and Types : 08/22/2023
Welcome to CS 124 : 08/21/2023
Introduction to Objects
classExample {
intvalue;
}
Exampleexample=newExample();
System.out.println(example.value);
example.value=10;
System.out.println(example.value);
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.
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.
A class definines 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:
Dog (class) v. my dog Chuchu (instance)
Computer (class) v. my laptop (instance)
Building (class) v. the Siebel Center for Computer Science (instance)
CS 124 student (class) v. you (instance)
Now, let’s continue the example above using our Person class and create some instances.
To create a new instance of a class we use the new keyword:
classExample {
intvalue=0;
}
// Assignment of a new Example instance to the variable e
Examplee=newExample();
// Assignment using local variable type inference
varanother=newExample();
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.
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.
Practice: Object with Public Value
Created By: CS 124 Staff
/ Version: 2021.9.0
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.
For this problem we're expecting only a method.
Don't define a class, include modifiers like static or public, add import statements, or add code outside the method declaration.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Practice: Compare Two Points
Created By: CS 124 Staff
/ Version: 2022.9.0
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.
For this problem we're expecting only a method.
Don't define a class, include modifiers like static or public, add import statements, or add code outside the method declaration.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Homework: Identify a Suspect
Created By: CS 124 Staff
/ Version: 2021.8.0
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-nullSet<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.