Unit 5 Writing Objects
student led lesson notes + homework
Classes
- classes are "blueprints" that we use to instantiate objects
- classes are an essential aspect of OOP
Anatomy
- Object
- state, attributes, behavior
- represented by an instance in the program
- Class
- defines an abstract data type
- object references: string variable
- instance variables: attributes, behaviors, data for objects
Functions
- methods: the action or behavior of an object
- constructor: special method for object instantiation, sets the initial values for variables, default constructors have no arguments
- main: tests the class
- accesser: accesses the values of variables, returns a copy of the variable
- mutator: changes values of variables
Writing Methods
- method_definition(Modifiers, Return Type)
- method_signature(Name, Parameters)
- method_body(Code)
- "Object.method()"
public int scoreGuess (String guess)
{
int count = 0;
for (int i = 0; i <= secret.length() - guess.length(); i++)
{
if (secret.substring(i, i + guess.length()).equals (guess))
{
count++;
}
}
return count * guess.length() * guess.length();
}
public void addMembers(String[] names, int gradYear ){ //mutator method - allows modification of grad year but not access to variable
for( String n : names ){
memberList.add(new MemberInfo( n, gradYear, true) );
}
}