CS 111 Essentials
Todd W. Neller
Note! This is not all that you are responsible for knowing. These slides summarize main content and defer details to required readings!
Class 1: Logging In, Course Website
Class 2: Hello.java, java, javac, bash
Source Code
.java extension
Plain text, human readable
Java Bytecode
.class extension
Binary, like machine-code
Intel
AMD
Qualcomm
javac
java
compile - translate program from higher-level language to lower-level language
interpret - translate and execute program on target processor
Intel JVM
AMD JVM
Qualcomm JVM
Java Virtual Machine (JVM)
Processors
Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Java commands:
javac Hello.java
java Hello
Bash commands: ls, mkdir, cd, nano/gedit, and many more in our Bash tutorial…
syntax
error
logic
error
Class 3: Eclipse, Style, and Comments
Programming style:
Comments:
// line comment (characters after "//" are ignored until end of the line)
/*
multi-line block comment
*/
/**
Javadoc multi-line block comment
*/
Working with Eclipse:
Class 4: Elementary Programming
Scanner - object for reading from stream of characters�System.in - standard input stream of characters�nextInt() - read the next integer from the Scanner�<type> <variable_name> = <value>; - variable declaration and assignment�System.out.print/println() - print a value without/with line break�(<type>) cast (convert) to datatype <type>
Class 5: Elementary Programming
final - Java constant, UPPERCASE_NAMING_CONVENTION
Math methods: Math.sqrt( ), Math.pow( , ), etc.
Number representations: bases, integer (42), binary (0b101010), octal (052), hexadecimal (0x2a), double (1.2, 3.4e-5d, -6.7e8D), float (1.2f, 3.4e-5F)
Integer division (1 / 2), floating-point division (1.0 / 2.0, (double) 1 / 2)
Remainder 5 % 3 == 2
Augmented assignment += -= *= /=
Preincrement (++i), postincrement (i++), pre/postdecrement (--i, i--)
Casting (type conversion), e.g. (int) truncates to an integer.
Math.round( ), Math.floor( ), Math.ceil( )
Class 6: Common Pitfalls, �Development Process
Common pitfalls:
Software Development Processes:
Class 7: Selection - boolean type, relational operators, if statements
boolean type true / false
Relational operators (<, <=, >, >=, ==, !=)
if statements, e.g. if-else chain: nested-if statement: �
Class 8: Selection - common errors, logical operators, random numbers
Common errors: missing braces, extra semicolon (after condition), bad style (e.g “== true”), dangling-else, assuming exactness of approximate floating-point operations.
Logical operators: && (and), || (or), ! (not), ^ (exclusive or - either or)
Random numbers:
Class 9: Selection - switch, selection operator, operator precedence, debugging
Switch statement: Selection/ternary operator (if-else expression!):�
Operator precedence: (var++ var--), (+ - (unary) ++var --var), casting, !, (* / %), (+ - (binary)), (< <= > >=), (== !=), ^, &&, ||, ?, (= += -= *= /= %=)
condition ? true_expression : false_expression
Debugging in Eclipse:
Source: Vogella tutorial
Class 10: Math package, characters
Math.___(...) - many of Java’s mathematical functions are of this form, using doubles and radians:
toRadians, toDegrees, sin, cos, tan, asin, acos, atan, log, log10, exp, pow, sqrt, round, floor, ceil, max, min, abs
Math.PI, Math.E - constants 𝜋, e
char - a single 16-bit Unicode character primitive data type
Character.___(...) - Java’s character functions, e.g.:
isDigit, isLetter, isUppercase, isLowercase, toUppercase, toLowercase
Class 11: Strings
String - a fixed (“immutable”) list of characters
substring, length, charAt, compareTo, equals, equalsIgnoreCase, regionMatches, startsWith, endsWith, indexOf, lastIndexOf, concat, replace, toUppercase, toLowercase, valueOf
0 | 1 | 2 | 3 | 4 |
H | e | l | l | o |
index
char
Class 12: printf, JOptionPane popups, Scanner
Class 13: Loops - while, do-while
Output:
0 1 2
Output:
3 2 1
Class 14: Loops - for, loop choice
Loop Choice:
Class 15: Loops - Nested Loops
Consider a “stepping stone” strategy for implementation, either inside-out/outside-in.
Class 16: Methods - syntax
Method syntax:
<modifiers> <return type> <method name>(list of parameter declarations) {
code block
}
modifiers:
return type:
Method signature: <method name>(list of parameter declarations)
Class 17: Methods - Modularization Benefits
Benefits of modularization with methods:
Class 18: Methods - Variable Scope, Stepwise Refinement
Variable scope: the part of a program where a variable can be referenced
Top-down approach: Decompose problem into subproblems and express the solution in terms of subproblems that haven’t been solved yet. Repeat with subproblems as needed. Gradually implement from problems to subproblems to subsubproblems…
Bottom-up approach: Implement the smallest possible subproblems and gradually compose them into solutions of larger subproblems, etc.
Both top-down and bottom-up approaches may be used together.
Class 19: Arrays - basics of 1D arrays
Common array algorithms: initializing iteratively (data, random), accumulating, finding smallest/largest value/index, shuffling, shifting
Class 20: Arrays - copying contents, arguments and return values, variable-length argument lists
[0, 0, 0, 0, 0, 42, 2, 3, 0, 0]
[42, 2, 3]
[4, 5, 6]
Class 21: Arrays - searching, sorting, Arrays methods, main String[] args
Linear search: Iterate through array indices. Return the index if the target value is found. Otherwise, return -1 (not found).
Binary search: Assumes sorted data. It’s the number guessing game on indices! “Higher” or “lower” is based on comparison of target to value at current guess index. Return the index if the target value is found. Otherwise, when low > high (failure of search) return -1.
Selection sort: For each index i in succession, find the smallest value from indices >= i, swap it with the value at index i.
java.util.Arrays methods: sort, binarySearch, equals, fill, toString
In class C method main(String[] args), args is a String array containing the words following “java C”, so “java C Testing 1 2 3” would result in args being {"Testing", "1", "2", "3"}.
Class 23: Multidimensional Arrays - 2D array declaration, creation, access, operations, arguments, examples
Class 24: Multidimensional Arrays - examples, 3D+ arrays
Class 25: Objects and Classes - defining & creating objects, fields, constructors, methods
Class 26: Objects and Classes - access specifiers, static keyword, getters & setters
…
Class 27: Objects and Classes - object arrays, immutable objects, variable scope, keyword this
Class 28: Object-Oriented Thinking - class abstraction, class relationships, design principles
Class 29: Object-Oriented Thinking - wrapper classes, auto-(un)boxing, BigInteger, BigDecimal
Class 30: Object-Oriented Thinking - String, StringBuilder, StringBuffer
Class 31: Recursion - recursive problem solving, base case(s), recursive step(s)
…
→ gcd(2226, 504)
→ gcd(504, 210)
→ gcd(210, 84)
→ gcd(84, 42)
→ gcd(42, 0)
→ 42
Class 32: Recursion - recursive helper methods
Class 33: Recursion - dynamic programming
Fibonacci number computation without DP
Fibonacci number computation with DP
Class 34: java.util Goodies - Generics, ArrayLists
Class 35: java.util Goodies - Stack, Queue (LinkedList), PriorityQueue, Set/HashSet/TreeSet
Class 36: java.util Goodies - Map/HashMap/TreeMap