In the Midterm
- Binary numbers
- Primitive Types:
int
, boolean
, String
, double
- Type casting
- Operations:
+
, -
, *
, /
, ++
, --
, +=
, -=
, %
- Boolean Logic: AND, OR, NOT (
&&
, ||
, !
)
- Comparisons:
>
, >=
, <
, <=
, ==
, !=
- Conditional Statements:
if
, else if
, else
- Loops:
for
and while
- Arrays
- Methods
- Passing variables to methods: Primitive types, and reference types ( Arrays )
- Sorting (not including merge sort)
NOT the Midterm
- Complexity
- Recursion
- Merge sort
Interpreting decimal numbers:
Decimal numbers (base 10 numbers)
- Counting with digits -> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
- From right to left:
- The 1nd digit is multiplied by 1 and added to the count
- The 2nd digit is multiplied by 10 and added to the count
- The 3rd digit is multiplied by 100 and added to the count
- The 4rd digit is multiplied by 1000 and added to the count
- The n-th digit is multiplied by 10n-1 and added to the count
Interpreting binary numbers
Binary numbers (base 2 numbers)
- Counting with bits -> { 0, 1 }
- From right to left:
- The 1nd digit is multiplied by 1 ( 20 ) and added to the count
- The 2nd digit is multiplied by 2 ( 21 ) and added to the count
- The 3rd digit is multiplied by 4 ( 22 ) and added to the count
- The 4rd digit is multiplied by 8 ( 23 ) and added to the count
- The n-th digit is multiplied by 2n-1 and added to the count
This is how you convert from a binary string to a decimal string
Binary practice questions
- Convert 37 to binary
- Convert 1010111010 to decimal
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)
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:OPERATOR
variable_1, variable_1OPERATOR
Assignments: variable_1 ASSIGNMENT_OPERATOR
variable_2
Assigment combined with binary operations:
Type Casting
Implicit (automatic) casting:
- Operations between
int
variables or values give an int
result
- Operations between
int
and double
variables or values give a double
result
- The
+
operation between any primitive type and String
give a String
result
Type Casting
Explicit casting:
We can force our program to treat a number as having a different type
Forcing java to treat a double
as an int
Forcing java to treat a int
as an double
Variables practice questions
- What are the types of the following operations
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
- The
true
and false
values
- Any
boolean
variable
- Any comparison between two variables ( yes/no answer translates to
true
/false
values )
- Any combination of the previous three kinds of logical expressions, using AND (
&&
) and OR (||
) operators
- Any of the previous 4 kinds of logical expressions, using the NOT (
!
) operator at the beginning
Boolean expressions practice questions
- What is the value of c after executing this code?
- What is the value of d after executing this code?
Using boolean expressions to conditionally execute code
- The
if
statement
- The
else
statement
- The
else if
statement
Practice questions
In the following code,
what will be the value of result
if
- SOME_VALUE = 4?
- How about if SOME_VALUE = 9?
- And if SOME_VALUE = 11?
The while loop
This works like an if statement
- While CONDITION is true, execute the code insidde the curly braces
- When CONDITION is false, continue with SOME_OTHER_CODE
The for loop
The condition variables initialized in the first part only exist within the curly braces
- start with INITIALIZE_INDEX.
- 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
- Which numbers will be printed by the following code?
- What is the value of c after executing this code?
- Will the following code work?
- Write a program that uses two loops to prints a square (Assignment 2 Warm up question 3)
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
- Write the code to reverse the elements of the test_array
- Write the code to print every character of the string, replacing every 'e' with an 'a'
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
- Write a method that takes an array as an input, and reverses its contents in place. Hints: This method should not return anything ( a void method).
- Write a method that takes an array and an integer
n
as an input. It should return the sum of the first n
elements of the array.
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
- DO ASSIGNMENT 3!!!!!!!!!!!!!!
/