knowt logo

AP CSA Unit 2

  • Class is a programmer-defined blueprint from which objects are created

  • An object is an instance of a class

  • Instance variable — variable defined in a class that represents an attribute of an object

    • Declaring an Instance Variable

    • private int xLocation;

      • Private — can’t be accessed outside of the class

      • Int — data type

      • xLocation — name for the instance variable

  • When an object is instantiated, it gets its own copy of the instance variable

  • Inheritance — when a subclass inherits the attributes and behaviors of a superclass

  • Access modifier — set the visibility of classes, variables, constructors, and methods

    • public — visible to all classes in a program

    • private — visible to only inside the class

  • Encapsulation instance variables of a class are hidden from other classes and can be accessed only through the methods of the classes

  • Inheritance helps in making our code reusable and keeping it DRY

    • DRY — “Don’t repeat yourself” —> reduce repetition

    • Refactor code — improve the readability, reusability, or structure of a program without altering its functionality

  • No-argument constructor — constructor w/ no parameters

  • Constructor is a block of code that has the same name as the class and tells the computer how to create a new object

  • How to write a constructor:

    • public name(){

      • Constructor signature

      • Inside the curly braces we write the body to the constructor by assigning values to the instance variables

      • No-argument constructor often assigns default values to the instance variables

        • A default value is a predefined value that is used by a program when the user does not provide a value (based on the data type, Java will automatically provide one)

  • When a constructor is called, it sets the state of the object by assigning values to the instance variables

    • State — attributes of an object that are represented by its instance variables

  • Parameterized Constructor — has specific # of arguments to be passed to assign values to an object’s instance variables

    • public Painter(int x, int y, String dir, int paint)

  • Overloading — defining 2 or more constructors w/ the same name but w/ different signatures

    overloadingformal parameter

local variablesactual parameter

Formal variables are local variables. Local variables are used first. The instant variables are then assigned the default values based on their data types

  • Scope — where a variable can be used

  • We can use the this keyword to refer to the instance variables instead of the local variables (refer to current object)

  • Subclasses inherit the attributes & methods of the superclass but they don’t inherit its constructors!

  • Subclasses inherit the private instance variables of a superclass, but they cannot access them because they are private

  • Super — refers to the superclass

    • Used to call superclass methods & to access the superclass constructor

  • If a subclass doesn’t include super as the first line in its constructor then Java will automatically add a super() call w/o parameters as the first line in a constructor

  • int score = 125;

    • Declaring the variable: giving a name & data type to a variable

    • = Assignment operator —> initialize or change the value assigned to a variable

    • 125 —> starting value —> initializing

      • Literal —> source code representation of a value, such as a # or text

  • Primitive type: double, int, boolean

  • Reference type: data type that contains a pointer to the memory location of an object

  • At the top of your program, if you want user input, you put : import java.util.Scanner;

  • Accessor method — gives the value that is currently assigned to an instance variable

  • How to write an Accessor method:

    • public int getX(){

    • return xLocation;

    • }

      • Public so it can be accessed from outside the class

      • Return type is the same as the data type of the instance variable

      • getX() —> name of the method is typically get followed by the name of the instance variable

      • Return keyword exits the method and provides the value to where the method is called

      • Then we specify the instance variable we want to return

  • Expression — combination of data & operators that evaluates to a single value

  • Operand — data that is operated on

  • Operators can be used to create compound expressions, or a combination of expressions

  • Dividing two int values will result in an int value

    • The decimal portion is truncated, or cut off

  • Dividing an int by 0 will result in an Arithmetic Exception

  • If you try to divide a double by 0, Java assigns the result to infinity

  • Compound assignment operator: shortcut syntax to perform an operation on both operands & assign the result to the variable on the left

  • Java evaluates expressions according to its order of operations

Scanner objects, user input ^^^

  • Don’t forget to input.close();

  • Mutator method: method that changes the value of an instance variable

  • Public so it can be accessed from outside of the class

  • The return type is void since it doesn’t return a value

  • The name of the method is typically set followed by the name of the instance variable

  • Specify a parameter that matches the type of the instance variable

  • Then we assign the value passed to the parameter to the instance variable

  • Calling a Mutator Method:

    • lisa.setAge(17);

    • When we call the method, we pass the new value to assign to the age instance variable

  • Using relational operators, we can write a Boolean expression, which is a logical statement that gives either a true or false

  • public String toString() — called whenever an object is printed

  • The toString() method in the Object class returns a String that consists of the name of the class, the @ symbol, and the hash code of the object

  • The toString() method is automatically called when an object reference is passed to a print statement or when an object reference is concatenated with a String

  • To override is to define a method in a subclass with the same method signature as a method inherited from a superclass

  • In the body of the toString() method, we return a String that contains information about the object

  • We can use the super keyword to call the superclass version of a method in a subclass

ZH

AP CSA Unit 2

  • Class is a programmer-defined blueprint from which objects are created

  • An object is an instance of a class

  • Instance variable — variable defined in a class that represents an attribute of an object

    • Declaring an Instance Variable

    • private int xLocation;

      • Private — can’t be accessed outside of the class

      • Int — data type

      • xLocation — name for the instance variable

  • When an object is instantiated, it gets its own copy of the instance variable

  • Inheritance — when a subclass inherits the attributes and behaviors of a superclass

  • Access modifier — set the visibility of classes, variables, constructors, and methods

    • public — visible to all classes in a program

    • private — visible to only inside the class

  • Encapsulation instance variables of a class are hidden from other classes and can be accessed only through the methods of the classes

  • Inheritance helps in making our code reusable and keeping it DRY

    • DRY — “Don’t repeat yourself” —> reduce repetition

    • Refactor code — improve the readability, reusability, or structure of a program without altering its functionality

  • No-argument constructor — constructor w/ no parameters

  • Constructor is a block of code that has the same name as the class and tells the computer how to create a new object

  • How to write a constructor:

    • public name(){

      • Constructor signature

      • Inside the curly braces we write the body to the constructor by assigning values to the instance variables

      • No-argument constructor often assigns default values to the instance variables

        • A default value is a predefined value that is used by a program when the user does not provide a value (based on the data type, Java will automatically provide one)

  • When a constructor is called, it sets the state of the object by assigning values to the instance variables

    • State — attributes of an object that are represented by its instance variables

  • Parameterized Constructor — has specific # of arguments to be passed to assign values to an object’s instance variables

    • public Painter(int x, int y, String dir, int paint)

  • Overloading — defining 2 or more constructors w/ the same name but w/ different signatures

    overloadingformal parameter

local variablesactual parameter

Formal variables are local variables. Local variables are used first. The instant variables are then assigned the default values based on their data types

  • Scope — where a variable can be used

  • We can use the this keyword to refer to the instance variables instead of the local variables (refer to current object)

  • Subclasses inherit the attributes & methods of the superclass but they don’t inherit its constructors!

  • Subclasses inherit the private instance variables of a superclass, but they cannot access them because they are private

  • Super — refers to the superclass

    • Used to call superclass methods & to access the superclass constructor

  • If a subclass doesn’t include super as the first line in its constructor then Java will automatically add a super() call w/o parameters as the first line in a constructor

  • int score = 125;

    • Declaring the variable: giving a name & data type to a variable

    • = Assignment operator —> initialize or change the value assigned to a variable

    • 125 —> starting value —> initializing

      • Literal —> source code representation of a value, such as a # or text

  • Primitive type: double, int, boolean

  • Reference type: data type that contains a pointer to the memory location of an object

  • At the top of your program, if you want user input, you put : import java.util.Scanner;

  • Accessor method — gives the value that is currently assigned to an instance variable

  • How to write an Accessor method:

    • public int getX(){

    • return xLocation;

    • }

      • Public so it can be accessed from outside the class

      • Return type is the same as the data type of the instance variable

      • getX() —> name of the method is typically get followed by the name of the instance variable

      • Return keyword exits the method and provides the value to where the method is called

      • Then we specify the instance variable we want to return

  • Expression — combination of data & operators that evaluates to a single value

  • Operand — data that is operated on

  • Operators can be used to create compound expressions, or a combination of expressions

  • Dividing two int values will result in an int value

    • The decimal portion is truncated, or cut off

  • Dividing an int by 0 will result in an Arithmetic Exception

  • If you try to divide a double by 0, Java assigns the result to infinity

  • Compound assignment operator: shortcut syntax to perform an operation on both operands & assign the result to the variable on the left

  • Java evaluates expressions according to its order of operations

Scanner objects, user input ^^^

  • Don’t forget to input.close();

  • Mutator method: method that changes the value of an instance variable

  • Public so it can be accessed from outside of the class

  • The return type is void since it doesn’t return a value

  • The name of the method is typically set followed by the name of the instance variable

  • Specify a parameter that matches the type of the instance variable

  • Then we assign the value passed to the parameter to the instance variable

  • Calling a Mutator Method:

    • lisa.setAge(17);

    • When we call the method, we pass the new value to assign to the age instance variable

  • Using relational operators, we can write a Boolean expression, which is a logical statement that gives either a true or false

  • public String toString() — called whenever an object is printed

  • The toString() method in the Object class returns a String that consists of the name of the class, the @ symbol, and the hash code of the object

  • The toString() method is automatically called when an object reference is passed to a print statement or when an object reference is concatenated with a String

  • To override is to define a method in a subclass with the same method signature as a method inherited from a superclass

  • In the body of the toString() method, we return a String that contains information about the object

  • We can use the super keyword to call the superclass version of a method in a subclass

robot