Anatomy of a Class

  • Object

    • State, attributes, behavior
    • Instance of a class
    • Represented by an instance in the program
  • Class

    • Defines an abstract data type
    • Object references
      • String variables
    • Instance Variables
      • Attributes, behaviors
      • Data for objects

Methods and Constructors

  • Methods
    • behaviors or methods that apply to the objects
  • Constructors
    • creates the object

Main Method

  • Main Method
    • Tests the class
    • Instance variables

Constructors

  • Special method for object instantiation
    • Sets initial values for variables
    • Can run methods
  • Default Constructor
    • No constructor defined
    • No arguments
  • Can have multiple constructors per class

Documentation with Comments

Commenting

  • Comment with // for a single line
  • / / for multiple line
  • /* / for documentation comment
  • Help with explaining what code is about
  • Preconditions
    • What must be true before code is implemented
  • Postconditions
    • Should be true after method is run
    • Describe the output if the method

Accessor and Mutator Methods

Accessor Methods

  • Get of methods or getters
    • Allows you to ‘get’ values of variables
    • Returns a copy of variable
  • Non-Void Methods
    • Return a value of specified data type
    • No parameters

Mutator Methods

  • Set of methods or setters
    • Allows you to change the values of instance variables
  • Void methods
    • Will not return a value
    • Will take parameters for instance variables

Writing Methods

Writing Methods

  • 3 Parts:
    • Method Definition (Modifiers, Return Type)
    • Method Signature (Name, Parameters)
    • Method Body (Code)
  • camelCase
  • Object.method()

Method Return Types

  • String
    • Returns a string
  • Int
    • Returns an integer
  • Bool
    • Returns a Boolean
  • Any Object Type
    • Returns specified Object
  • Void
    • Method have any return values

Keywords in Java

Static Modifier

  • Key word usually used after access modifiers
  • Denotes as belonging to a class
  • Objects cannot reference/use static variables & methods
  • Universally shared variables and methods among objects

This

  • Refers to the constructor that it is being called in
  • Eliminates confusion between attributes and parameters

Access Modifiers

  • Restricts scope of classes, variables, and functions
  • Mainly used for encapsulation
  • Prevents misuse of data & methods

Scope and Access

Scope

  • Where a variable can be accessed or used
    • Declaration of variable
  • 3 Levels:
    • Class Level: Instance var
    • Method Level: Local, Parameter var
    • Block Level: Loops, If/Else

Homework: 2021 FRQs Part 1A and 3A

2021 FRQ 1A

Write the WordMatch method scoreGuess. To determine the score to be returned, scoreGuess finds the number of times that guess occurs as a substring of secret and then multiplies that number by the square of the length of guess. Occurrences of guess may overlap within secret. Assume that the length of guess is less than or equal to the length of secret and that guess is not an empty string.

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();
}

2021 FRQ 3A

A high school club maintains information about its members in a MemberInfo object. A MemberInfo object stores a club member’s name, year of graduation, and whether or not the club member is in good standing. A member who is in good standing has fulfilled all the responsibilities of club membership.

public void addMembers(String[] names, int gradYear)
{
    for( String n : names )
    {
        MemberInfo newMember = new MemberInfo (n, gradYear, true);
        memberList.add(newMember)
    }   
}