1 of 24

Beginning of Year Review

AP Computer Science A

2 of 24

Objectives

  • Define variables and explain how they are used in computer programming
  • Explain what a "strongly typed" language means
  • Identify errs related to variables
  • List primitives used in Java

3 of 24

Variables

  • Variables are used to manage a computer's memory locations
  • Each variable relates to a memory location
  • Each variable controls the contents of that memory location
  • Assignment operator ' = ' is used to store a value in that memory location
  • Amount of memory needed is depended upon type of value

4 of 24

Variables

  • Specify type of values to be stored - strongly typed language
  • Type of value can Not be changed for a particular variable
    • ie. loss of precision err int sum = 3.5;

  • Err - "Cannot not find symbol"
    • not been declared
  • Err - "might not have been initialized"

int sum;

Declaration statement

sum = 3.5;

Assignment Statement

5 of 24

Variables

  • Naming conventions:
    • letters, numbers, _, $ allowable
    • Can not begin with a number
    • No spacing (special character)
  • Class name begins with capital letter

6 of 24

Primitive Data Types

char

  • 16 bits
  • unicode
  • 'a', 'T', '('
  • default: \u0000

Null

boolean

  • 1 bit
  • true or false
  • default: false

byte

  • 8 bits

short

  • 16 bits

int

  • 32 bits

long

  • 64 bits

  • default: 0 for each

float

  • 32 bits
  • 0.0f

double

  • 64 bit
  • 0.0d

7 of 24

Check for understanding

Activity:

AP Classroom 1.2 Variables

8 of 24

Numbering Systems

9 of 24

Numbering Systems

Base 10 - decimal

  • 0-9

Base 16 - hexadecimal

  • 0-9, a-f

Base 2 number - binary

  • 0, 1

Base 8 number - octal

  • 0-7

10 of 24

Doubles and Scientific Notation

  • Decimal numbers that have more than seven digits to the left of the decimal point are printed in scientific notation.
    • 123456789.123 = 1.23456789123E8

  • Java considers any number named in scientific notation to be a floating point number
    • 4E3 = 4000.0

11 of 24

Casting

  • The process of changing a value of one data type into a value of another data type
    • int a = (int) 2.5; narrowing
    • without the cast an error will be returned - loss of precision
  • Java will implicitly store an int as a double
    • double a = 5; returns 5.0 widening
    • double a = (double) 5;

12 of 24

Arithmetic Expressions

  • Arithmetic operators: *, /, %, +, -, (order of precedence)
    • - (subtraction) is unary operator: only one operand needed
    • -a
  • Operands can be variables or literals
    • sum = a + 2;
    • average = sum / 3.5

13 of 24

Arithmetic Expressions

  • Five shorthand arithmetic operators:
    • += ? same as a=a+ ?
    • -= ? same as a=a- ?
    • *= ? same as a=a* ?
    • /= ? same as a=a/ ?
    • %=? same as a=a% ?

? = a number literal or variable

14 of 24

Arithmetic Expressions

  • Either operand is a double, then the other operand will (if necessary) be implicitly cast to a double and the result will be a double.
    • a = 5 + 3.2 ... a=5.0+3.2 ... results in a double 8.2
    • c = 5 + (int) 3.2 or c = (int) (5+3.2)

15 of 24

Arithmetic Expressions

  • Calculations are not always exact
  • All integers lie in the range from Integer.MIN_VALUE through Integer.MAX_VALUE
  • Calculation whose result lies outside this range - overflow - strange things can happen.

16 of 24

Check for Understanding

AP Classroom

1.3 Expressions

17 of 24

Strings

  • String phrase = "Hello World";
  • String literal
  • Each letter is a character
  • Location is index - begins at 0
  • Number of character is length ( .length() )
  • Escape Sequence indicate with \ (backslash)
    • \" , \n, \\

18 of 24

String Concatenation

  • Build a String with two or more shorter Strings
    • "Go " + "Jaguars" evaluates to Go Jaguars
    • + operator concatenates if both operands are Strings
    • + operator adds if both operands are double or int
    • + operator concatenates if first operand is String

19 of 24

String Methods

  • int length() // number of characters in String

  • String substring(int from, int to)

// returns the substring beginning at from and ending at to-1

  • String substring(int from)

// returns substring(from, length())

20 of 24

String Methods

  • int indexOf(String str)

// returns the index of the first occurrence of str; returns -1 if not found

  • int compareTo(String other)

// returns a value < 0 if this is less than other

// returns a value = 0 if this is equal to other

// returns a value > 0 if this is greater than other

21 of 24

String to Number Conversion

  • Integer.parseInt
    • String to int

  • Double.parseDouble
    • String to a double

  • Double.toString
    • double to String
    • " " + 5.2;

  • Integer.toString
    • int to String
    • "" + 6;

IMACS Ex. 29

22 of 24

Check for Understanding

AP Classroom 2.7_StringMethods

23 of 24

Booleans

  • Java provides six relational operators:

<, <=, >, >=, ==, and !=

  • compare values
  • relational expression or boolean expression

24 of 24

Resources

Data Types: Florida Institute of Technology - http://cs.fit.edu/~ryan/java/language/java-data.html

Data Types: Oracle Java Documentation - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html