- Classes and Objects
- Blueprint / cookie cutter
- product / cookie
- Instance vs class (a.k.a. “static”)
- Instance variables are the properties that make one object distinct from another.
- A date object, for example, may have month, day, and year as instance variables, as that is what we use to distinguish two dates.
- Class (aka “static”) variables are shared among all objects in a class.
- Common for constants (e.g., # of days in each month)
- Can also be mutable
- Sometimes classes need to keep track of the number of instances. That can be done with a class variable.
- Suppose I write a date as “10/15/93”. What year is that?
- Suppose I write a date as “10/15/22”. What year is that?
- Suppose I write a date as “10/15/32”. What year is that?
- Suppose I write a date as “10/15/42”. What year is that?
- There has to be some rule for when you switch back to the 1900s. That cutoff could be a static variable: Changing it changes the rule for all dates.
- Instance methods are methods that rely on instance variables. For example if I say
dayOfWeek()
, you can only provide an answer with respect to a specific object (i.e., a specific date). The question simply doesn’t make sense otherwise. - Class methods don’t rely on any instance data. For example, you can answer
isLeapYear(1954)
without any additional data.- Think of class methods as independent methods that are simply packaged up inside a given class for convenience / organization. Technically, they could be placed anywhere (although that would make the code hard to follow.)
- Some software engineers prefer class methods over instance methods. Why?
- They are easier to test: Why? No side effects.
- Why is the
main
method static? Because when Java first begins, there are no objects. Thus, no objects on which to invoke an instance method.
- Instance variables are the properties that make one object distinct from another.
- Method Overloading
- A class can have several methods with the same name, as long as the parameter list is different. This is called Overloading.
- Think of a class as a tool to help other programmers get the job done.
- Overloading is a convenience to the programmer who uses that “tool.” In most cases, the overloading isn’t necessary — just convenient.
- Look at the Java String API https://https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- There are four different versions of
indexOf
. indexOf(int ch)
is not necessary. It just makes it so you can saystr.indexOf('x')
rather thanstr.indexOf('x', 0)
. Notice the convenience factor.- What’s the shortest way to write
indexOf(char)
if you have already completedindexOf(char, int)
? - Make sure you do this when completing the project.
- What does DRY stand for? Don’t repeat yourself.
- Be sure to look for opportunities to apply DRY in 163. It’s part of the grading rubric.
- What’s the shortest way to write
indexOf(String str)
does the same thing but matches strings instead of characters. We could have given it a different name; but, since it does the same thing, it’s easier to remember just one name.- Of the four
indexOf
methods, which would you implement first? Why? How could you write the other three in terms of the first?
- There are four different versions of
- The
SimpleDate
class has two different constructorsSimpleDate(int, int, int)
andSimpleDate(String)
. You will implement both.- In the case of this constructor, there isn’t a good way to write one in terms of the other. But, you can factor common code out into a private helper method.
Switch to testing.
Array tricks
- Discuss how to test
ordinalDate
(i.e., which test cases and why)?- Have students implement
- After 5 or minutes ask to see some different approaches.
- Show
daysInMonth
static final array. Ask for ideas of how this could make the source code more concise. - What else could be put in an array to make it even better?