[61B FA24] Lecture 1 - Introduction
[61B FA24] Lecture 1 - Introduction
es
Lecture 1
2
61B Overview
What is 61B about?
● Writing code that runs efficiently.
○ Good algorithms.
○ Good data structures.
● Writing code efficiently.
○ Designing, building, testing, and debugging large programs.
○ Use of programming tools.
■ git, IntelliJ, JUnit, and various command line tools.
○ Java (not the focus of the course!)
6
Joining the Course
● Course staff does not control getting into the course.
○ Contact a major advisor if you have questions.
● Being added to course platforms (Ed, Gradescope) is manual. We do
this every day. Please do not email us asking to be added to the
platforms unless it’s been more than 2 days since you joined the course.
● If you are a pending concurrent enrollment student, you should be
added to bCourses, Ed, and Gradescope 1–2 days after you've
submitted your application.
○ We cannot process/approve applications until the department tells
us to. We will post on Ed if there are updates.
○ Do not email course staff about getting added to the course
platforms unless it’s been more than 2 days since you’ve
submitted your application.
Course Components
Lectures provide you with an introduction and a foundation.
There is no partial credit for work submitted late. Gradescope gives zero
points by default to late work.
16
Intro to Java
Let’s try writing some simple Java programs.
● First I’ll write them in Python (~99% of you have seen Python).
● Then I’ll write the equivalent Java program.
If you’ve only ever written code in MATLAB, this will be a little harder for you, but
still comprehensible.
*: This is not completely true, e.g. we can also declare “interfaces” in .java files
that may contain code. We’ll cover these soon.
• Welcome!
• Welcome to 61B
• 61B Logistics
• Our First Java Programs
• Hello World
• Hello Numbers
• Larger
• Reflections on Java
• Object-Oriented Programming
• HW0: Due Friday!
Hello Numbers • Bonus Slides: Workflow
• Compilation
• IntelliJ
Lecture 1, CS61B, Fall 2024
21
Java and Static Typing
Reflections on Hello Numbers:
● Before Java variables can be used, they must be declared.
● Java variables must have a specific type.
● Java variable types can never change.
● Types are verified before the code even runs!
28
Larger: Reflections
● Functions must be declared as part of a class in Java.
A function that is part of a class is called a "method."
So in Java, all functions are methods.
● To define a function in Java, we use "public static".
We will see alternate ways of defining functions later.
● All parameters of a function must have a declared type,
and the return value of the function must have a declared type.
Functions in Java return only one value!
• Welcome!
• Welcome to 61B
• 61B Logistics
• Our First Java Programs
• Hello World
• Hello Numbers
• Larger
• Reflections on Java
Reflections on •
• Object-Oriented Programming
HW0: Due Friday!
Java • Bonus Slides: Workflow
• Compilation
• IntelliJ
Lecture 1, CS61B, Fall 2024
34
Compilation vs. Interpretation
In Java, compilation and interpretation are two separate steps.
Compiler Interpreter
stuff
Hello.java javac Hello.class java happens
The Bad:
● Code is more verbose.
● Code is less general, e.g. would need a second larger function to compare
non-integers like 5.5.
• Welcome!
• Welcome to 61B
• 61B Logistics
• Our First Java Programs
• Hello World
• Hello Numbers
• Larger
• Reflections on Java
Object-Oriented • Object-Oriented
Programming
•
Programming
HW0: Due Friday!
• Bonus Slides: Workflow
• Compilation
Lecture 1, CS61B, Fall 2024 • IntelliJ
37
CS61A Review: Object-Oriented Programming
● A model for organizing programs.
○ Modularity: Define each piece without worrying about other pieces, and
they all work together.
○ Allows for data abstraction: You can interact with an object without
knowing how it's implemented.
● Objects
○ An object bundles together information and related behavior.
○ Each object has its own local state.
○ Several objects may all be instances of a common type.
● Classes
○ A class serves as a template for all of its instances.
○ Each object is an instance of some class.
CS61A Review: Constructors
● Constructors: A special method that creates a new object.
(in other words, a new instance of the class)
○ In Python: __init__
○ In Java: Same name as the class.
● Can take in additional arguments (in the example, m).
● Can be used to initialize instance variables (local state) for the new object.
● In Java: We also have to declare instance variables before using them.
Car.jav car.p
a y
public class Car { class Car:
public String model;
public int gas;
Car.jav car.p
a y
public class Car { class Car:
public void drive() { def drive(self):
if (gas < 5) { if (gas < 5):
System.out.println("Cannot drive!"); print("Cannot drive!")
return; return
}
gas -= 5; gas -= 5
System.out.println(model + " goes print(self.model + " goes vroom!")
vroom!");
}
def gasLeft(self):
public int gasLeft() { return self.gas
return gas;
}
def addGas(self, amount):
public void addGas(int amount) { gas = gas + amount
gas = gas + amount;
}
}
Java Syntax: this Keyword
● The this keyword can be used to access the current object's instance variables
or methods.
● Unlike Python, where self is mandatory, using this is not mandatory (as long
as variable names are unique).
● More details here
Car.jav car.p
a y
public static void main(String[] args)
{
Car c1;
Car c2;
c1 = Car("Honda Civic")
c1 = new Car("Honda Civic"); c2 = Car("Model T")
c2 = new Car("Model T");
}
CS61A Review: Accessing Objects
● Use dot notation to access methods of an object.
Car.jav car.p
a y
public static void main(String[] args)
{ ...
... print(c1.gasLeft()) # 5
46
Time to Go Learn Java Basics!
I am not going to spend time in this class covering for loops, while loops, etc. in
Java!
● You’ve seen this all before in some other language.
If you can, start lab 1 early! Most of it is just downloading and installing software.
Post-Lecture Q&A
If you have questions, come find us outside the lecture hall!
We have to clear out of the lecture hall for the next class.