# Java: What you need to know in CMIS 102A

This is a summary of the topics that your are expected to know (ie, will be on the final exam) at the end of CMIS 102A. Most of this would be covered in every introductory programming course, although some would cover less and some more.

## Basics

- Class: Name starts with uppercase letter. File name must be identical. `MilesToKilometers.java`
- Comments: // to end of line. At top with program, name, date. Before sections.
- Declare main: `public static void main(String[] args)`

## Style Essentials

- Good variable names. Meaningful. Start with lower case letter.
- Use named constants: No magic numbers. `final double KILOMETERS_PER_MILE = 0.621;`
- Whitespace: Indentation is essential. Blanks lines should separate sections.

## Types and declarations

- Primitive types: `int`, `double`. Later `char` and `boolean`.
- Object (reference) types: `String`, `Scanner`, ...

## Declarations

- Declare variables before use. Examples: `int score; double total = 0.0; String name;`
- Variables declared in a block (surrounded by { and }) are not visible outside the block.

## Expressions

- Numeric (int, double) operators
  - Operators: +, -, *, /, % (mod or remainder). Examples: 5 / 2.0 is 2.5. 5 / 2 is 2. 5 % 2 is 1.
  - Increment/decrement: ++, -- (don't embed in expression).
  - Comparisons: <, <=, ==, !=, >=, > (return boolean value)
  - Cast: `i = (int)d; // Truncates value of d`
  - Precedence: 1: *, /, %. 2: +, -. 3: comparisons. 4 logical. 5: assignment.
- Logical (boolean) operators
  - && ("and" true if both true), || ("or" true if either true), ! ("not" inverts value)
- Strings
  - String: + operator concatenates strings, converts non-string to string if necessary.
  - Indexing of characters starts at 0.
  - String methods: `s.length()`, `s.substring(from)`, `s.substring(from, upto)`, `s.charAt(pos)`, `s.toUpperCase()`, `s.toLowerCase(`), `s.trim()`, `s.indexOf(t)`, `s.lastIndexOf(t)`
  - Equality comparison: `s.equals(t)` and `s.equalsIgnoreCase(t)` return boolean.
  - Relative comparison: `s.compareTo(t)` and `s.compareToIgnoreCase(t)` return <0, 0, >0.
  - Null comparison: Use `==` and `!=` only when comparing to `null`.

## Control Flow

- `if`
  - Three forms: no else, with else, or in "else if" style.
- `switch` - Selects a case based on a single value.
  - Keywords: `switch`, `case`, `break`, `default`.
- Loops: while, for, do-while.
  - While loop: Like "if" that repeats until false.
  - While loop: Can use "while (true)" if there is a "break" inside loop.
  - For loop: used when counting. `for (int i = 0; i < 100; i++)`. Can also go from high to low.
  - Do while loop: Always executes one time. Go back if condition is true.
  - Do while loop: Used for repeat input until OK, asking user if they want to repeat.
  - `break` exits immediately from enclosing loop.

## Console I/O

- Output: `System.out.println(...);` or `System.out.print(...);`
- Input:
  - `import java.util.*;`
  - `Scanner in = new Scanner(System.in);`
  - Use methods to get values: `in.nextInt()`, `in.nextDouble()`, `in.next()`, `in.nextLine()`.
  - Test with `in.hasNextInt()`, `in.hasNextDouble()`, ...

## Methods

- Calling methods.
- Defining static methods.

Copyleft 2006 Fred Swartz