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());
}
}For now, you have used public and private:
We will take more about these later:
new do?