1.1 Programming basics

Program starts in main(), executing statements in {}
Statement ends in ;

// declare an integer variable
int num;
// assign
num = 1;

Scanner: text parser that can get numbers, words, or phrases from an input source such as the keyboard

System.in: keyboard input

import java.util.Scanner;
Scanner scnr = new Scanner(System.in);
int x = scnr.nextInt();

System.out.print: continue printing on the same output line
System.out.println: start a new output line after the outputted values
String literals: text in double quotes ""

String conversion: convert the other operand to a string when using + with a String

QUESTION:
What are we going to be tested on? The definition such as the program starts in main() and what is statement? Past exams?

1.2 Programming knowledge

Integer division: only keep result’s integer part

// integer division will truncate the decimal part round toward zero
// mathematical result: 2.75; integer division: 2
num = 11 / 4;
// mathematical result: -2.75; integer division: -2
num = -11 / 4;

1.3 Basic debugging

Program: a series of instructions (statements)
Bug: a problem cause
Debugging: troubleshooting
Visual inspection: looking at statement one-by-one to try to find a bug

Visual inspection process

  1. Hypothesis: the statement has a bug
  2. Test: a visual inspection
  3. Validate hypothesis if found a bug otherwise inconclusive result

Debug output statements
Output helps determine whether the preceding statement has the bug

Hierarchical debugging

  1. Divide the statements into regions (related statements)
  2. Insert debug output statement after each region
  3. Create sub-hypotheses if a region shows unexpected output

1.4 Common errors: Methods and arrays

Method signature errors
Design methods with single purpose

Return typePurpose
Array referenceNew array constructed
VoidArray contents modified
Something elseArray contents not modified
Reference array errors
Strictly pass-by-value: the reference to the array is passed by value

NOTICE: Java strictly enforces pass-by-value

Side effect: an error unintentionally modifies data

1.5 Perfect size arrays

Perfect size arrays: the number of elements exactly equal to the memory allocated, used when array has a fix sized in the context of the problem

Array initializer: only use when declaring an array, not when reassigning it

char[] letters = {'a', 'b'};
// error since array initialization can only occur when the declaring reference
letters = {'a', 'b', 'c'};

Access number of array elements and the data from array reference
Method declaration that returns array implicitly indicates perfect size array since a method cannot return two items

// indicates perfect size array
// no need to pass current size seperately (use arr.length)
int[] fill(int size, int val) {...}
void fill(int[] arr, int val) {...}
// indicates imperfect size array
// may have extra unused space (pass size seperately)
void shuffle(int[] arr, int currentSize) {...}

1.6 Oversize arrays

Oversize array: the number of elements used less than or equal to the memory allocated, used when array has unknown number of elements or the number of elements varies over time

Small size: fail when the number of required elements exceeds the number of allocated elements
Large size: wasteful and possible performance degrade

Use a final constant linked to the array name instead of numeric literals to make the code easy to understand

1.7 Methods with oversize arrays

Pass both the array reference and current size
Return the array’s new size

int addElement(int[] arr, int currentSize, int element) {
    if (currentSize == arr.length) return currentSize;
    arr[currentSize++] = element;
    return currentSize;
}
// pass array reference and current size
// reassign the returned value to current size
currentSize = addElement(arr, currentSize)

NOTICE: Array.toString() prints the entire array contents

1.8 Comparing perfect size and over size arrays

Perfect size arrayOversize array
Method signatureOne parameter for array reference and not parameter for the array sizeTwo parameters: one for the array reference and one fore the array size
Return typeReturns perfect size array reference or voidReturns new oversize array size or void
UsageArray not modified, only array contents modifiedArray not modified, only array contents modified, only array size modified, array modified
AdvantageFewer paramtersEasy to change size
DisadvantageConstruct new array on size changeMore parameters
int return type does not guarantee the use oversize array

NOTICE: method signature includes only method name, parameter, number and order of parameters, and formal type parameters and exclude return type, access modifiers, parameter names, the exceptions

// methods without side effects support perfect size array and oversize array
int search(int[] arr, int size, int target) {...}
// support perfect size array
search(perfectSizeArr, perfectSizeArr.length, target);
// support oversize array
search(oversizeArr, size, target);
 
// however, methods changing array size only support oversize array
int remove(int[] arr, int size, int target) {...}
// support oversize array only
remove(oversizeArr, size, target);

Java API often provides one method for perfect size arrays and another for oversize arrays

QUESTION: Do we need to write oversize array to have fromIndex and toIndex? Also, some of the questions are stupid such as

// thid does not work since but the returned int has to be sourceArray.length (or maybe not since copyArray.length might be smaller)
void copyArray(int[] sourceArray, int[] copyArray)

1.9 Using references in methods

Storage of primitive data types and arrays
Store an int and double directly in stack frame
Store array in heap and store reference in stack frame
Copy array’s reference to method’s stack frame when passing array to a method

NOTICE: arr: an array reference variable store in stack, arr[0]: element of array store in heap

1.10 Returning arrays from methods

Return reference when construct an array or modify array size within a method
Cannot modify an array’s size because adjacent memory locations may hold other data

NOTICE: check assignment of returned array reference