Slightly less basic Java
- 3.11: assignments, initialization, initialization & declaration
- 3.14: more on constants
- 3.17: ++, --, +=, and similar operations
- 3.18: tracing code
- Testing
Discussion: why worry about spaces in output?
Assignment
- Write program to compute time to travel on city highway
- Prompt for distance, compute minutes of travel time assuming 45 mph average
- Rewrite to compute time to travel on back streets (new distance,
assume average 23 mph)
- Assignment vs. initialization
- Assignment: replace value
- Initialization: set value first time (initial value)
- Should always initialize variables at point of declaration unless the
value is not going to be used.
- Textbook: declare variables at top of main; this is a poor practice
- Additional assignment operators:
- +=, *=, -=, etc.
- ++, -- (foreshadow loops)
Tracing Code
- Step through above problem
- Note:
- Stepping
- Variable values
- Breakpoints
- Debug vs. run
Algorithms, Revisited
- Chapter 2
- Rewrite program using pseudocode
- Pseudocode: "sort of" code
- Algorithm: the structure of the solution
- Implementation: the actual program written in Java (or another language)
- Note basic structure of this program: sequence
- Simple, but very limited
- Complexity is in the rules about the individual pieces: types,
expressions, string operations, etc.
- Adding more complex behavior
- Would like the program to recommend one path over another
- Needs to make a decision
- Other types of decisions
- Suggestions?
- Input sanity check
- One solution: print message and quit
- Another: convert to positive
- Which is actually better?
- Add ability to describe traffic
- Review terms
- Sequence, decision
- Algorithm
- Pseudocode
- Implementation/code
- How to write a program
- Get suggestions of some of the things that need to be done
- Steps:
- Be sure you can hand-compute the results
- Write out an algorithm as pseudocode
- Write solution for part of problem
- Fill out comment block at top right away!
- Test that portion works
- Repeat for other portions
- Test full solution, especially testing with obscure data
- Review initial assignment to make sure nothing missed
Loops
- Initial program sort of odd
- Why just two branches?
- If user enters a bad input, should we really just exit?
- Solution: introduce loops
- For now: ask user how many paths to look at
- Pseudocode for looping: repeat n times
- Advanced: add something to keep track of best time
More modern input and output
- Section 3.25 of textbook
- Try:
- import javax.swing.JOptionPane;
- JOptionPane.showMessageDialog(null, "message")
- JOptionPane.showInputDialog("prompt")
- Integer.parseInt(JOptionPane.showInputDialog("prompt message"));
Exercises
(If time - all in psuedocode)
- Write pseudocode to compute time of day in seconds
- Add error if time is invalid
- Read name, print first 3 letters if long enough, 1 letter otherwise
- Add list of numbers entered by user (where user states how many up front)
Review
- assignment, initialization, declaration
- tracing, breakpoint, debug vs. run
- algorithms, pseudocode, implementation
- sequence, decision, loop
- Note: a lot of terms!
- Other terms to pay attention to: comment, input/output, variable,
float, double, integer, character, string
- Being caught up:
- Ch. 2, Ch. 3 through 3.25