---
marp: false
theme: uncover
title: 29 September
---

# <!-- fit --> CS 199 EMP

## <!-- fit --> Even More Practice

---

# 2 meetings every week Tuesday and Thursday

Each day has two timeslots for 

* Eastern timezones and American early risers

* Western timezones and American afternoons

* Make sure to keep up with the calendar for links and timings

---

# Format

* 3 practice problems every session

* focused on practical understanding of concepts covered recently in class

* slides and material available after class

--- 

# How to follow along

* Small breakout rooms of working together 

* Work for 10-15 minutes together, feel free to use paper, whiteboards, online share tools.

* Use the `jeed` playground on the 125 homepage for the interactive running

---

# 1) Getters and Setters

* In java all the instance variables are `private`

* To read them we use getters, for `String name` you might have a function called `String getName()` 

* To write to them we use setters, for `String name` you might have a function called `void setName(String setName)`

---

# 1) Getters and Setters - Example

```java
public class Country {
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String setName) {
        name = setName;
    }
}
```

---

# 1) Getters and Setters - Practice

* Write a class `Pet` that has one field `int age`

* Create `getAge()` and `setAge(int)`

---

# 1) Getters and Setters - Starter code (10 minutes)

```java
public class Pet {
  // implement
}
```

---

# 2) Constructor Overloading

* You can overload many constructors much like functions.

* This is to deal with a variety of inputs.

* Each constructor has different parms but the same name as the class.

* You can use the `this` keyword to call other constructors.

---

# 2) Constructor Overloading - Example

```java
public class Country {
    private String name;
    
    public Country() {
        name = "India"; // default
    }

    public Country(String setName) {
        name = setName;
    }
}
```

---

# 2) Constructor Overloading Practice - Practice

* This time Pet should have two fields, `String name` and `int age` 

* One constructor should take a `String` for name and the default age should be set at `6`

* The other should take both a `String` for name and an `int` for age.

---

# 2) Constructor Overloading Starter Code (10 minutes)

```java
public class Pet {
  // implement 
  // don't worry about getters and setters
}
```

---

# 3) OOP Functionality

* Classes _encapsulate_ state and behavious

* For behavious we can make instance methods inside a class which use the instance variables.

* These can't have the keyword `static` (more next time)

--- 

# 3) OOP Functionality - Example

```java
public class Circle {
    private int radius;
    [...]
    public int circumference() {
        return 2 * Math.PI * radius;
    }
}
```

---

# 3) OOP Functionality - Practice

* Model a class `Student` with two midterms and one final score as `double`

* Create a function `grade` which takes no parameters and returns a double. $grade = 0.2 midterm1 + 0.3 midterm 2 + 0.5 final.

---

# 3) OOP Functionality - Starter code (15 minutes)

```java
class Student {
    // define variables

    double grade() {
        return 0.0; // todo
    }
}
```

---

# <!-- fit --> Solutions

Spoiler alert :P

---

# 1) Getters and Setters - Starter code (10 minutes)

```java
public class Pet {
  private int age = 0;
  public int getAge() { 
    return age; 
  }
  public void setAge(int setAge) { 
    age = setAge; 
  }
}
```

---

# 2) Constructor Overloading

```java
public class Pet {
  private String name;
  private int age;
  public Pet(String setName) {
      name = setName;
      age = 6;
  }

  public Pet(String setName, int setAge) {
      name = setName;
      age = setAge;
  }
}
```
---

# 3) OOP Functionality

```java
class Student {
    private double midterm1;
    private double midterm2;
    private double finalScore;

    double grade() {
        return 0.2*midterm1 + 0.3*midterm2 + 0.5 * finalScore; // todo
    }
}
```
---
