1 of 20

Introduction to Java

Discussion 02

CS 61B Spring 2023

2 of 20

Announcements

  • Welcome to CS 61B!
  • Please read our Ed guidelines before you post to make sure everything follows the rules
  • Lab 1 - due this Tuesday 1/24
  • Lab 2 - due this Friday 1/27
  • Project 0 - due this Friday 1/27
  • HW0B - due this Monday 1/23
  • HW1 - due next Monday 1/30

CS 61B Spring 2023

3 of 20

Content Review

CS 61B Spring 2023

4 of 20

Quick Java Basics

public class Hello {

public static void main(String[] args) {

System.out.println(“Hello world!”);

}

}

  • In Java, pretty much everything is defined in a class
  • Type declarations: Java is statically typed, so we have to tell the computer what type of value every variable holds and what every function returns (ie. int, void)
  • Don’t forget the brackets and semicolons!

CS 61B Spring 2023

5 of 20

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

6 of 20

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

7 of 20

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

8 of 20

Data Structures Review

List

ex. LinkedList, ArrayList

Map

ex. HashMap, TreeMap

Set

ex. HashSet, TreeSet

Array

  • Ordered collection
  • Allows duplicates
  • Zero-indexed
  • Associates a key with a value
  • No duplicate keys
  • Unordered collection
  • No duplicates
  • Fixed-length (no appends or deletions)
  • Zero-indexed

ex. String[], int[]

CS 61B Spring 2023

9 of 20

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

10 of 20

Worksheet

CS 61B Spring 2023

11 of 20

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:

  1. a capacity for the maximum number of students
  2. a collection of ids of students who have signed up to take the course
  3. the semester (ie. ”Spring 2023”).

Hint: students sign up for the course in an ordered fashion!

CS 61B Spring 2023

12 of 20

1A Welcome to CS 61B

public class CS61B {

// space for variables

public CS61B(int capacity, List<Integer> ids, String semester) {

}

}

CS 61B Spring 2023

13 of 20

1B Welcome to CS 61B

public class CS61B {

// variables here

// constructor from part a

...

}

Define the following variables within the class:

  1. university: the name of the university, which should be ”UC Berkeley” for all semesters of CS61B
  2. semester: the semester that the course is being taught
  3. students: all the CS61BStudents in this semester’s CS61B. Remember that the course has a fixed capacity!
  4. idToStudent: a way for us to quickly look up a CS61BStudent by their student ID

CS 61B Spring 2023

14 of 20

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

15 of 20

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

16 of 20

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

17 of 20

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:

  1. makeAllStudentsWatchLecture: makes every CS61BStudent in this semester of the course watch lecture
  2. updateGrade: takes in a student ID sid and point value points. Adds points to the grade of the CS61BStudent corresponding to sid and returns their new grade.
  3. changeUniversity: takes in a new university name newUniversity. Changes the university for all semesters of CS61B to newUniversity

CS 61B Spring 2023

18 of 20

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

19 of 20

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

20 of 20

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