Introduction to Java
Discussion 02
CS 61B Spring 2023
Announcements
CS 61B Spring 2023
Content Review
CS 61B Spring 2023
Quick Java Basics
public class Hello {
public static void main(String[] args) {
System.out.println(“Hello world!”);
}
}
CS 61B Spring 2023
Structure of a Class
public class CS61BStudent { // Class Declaration
public int idNumber; // Instance Variables
public int grade;
public static String instructor = “Hug”; // Class (Static) Variables
public CS61BStudent (int id) { // Constructor
this.idNumber = id; // this refers to the instance of the CS61BStudent we are in
this.grade = 100;
}
public void watchLecture() { // Instance Method
...
}
public static String getInstructor() { // Class (Static) Method
...
}
}
CS 61B Spring 2023
Instantiating Classes
public class CS61BLauncher {
public static void main(String[] args) {
CS61BStudent studentOne; // Declare a new variable of class CS61BStudent
studentOne = new CS61BStudent(32259); // Instantiate and assign to our new instance
CS61BStudent studentTwo = new CS61BStudent(19234); // Both at once
studentOne.watchLecture(); // Instance methods are called on instance
CS61BStudent.getInstructor(); // Static methods can be called on the class OR the
instance
CS61BStudent.watchLecture(); // Fails. Which student is watching lecture?
studentOne.getInstructor(); // Works, though is seen as bad practice.
}
}
CS 61B Spring 2023
Overview: Static vs. Instance
Static variables and functions belong to the whole class.
Example: Every 61B Student shares the same instructor, and if the instructor were to change it would change for everyone.
Instance variables and functions belong to each individual instance.
Example: Each 61B Student has their own ID number, and changing a student’s ID number doesn’t change anything for any other student.
Check for understanding: can you reference instance variables in static methods? Can you reference static variables in instance methods?
*Don’t worry if you don’t fully understand the difference right now! We’ll talk more about this in future discussions
CS 61B Spring 2023
Data Structures Review
List
ex. LinkedList, ArrayList
Map
ex. HashMap, TreeMap
Set
ex. HashSet, TreeSet
Array
ex. String[], int[]
CS 61B Spring 2023
A Few Data Structures APIs
List
LinkedList a = new LinkedList<Cat>();
// out of bounds error! The list is empty
Cat z = a.get(0);
// add to list
a.add(someCat);
// get 0th element of the list
Cat x = a.get(0);
Map
HashMap<String, Integer> b = new HashMap<>();
// add a key-value pair to the map
b.put(“hi”, 3);
// get the value for key “hi”
Integer y = b.get(“hi”);
Set
HashSet<Dog> c = new HashSet<Dog>();
// add to set
c.add(aDog);
// check if set contains aDog
boolean hasADog = c.contains(aDog);
String[] d = new String[50];
// k is null
String k = d[2];
// put a String into the array
d[32] = “hello”;
// only 50 positions in the array! Out of bounds error
String s = d[50];
Array
* note all the ways we can use <> when we initialize List/Map/Set
CS 61B Spring 2023
Worksheet
CS 61B Spring 2023
1A Welcome to CS 61B
public class CS61B {
// space for variables
// constructor here
}
Each CS61B instance represents one semester of the course.
Define a skeleton for the constructor that takes in:
Hint: students sign up for the course in an ordered fashion!
CS 61B Spring 2023
1A Welcome to CS 61B
public class CS61B {
// space for variables
public CS61B(int capacity, List<Integer> ids, String semester) {
}
}
CS 61B Spring 2023
1B Welcome to CS 61B
public class CS61B {
// variables here
// constructor from part a
...
}
Define the following variables within the class:
CS 61B Spring 2023
1B Welcome to CS 61B
public class CS61B {
public static String university = “UC Berkeley”;
public String semester;
public CS61BStudent[] students;
public Map<Integer, CS61BStudent> idToStudent = new HashMap<>();
// constructor from part a
...
}
Notice that we can’t initialize semester or students yet: we don’t know what the semester or capacity of the class are!
CS 61B Spring 2023
1C Welcome to CS 61B
public class CS61B {
public static String university = “UC Berkeley”;
public String semester;
public CS61BStudent[] students;
public Map<Integer, CS61BStudent> idToStudent = new HashMap<>();
public CS61B(int capacity, List<Integer> ids, String semester) {
}
}
In the constructor, populate students and idToStudent with the first capacity students whose ID numbers are in ids and initialize the semester instance variable.
You’ll also want to instantiate and assign any other instance variables that you haven’t already!
Hint: We have both a constructor variable and instance variable named semester. How can we distinguish them?
CS 61B Spring 2023
1C Welcome to CS 61B
public class CS61B {
public static String university = “UC Berkeley”;
public String semester;
public CS61BStudent[] students;
public Map<Integer, CS61BStudent> idToStudent = new HashMap<>();
public CS61B(int capacity, List<Integer> ids, String semester) {
students = new CS61BStudent[capacity];
for (int i = 0; i < capacity; i += 1) {
Integer newStudentId = ids.get(i);
CS61BStudent newStudent = new CS61BStudent(newStudentId);
students[i] = newStudent;
idToStudent.put(newStudentId, newStudent);
}
this.semester = semester;
}
}
CS 61B Spring 2023
1D Welcome to CS 61B
public class CS61B {
public static String university = “UC Berkeley”;
public String semester;
public CS61BStudent[] students;
public Map<Integer, CS61BStudent> idToStudent = new HashMap<>();
// constructor
...
// methods here
}
Add the following methods to the class:
CS 61B Spring 2023
1D Welcome to CS 61B
public class CS61B {
// variables and constructor
...
public void makeAllStudentsWatchLecture() {
for (CS61BStudent student : students) {
student.watchLecture();
}
}
public int updateGrade(Integer sid, int points) {
CS61BStudent student = idToStudent.get(sid);
student.grade += points;
return student.grade;
}
public static void changeUniversity(String newUniversity) {
university = newUniversity;
}
}
}
CS 61B Spring 2023
1E Welcome to CS 61B (Extra)
idToStudent associates a student ID with a CS61BStudent. CS61BStudent also has other variables like grade and instructor. Would it make sense for us to also have mappings from these attributes to a CS61BStudent?
CS 61B Spring 2023
1E Welcome to CS 61B (Extra)
idToStudent associates a student ID with a CS61BStudent. CS61BStudent also has other variables like grade and instructor. Would it make sense for us to also have mappings from these attributes to a CS61BStudent?
Not really: A map requires a unique key to look up some value, but grade and instructor aren’t good examples of unique keys (ie. what if two students both had a grade of 90? Which CS61BStudent should 90 map to? Likewise, every CS61BStudent shares the same instructor because it is a static variable, so the mapping isn’t clear).
CS 61B Spring 2023