Introduction: 2.2.0

  • Java class defines:
    • What a class knows (attributes)
    • What a class can do (behaviors)
  • Class has constructors which initialize attributes in an object

  • new object created with new keyword followed by class name
    • new Class()
    • Creates a new object of the class, calls constructor
      • Constructor has same name as class
        • new World() constructs a new World object
import frc.robot.RobotMap;

// to create a new object and call the constructor:
// ClassName varName = new ClassName(params);

// creating a world object and calling its constructor
World habitat = new World();

// some code I might use in robotics to create a new motor object, 
// with the CAN ID (Controller Area Network ID) of that motor as a parameter
TalonFX intakeMotor = new TalonFX(RobotMap.INTAKE_MOTOR_CAN_ID);

Overloading Constructors: 2.2.1

  • Can be more than one constructor defined in a class
    • Known as overloading the constructor
    • Usually includes a constructor without parameters
      • no argument constructor
      • Sets attributes to their default values
    • Other constructors which take parameters
      • Turtle(habitat)
      • a parameter is a value which is passed into a constructor
        • used in the initialization of the object

The World Class Constructors: 2.2.2

  • no argument constructor creates a window of 640x480
  • World(int width, int height) initializes World with custom width and height
    • new World(300, 400) creates a world of 300x400
  • World coordinates go from the top left corner.
    • x increases to the right
    • y increases to the bottom

The Turtle Class Constructors: 2.2.3

  • Has multiple constructors, all of which require a world
    • Default location for the turtle is the middle of the world
    • Turtle turguggins = new Turtle(world1);
    • Also a constructor which places a turtle at a specific (x,y)
      • Turtle turguggins = new Turtle(50, 100, world1);
  • The order of parameters matter

Object Variables and References: 2.2.4

  • You can declare an object variable and initialize it to null
    • Turtle turguggins = null;
      • Creates variable turguggins
      • null means it doesn’t refer to anything yet
      • can create the object later
        • turguggins = new Turtle(world1);
      • can also just declare and initialize in the same line
        • Turtle turguggins = new Turtle(world1);
  • An object variable holds a reference to an object
    • A reference is a way to find an object in memory
    • Like a tracking number on a package

Video

  • represent null as an X
Dog fido = new Dog();  // Dog fido gets a reference to a new dog

// we make a new variable which can reference a dog
Dog dog = null; // is the same as...
Dog dog1;
World world1 = new World();

// create and init on seperate lines
Turtle turguggins = null;
turguggins = new Turtle(world1);

// create and init on one line
Turtle turguguggins = new Turtle(world1);

Constructor Signatures: 2.2.5

  • when using a library someone else created, you can look up how to use the code using documentation
    • Documentation for turtle class
    • lists the signatures(headers) for the constructors and methods
      • tells you name, and parameter list for constructors and methods
        • parameter list lists the variables and datatypes used in a given method
  • Constructors are overloaded when multiple exist
    • Both constructors must have different signatures
      • not the same number, type, and/or order of parameters
public class Date
{
  /* ATTRIBUTES */
  private int year;
  private int month;
  private int day;

  /* CONSTRUCTORS */  
  /* A constructor to initialize to today's date */
  public Date() {
    // implementation not shown
  }

  /* a constructor to initialize the attributes for a date with given parameters */
  public Date(int year, int month, int day) {
    // implementation not shown
  }
}

Formal and Actual Parameters: 2.2.6

  • Formal parameters are what is declared in the method
    • in Date(year, month, day), year, month, and day are formal parameters
  • Actual parameters are arguments
    • new Date(2021, 11, 1)
  • Date is call by value
    • copies of the parameter values
    • used to initialize the object’s attributes
  • type of values in arguments needs to be the same as the type of the formal parameter
    • Cannot assign an int to be a String value
  • The order of arguments needs to be consistent

Summary: 2.2.8

  • Constructors initialize attributes in newly created objects
    • Have the same name as the class
  • Constructor signature is the constructor name followed by the parameter list
  • overloading is when more than one constructor is present
    • must differ in number, type, or order of parameters
  • new is a keyword to create a new object of a class
    • new ClassName()
  • no argument constructor is a constructor which doesn’t take any passed-in values
  • parameters allow values to be passed in to initialize newly created attributes
  • parameter list is a list of the type and name of variable being passed in the header of a constructor
    • these are formal parameters
  • actual parameters are the values being passed to a constructor
    • formal parameters are being set to the actual parameters
  • formal parameters are the specification of parameters in constructor heading
    • a list of the type and name of each parameter
      • World(int width, int height)
  • call by value is the process by which a value passed to a constructor/method is copied to be used in that constructor/method

AP Practice: 2.2.9

No notes here; just complete the practice.