COMP 202 Midterm Review

In the Midterm

  1. Binary numbers
  2. Primitive Types: int, boolean, String, double
  3. Type casting
  4. Operations: +, -, *, /, ++, --, +=, -=, %
  5. Boolean Logic: AND, OR, NOT (&&, ||, !)
  6. Comparisons: >, >=, <, <=, ==, !=
  7. Conditional Statements: if, else if, else
  8. Loops: for and while
  9. Arrays
  10. Methods
  11. Passing variables to methods: Primitive types, and reference types ( Arrays )
  12. Sorting (not including merge sort)

NOT the Midterm

  1. Complexity
  2. Recursion
  3. Merge sort

Binary

Interpreting decimal numbers:

Decimal numbers (base 10 numbers)

173

3 + 10*7 + 100*1

Interpreting binary numbers

Binary numbers (base 2 numbers)

This is how you convert from a binary string to a decimal string

1101101

109

Binary practice questions

If you understood the deal with binary numbers, you should be able to generalize to other bases

Base 4 numbers

  • Counting with -> { ?, ?, ?, ? }
  • From right to left:
    • The 1nd digit is multiplied by ? ( ?0 ) and added to the count
    • The 2nd digit is multiplied by ? ( ?1 ) and added to the count
    • The 3rd digit is multiplied by ? ( ?2 ) and added to the count
    • The 4rd digit is multiplied by ? ( ?3 ) and added to the count
    • The n-th digit is multiplied by ?n-1 and added to the count
  • Convert 123 from base 4 to decimal (base 10)

Variables

Variable types and declaration

Primitive types: int, float, double, char, boolean

Literals (values): 27, 3.14159, 'r', true/false

A special type in Java: String

Declaring variables

To use variables in Java we need to declare them

TYPE variable_name;

TYPE variable_name = INITIAL_VALUE;



Operations with variables

Binary operations: variable_1 OPERATOR variable_2

Unary operations:OPERATORvariable_1, variable_1OPERATOR

Assignments: variable_1 ASSIGNMENT_OPERATOR variable_2

Assigment combined with binary operations:

Type Casting

Implicit (automatic) casting:

Type Casting

Explicit casting:

We can force our program to treat a number as having a different type

(TYPE)variable_name

Forcing java to treat a double as an int


Forcing java to treat a int as an double

Variables practice questions

Logical operations ( Boolean expressions, comparisons )

Boolean expressions

Boolean expressions are anything that can be evaluated to be true or false

The following are boolean expressions

Boolean expressions practice questions

Conditional statements

Using boolean expressions to conditionally execute code

Practice questions

In the following code,


what will be the value of result if

Loops

The while loop


This works like an if statement

The for loop


The condition variables initialized in the first part only exist within the curly braces

  1. start with INITIALIZE_INDEX.
  2. evaluate if CONDITION_ON_INDEX is true
    • if true, DO_SOMETHING; CHANGE_VALUE_OF_INDEX; and go back to step 2
    • if false, continue with SOME_OTHER_CODE

Loops practice questions

Arrays

Declaring Arrays

If we know the size but not the content of the array

TYPE[] variable_name = new TYPE[N];


If we know the content of the array

TYPE[] variable_name = {VALUE_1,VALUE_2, ..., VALUE_N};

Breaking down the declaration of an Array - 1

int: the type of the array

[]: this variable is an array

days_per_month: the variable name

{31,28,31,30,31,30,31,31,30 31,30,31}: The content of the array

Breaking down the declaration of an Array - 2

int: the type of the array

[]: this variable is an array

days_per_month: the variable name

new int: Allocating memory on the computer for this array

[12]: The length of the array (number of elements)

Accesing Arrays


Accesing the character at position i :

month_array[i]

Strings as Arrays


Accesing the character at position i :

month_array.charAt(i)

Arrays practice questions

Methods

Creating methods

Methods that do not return a value

Methods that return a value

Using methods

Passing primitive types to a method

You CANNOT change the value of a primitive type parameter inside a method

Passing reference types to a method

You CAN change the value of a reference type parameter inside a method (e.g. you can modify an array inside a method)

Methods practice question

Sorting and search

Linear Search

 
Given an array of N items, and a target to search
For every position i in the array {
    if the item as position i is our target{
       Finish! We found our target!
    }
}

Binary Search

 
Given an array A of N items, and a target to search
Let start = 0, end = N-1
while target not found{
    Set mid = (start + end)/2
    if A[mid] is our target{
       Finish! We found our target!
    }
    else{
        if A[mid] < target{
            set start = mid + 1
        }
        else if A[mid] > target{
            set end = mid - 1
        }
    }
}

Bubble sort!

 
Start with an unsorted array A with N items

while the array is unsorted{

    For every position between 1 and N {

        if A[i] and A[i-1] are 
        not in the correct order{

           Swap A[i] and A[i-1]

        }

    }

}

Insertion sort!

 
Start with an unsorted array A with N items

For every pos between 1 and N-1 {
    
    Let j = pos 
    Let ITEM = A[pos]
    
    While j > 0 
    and ITEM is smaller than A[j -1] {

        Set A[j] = A[j-1]
        Decrease j by 1

    }

    Set A[j] = ITEM
}

Sorting and Search practice questions

/