Static vs. non-static properties
Not a good way to do this:
First.java
UseFirst.java
Add static after public for one of the instance variable (to make it a class variable). What is the side effect?
First.java
UseFirst.java
We write these:
getSomething( )
setSomething(<type> newVal)
Same name as the class, no return type
Public vs. Private
Public vs. Private
Note – the rules are enforced at a FILE level
Declare:
We overwrite these:
toString()
equals(Object other)
Imagine you wanted to make a java class representing a date in the Gregorian calendar system (The current most common calendar)
As a designer you have to live in two worlds:
It’s nice when they align, but this isn’t always true
Keeping these straight: access modifiers
Write a GregorianDate class with the following public methods:
getYear()getMonth()getDay().toString() should return the following format April 13, 1981Submit to gradescope: https://www.gradescope.com/courses/1074753/assignments/7004597
public class GregorianDate {
private int year;
private int month;
private int day;
private static final String[] MONTHS = new String[]{"January", "February",
"March", "April", "May",
"June", "July", "August",
"September", "October", "November",
"December"};
public GregorianDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
private String getMonthName() {
return MONTHS[month-1];
}
public String toString() {
return getMonthName() + " " + day + ", " + year;
}
}An object should hide its internal details