Modularity: Classes allow us to split the problem to be solved into distinct tasks
You should be able to think about what a class does, not how it does it
Class - A blueprint or template that defines the structure and behavior of objects.
Object/Instance - A concrete realization of a class. If a class is the blueprint, an object is the actual house built from it.
ClassnewThe keyword new does three things:
Class definition is like the plan for a houseObject is like a house built according to the planObjects can be created from a Class definitionAttributes/Properties/Fields - Variables that belong to a class or object, storing data about that object’s state.
Methods are functions defined inside a class that describe behaviors or actions an object (or class if the static keyword is used) can perform.
Types of Methods:
A special method that runs automatically when creating a new object, typically used to initialize attributes.
this – A reference to the current instance of the class, keyword used to differenciate between the instance variable and the parameter name
These are usually public – used to access and modify instance variables
Bundled data and methods together (getters/setters) while hiding internal details and controlling access to them.
private, and those that are intended for general use will have public accessors (getters and/or setters) with validation for settersOperator methods – use public keyword to provide external way to perform an operation – example: add complex numbers, subtract complex numbers
Helper methods – Use private keyword to perform operations that are used internally by other methods. These are intended for internal use within a class to support other methods, typically not meant to be called directly from outside the class.
Helper methods are usually used to break down complex operations into smaller, reusable pieces. Operator methods often call helper methods.
This does not complete the class code, what else do you need to make this class work as intended?
public String getHMS() {
return hours + ":" + minutes + ":" + seconds;
}
private int getHours() {
return seconds/3600;
}
private int getMinutes() {
return seconds/60;
}What instance variables do we need?
What code do you need to write to have this main method work?
public static void main(String[] args) {
MyTime time = new MyTime(3661);
System.out.println(time.getHMS());
}Submit your MyTime.java file to gradescope.
public class MyTime {
public MyTime(int seconds) {
secondsLeft = seconds;
// get full hours
hours = getHours();
// update how many seconds are left
secondsLeft = secondsLeft - (hours * 3600);
// calculate full minutes
minutes = getMinutes();
// update how many seconds are left
secondsLeft = secondsLeft - (minutes * 60);
}
public String getHMS() {
return hours + ":" + minutes + ":" + secondsLeft;
}
private int getHours() {
return secondsLeft/3600;
}
private int getMinutes() {
return secondsLeft/60;
}
private int secondsLeft;
private int hours;
private int minutes;
public static void main(String[] args) {
MyTime time = new MyTime(3661);
System.out.println(time.getHMS());
}
}Static vs. non-static properties
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 your to GregorianDate.java file to gradescope
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
new do?