Unit 5 Writing Classes College Board
writing classes unit 5
- blueprints to instantiate objects
- anatomy of a class: object has state, instances, and are represented in an instance
- methods and insutructors: behaviors that apply to the object
- main method: what the java code runs on
- constructors: special method for objecet instantiation
- default constructors: no arguements
- accessor methods: gives access to the variable value with out to the actual value itself
- Mutator method: classes can change the value of a variable without accessing the variable itself
- writing a method: method defintion, the signature, the body
- void: no reutrn just execute code
- static modifiers: class itslef has to be the one that executes the code
- "this": refers to constructor being called in , references the object
- access modifiers: mainly used for encapsulation and helps to prevent misuse of data public static void main string args
- varaibles:local, lock level, parameter
New Vocab
Writing Classes
- In Java, there are two main access levels that you will see in this course, private and public
- When creating classes, classes should always be declared public since they need to be accessed by other classes
Ex: public class car { blah }
Constructor
- You use constructors to create new objects using a class
- Constructors are used to set the initial state of an object, which should include initial values for all instance variables
- The no-argument constructor executes anytime an object is created and sets any instant variable to their default value
- The reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Ex: private int height;
Accessor Methods
- An accessor method is a method that is used to return the value of an instance (or static) variable to the user
- When naming your method you should always use this format: get[Variable Name] for the name- Getter method Ex:
private int height = 3;
public int getHeight() { return height; }
Mutator Methods
- A mutator method is a method that is used to update the value of an instance (or static) variable
- Mutator methods set a new value. As such, they are often referred to as setter methods
- When creating mutator methods you should always use: set[Variable Name] Ex: private int width = 10;
public void setWidth(int newWidth) { width = newWidth; }
Static Methods/Class Methods
- A class method (or static method) is a method of a class that is common to all instances of a class and is not called on any specific object instance
- Classes can have both instance variables and static variables, but static methods can only update static variables Ex: private static int getTotalBirds() { return totalBirds; }
Static Variables
- Because static methods and variables are available across an entire class, they don’t need to be called on a particular instance of the class. Ex: Skater.getTotalSkaters(); //This will call the getTotalSkaters() method on the Skater class without requiring an instance of the class to be created.
This
- "this": refers to constructor being called in , references the object
Access modifiers
- mainly used for encapsulation and helps to prevent misuse of data public static void main string args
- public, protected, default, private Ex: private static void protected void
int width = 10;
public int getWidth()
{
return width;
}
int height = 3;
public void setHeight(int newHeight)
{
height = newHeight;
System.out.println(height);
}
class SuperHeroTester
{
public static void main(String[] args)
{
Power speed = new Power("Speed", 10);
System.out.println("Creating the Flash!");
SuperHero theFlash = new SuperHero("The Flash", speed);
System.out.println("Number of SuperHeroes Created:");
System.out.println(SuperHero.getNumSuperHeroes());
System.out.println("Creating Shazam!");
SuperHero shazam = new SuperHero("Shazam", speed);
System.out.println("Number of SuperHeroes Created:");
System.out.println(SuperHero.numSuperHeroes);
}
}
public int scoreGuess (String guess)
{
int count = 0
for (int = 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 )
{
MemberInfo newMember = new MemberInfo (n, gradYear, true);
memberList.add(newMember)
}
}