# Java Notes

# Overview of Introductory Programs

## What is an "Introductory Program"?

No OOP, no GUI. These programs are classified as introductory because they don't handle the advanced topics such as Object-Oriented Programming (OOP) and Graphical User Interface (GUI) programing.

Why delay OOP? Some advocate an "objects first" approach, but my experience has been that students struggle with the OOP issues, and this is not helped by the unmotivating and impractical examples that are usually used.

Why delay GUI? Java makes introductory GUI programming complicated and difficult. There is no reason it should be that hard for simple programs, but it is. So it's generally not taught at the beginning. Too bad, because it doesn't need to be hard.

## Organized by flow

Control flow often is used to order the teaching of topics. Control flow can be taught in many orders, but for these example programs it's in the order:

1. Sequential flow.
2. Conditional flow (`if`, `switch`)
3. Loop flow (`while`, `for`, `do-while`)
4. Method calls.
5. Exceptions.

## 1. Sequential Flow (emphasis on basic structure, types, variables, IO)

1. Output comment
   - Example
     - Console: Hello Earthling
     - Dialog: Hello Earthling
   - Purpose
     - Basic structure: class definition, main definition, braces, indentation, semicolons.
     - Flow: Sequential from top to bottom.
     - Output: `System.out.println(...)` and `System.out.print(...)`.
     - Data types: String literals.
   - Other variations
     - Display other greeting.
     - Display multiple lines of text.
     - May introduce backslash escaped characters such as "\n" and "\"".
2. Input, output
   - Example
     - Console: Take me to your leader - Reads one word, prints message with concatenation.
     - Dialog: Take me to your leader - Reads one word, prints message with concatenation.
3. Input, numeric calculation, output
   - Understand
     - Read value, calc/convert - input, more numbers.
   - Example
     - Console: Convert Miles to Kilometers -
   - Variations
     - Just about any conversion or calculation problem.
     - BMI - Body Mass Index. In metric, in English units.
4. Strings
   - Understand
     - Read string, use string methods such as indexOf, substring, ... .
   - Example
     - Example: Console: Capitalize
   - Variations
     - To uppercase
     - Indexing - "First Last" to "Last, First"
     - Capitalize names "mickEy MaUs" to "Mickey Maus"
     - Convert mm/dd/yyyy date into yyyy-mm-dd style.
     - Find email address.

## 2. Conditional flow (`if-else`, `switch`)

- Max/min of two numbers. if
- Max/min of three numbers. nested if
- Leap year - Boolean operators.
- All "calculators" above can have the following additions:
  - Input legality. Check the input values for legal range (eg, height must not be less than zero, or more than ...).
  - Output interpretation. The output should sometimes be described, eg, turning a number into a grade, giving advice about BMI, etc.

## 3. Loops (`while`, `for`, `do-while`)

- Understand
  - Three loop termination styles: Read to sentinal, fixed number of times, or to EOF.
  - How to compute sum, min, max, avg, ...
- Examples
  - Example: Console: Min, max, average income
  - Example: Dialog: Adding Machine
  - Example: Dialog: Longest Word
- Variations
  - Print 1 to 10 - Using while, for, or do-while.

## Methods

- Example: Hammurabi 0
- Understand: How to write simple method to compute something from the parameters, and return the value.