Writing Classes Student Lesson
Classes
Classes
- blueprints for instantiating objects
- model real world objects in code
Anatomy of a Class
- Object
- instance of a class
- Class
- defines an abstract type
- Methods
- behaviors you can get the object to do
- Constructors
- creates the object
- special method for object instantiation
- default constructor has no arguments
- Main method
- test the class
Accessor Methods
- Get of methods or getters
- allows you to get values of variables
- return a copy
- Non-void Methods
- value of specific data type returned
- no parameteres
Mutator Methods
- Set of methods or setters
- allows you to change the value of the variable
- Void Methods
- no value returned
- will take parameters
Writing Classses
- Method defintion
- Method Signature
- Method body
Key Words
- /Static Modifiers
- used after access modifiers
- denotes as belonging to a class
- /This
- refers to the constructor that is being called
- Access Modifiers
- Restricts scope of classes, variables and functions
Scope and Access
- /Scope
- Where a variable can be accessed/used
- Class level: Instance Var
- Method level: Local, parameter var
- Block level: If else/ Loops
2021 1a and 3a
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 ){
for( String n : names ){
memberList.add(new MemberInfo( n, gradYear, true) );
}
}