K-12 CS Teacher Professional Development Workshop
Reading
Creating a class is simple, but understanding how to think in terms of classes when problem solving is the biggest challenge. The best thing to do is understand how classes work, understand what maximizes efficiency in one's code, and what objects can do in Java. Here are the three main types of methods in a non-main class...
Constructors
Accessor/Mutator (Getter/Setter) Methods
The toString() Method
Every method is constructed with almost an identical header. First, the visibility is specified (public/private). Second, the return type is specified (void (cannot return a type)/String/int/double/etc.). Third, the method's name is established. There are exceptions to this format, but most methods are created like this.
Constructors
These methods are what define your object when it is created. Unlike the other methods, they must be named after the class and they cannot have any return type specified (not even void!). The function of the constructor is to take in information in their parameters and use this information to define the object that is represented by the class. For example, there may be a class called meat which makes meat objects. Each meat object may have attributes which define the type of meat and the cook time. This is what a constructor would look like for it...
public class Meat {
//private variables are made to hold the information for each object.
private String mType;
private int mTime;
public Meat(String type, int cookTime) {
/*The private variables in this class will be set to whatever the object's parameters are declared to be * whenever it is made.
*/
mType = type;
mTime = cookTime;
}
}
In the actual program, a Meat object would be made like this...
public Meat steakOne = new Meat("steak", 5);
When the program runs, a new Meat object will be made. The information given in the parameters will pass through the constructor and this specific meat called steakOne will have its mType be "steak" and mTime be 5.
Accessor/Mutator Methods
It is always important to keep data safe in Java. There is an idea in Java called encapsulation which keeps variables private while still being mutable through public methods. This is to keep data safe from being freely changed at will. Accessor methods look like this...
public String getMType() {
return mType;
}
...while mutator methods look like this...
public void setMType(string newType) {
mType = newType;
}
These two methods are used to access and change your variable indirectly.
The toString() Method
The toString() method is one that is inherent to all classes in Java already, but it is a useful method to override in console programming. It is normally used as a method which contains all the information about an object, and because toString() is an inherent method for all classes, trying to print any object will automatically print the toString(). Here is what writing a toString() method should look like...
public String toString() {
return "Meat Type: " + mType + "\nCooking Time: " + mTime;
}