knowt ap exam guide logo

AP COMP SCI

Lesson 1.3 Expressions & Assignment Statements

Creating a “int“ Variable

int num = 7;
  • Is stored in 4 bytes

  • “int“ creates an integer variable

  • “num” is the placement for the name of the variable.

    • EX: numPets, inStorage

  • “7“ is the value of the variable

  • “=” is an assignment operator

    • Assignment operators take whats on the right-hand side and puts that value in the variable.

Creating a “double” Variable

double costBurger = 3.95;
  • Is stored in 8 bytes

Creating a “String” Variable

String student = "Vicky Viking";

Creating a “char” Variable

char mcChoice = 'a';

Only need 2 parts to create a variable

  • By creating a variable, the space is allocated in the stack even if the value is not yet defined.

(+) Operator does 2 things

  • 3 + 2 adds to 5

  • String + number → acts like a concatenation operator

Arithmetic Operators

  • (adding)

  • (subtract)

  • / (divide)

  • * (multiple)

  • % (modulus)

int num = 5 + 7;
//num = 12
int num = 7 - 5;
// num = 2
int num = 10 / 5;
// num = 2
int num = 2 * 5;
//num = 10
int num = 15 % 6;
// num = 3

Lesson 1.4 Compound Assignment Operators

  • += (Plus Equals)

    int num = 5;
    num += 10;
    //num is now 15
  • -= (Minus Equals)

    int num = 5;
    num -= 3;
    //num is now 2
  • *= (Multiply Equals)

    int num = 5;
    num *= 2;
    //num is now 10
  • /= (Slash Equals)

    int num = 10;
    num /= 2;
    //num is now 5
  • %= (Mod Equals)

    int num = 5;
    num %= 2;
    //num is now 1

  • ++

    int num = 5;
    num ++;
    //num is now 6
  • --

    int num = 5;
    num --;
    //num is now 4

Lesson 1.6 Casting and Ranges of Variables

Casting is a way to temporarily charge the type of a variable

int num = 7;
int newNum = 15;
int div = newNum/num;
// The value stored in div will be 2
double div = newNum/num;
// The value stored in div will be 2.0
double div = (double)newNum/num;
// The value stored in div will be 2.14285...
// By casting newNum as a double, we can prevent integer division and allow it to divide properly.

Casting does not change the variable type permanently.

Lesson 2.1 Object: Instances of Classes

Java uses Classes

Classes describe objects

Running the code creates objects

  • “instantiating an object”

  • “create an instance of an object”

Objects

→ have stuff = variables

→ do stuff = methods

EX: Write the Student class code

→ (Store in) file named “Student.java“

In our main file

→ create Student objects

Variables

name

Bob

gpa

2.0

Methods

study

Bob.study()

takeTest

Bob.takeTest

Void Method:

→ returns nothing

public String toString()
// public = privacy spec
// Returns a String variable
// toString = name of method

//--------------------------------------------------------


Lesson 3.1 Boolean Expressions and If Statements

Booleans are variables that can be set to true or false

//Example
boolean isHungry = true;
System.out.println("Are you hungry?" + isHungry);
//Prints out Are you hungry? true

NL

AP COMP SCI

Lesson 1.3 Expressions & Assignment Statements

Creating a “int“ Variable

int num = 7;
  • Is stored in 4 bytes

  • “int“ creates an integer variable

  • “num” is the placement for the name of the variable.

    • EX: numPets, inStorage

  • “7“ is the value of the variable

  • “=” is an assignment operator

    • Assignment operators take whats on the right-hand side and puts that value in the variable.

Creating a “double” Variable

double costBurger = 3.95;
  • Is stored in 8 bytes

Creating a “String” Variable

String student = "Vicky Viking";

Creating a “char” Variable

char mcChoice = 'a';

Only need 2 parts to create a variable

  • By creating a variable, the space is allocated in the stack even if the value is not yet defined.

(+) Operator does 2 things

  • 3 + 2 adds to 5

  • String + number → acts like a concatenation operator

Arithmetic Operators

  • (adding)

  • (subtract)

  • / (divide)

  • * (multiple)

  • % (modulus)

int num = 5 + 7;
//num = 12
int num = 7 - 5;
// num = 2
int num = 10 / 5;
// num = 2
int num = 2 * 5;
//num = 10
int num = 15 % 6;
// num = 3

Lesson 1.4 Compound Assignment Operators

  • += (Plus Equals)

    int num = 5;
    num += 10;
    //num is now 15
  • -= (Minus Equals)

    int num = 5;
    num -= 3;
    //num is now 2
  • *= (Multiply Equals)

    int num = 5;
    num *= 2;
    //num is now 10
  • /= (Slash Equals)

    int num = 10;
    num /= 2;
    //num is now 5
  • %= (Mod Equals)

    int num = 5;
    num %= 2;
    //num is now 1

  • ++

    int num = 5;
    num ++;
    //num is now 6
  • --

    int num = 5;
    num --;
    //num is now 4

Lesson 1.6 Casting and Ranges of Variables

Casting is a way to temporarily charge the type of a variable

int num = 7;
int newNum = 15;
int div = newNum/num;
// The value stored in div will be 2
double div = newNum/num;
// The value stored in div will be 2.0
double div = (double)newNum/num;
// The value stored in div will be 2.14285...
// By casting newNum as a double, we can prevent integer division and allow it to divide properly.

Casting does not change the variable type permanently.

Lesson 2.1 Object: Instances of Classes

Java uses Classes

Classes describe objects

Running the code creates objects

  • “instantiating an object”

  • “create an instance of an object”

Objects

→ have stuff = variables

→ do stuff = methods

EX: Write the Student class code

→ (Store in) file named “Student.java“

In our main file

→ create Student objects

Variables

name

Bob

gpa

2.0

Methods

study

Bob.study()

takeTest

Bob.takeTest

Void Method:

→ returns nothing

public String toString()
// public = privacy spec
// Returns a String variable
// toString = name of method

//--------------------------------------------------------


Lesson 3.1 Boolean Expressions and If Statements

Booleans are variables that can be set to true or false

//Example
boolean isHungry = true;
System.out.println("Are you hungry?" + isHungry);
//Prints out Are you hungry? true