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
3 practice problems every session
focused on practical understanding of concepts covered recently in class
slides and material available after class
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
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)
public class Country {
private String name;
public String getName() {
return name;
}
public void setName(String setName) {
name = setName;
}
}
Write a class Pet
that has one field int age
Create getAge()
and setAge(int)
public class Pet {
// implement
}
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.
public class Country {
private String name;
public Country() {
name = "India"; // default
}
public Country(String setName) {
name = setName;
}
}
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.
public class Pet {
// implement
// don't worry about getters and setters
}
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)
public class Circle {
private int radius;
[...]
public int circumference() {
return 2 * Math.PI * radius;
}
}
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.
class Student {
// define variables
double grade() {
return 0.0; // todo
}
}
Spoiler alert :P
public class Pet {
private int age = 0;
public int getAge() {
return age;
}
public void setAge(int setAge) {
age = setAge;
}
}
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;
}
}
class Student {
private double midterm1;
private double midterm2;
private double finalScore;
double grade() {
return 0.2*midterm1 + 0.3*midterm2 + 0.5 * finalScore; // todo
}
}