(61B FA24) Lecture 2 - Defining and Using Classes
(61B FA24) Lecture 2 - Defining and Using Classes
1
Classes in Java
• Defining and Instantiating
Classes
• Class Terminology
• Static vs. Instance Members
Defining and • Practice Question
Interactive Debugging
Instantiating • Goal: Larger Than Four
Neighbors
Classes • Using the Debugger
public class Dog { For those of you who know Python, the
public int weightInPounds; equivalent code is given below.
Example:
0 1
Classes in Java
• Defining and Instantiating
Classes
• Class Terminology
• Static vs. Instance Members
Static vs. • Practice Question
Interactive Debugging
Instance • Goal: Larger Than Four
Neighbors
Members • Using the Debugger
int i = 0;
while (i < manyDogs.length) {
Dog.maxDog(manyDogs[i],
mediumDog).makeNoise();
i = i + 1;
}
}
Answer to Question
Won’t go over in live lecture.
Use the Java visualizer to see the solution here: http://goo.gl/HLzN6s
Video solution: https://www.youtube.com/watch?v=Osuy8UEH03M
Classes in Java
• Defining and Instantiating
Classes
• Class Terminology
• Static vs. Instance Members
• Practice Question
Interactive Debugging
Interactive • Goal: Larger Than Four
Neighbors
Debugging • Using the Debugger
Today, we'll use IntelliJ's built-in, interactive debugging tool to find bugs in some
code.
Classes in Java
• Defining and Instantiating
Classes
• Class Terminology
• Static vs. Instance Members
Goal: Larger • Practice Question
Interactive Debugging
Than Four • Goal: Larger Than Four
Neighbors
Neighbors • Using the Debugger
A. [20]
B. [20, 22]
C. [20, 22, 20]
Goal: largerThanFourNeighbors
Suppose we want to write a method:
A. [20]
B. [20, 22]
C. [20, 22, 20]
Classes in Java
• Defining and Instantiating
Classes
• Class Terminology
• Static vs. Instance Members
• Practice Question
Interactive Debugging
Using the • Goal: Larger Than Four
Neighbors
Debugger • Using the Debugger
● When the program is paused, you can view the values of all the variables (as
if you had added print statements).
● You can also execute lines of code interactively in the "Evaluate expression"
box.
● IntelliJ highlights the line about to execute (has not executed yet).
● If the highlighted line contains a function call:
○ steps over the function call, and pauses at the next line after calling the
function.
Useful if you don't care about the code inside the function.
○ steps into the function call, and pauses at the first line of the function.
Useful if you want to step through the code inside the function.
public static int addOne(int x) {
int retValue = x + 1; Step into pauses the program
return retValue; here
}