Topic: Here's how we program in Java. Actually, this is just the C-ish subset of Java; it is not much object-oriented. We will look into the object-oriented features in Chapters 1–3.
Examples Developed in Class
August 27, 2012
These examples are also available on merlin.iue.edu in the directory /home/info/share/c243/java-review-2012-08-27
Discussion Notes
Appendix A: Java Review
A1 First Program
- Figure A.1: A1Hello.java (restricted access) — restricted to IU East students in CSCI-C243; see Oncourse Modules (the module for the first week) for details.
A2 Variables and Types
- (p. 522) Fig A.2: A2Hello.java (restricted access)
- What are the variables and their types?
- What does
+ do?
- How to change
name to an array of strings?
A3 Loops
- (p. 524) Fig. A5: A5While.java (restricted access)
- How do we run this to greet three persons?
- Figures A6–A9: How is each different?
- How many would prefer having only one type of iteration control statement? Which one would that be?
A4 User Interaction
p. 526 Creating a Scanner (java.util.Scanner)
Using it to read a line (a String), an int, a double, etc.
nextLine()
nextInt()
nextDouble()
- Use
nextLine() to clear the rest of a partially read line.
- Do exercise A.11, p. 526
A5 Branching
- Ask?
- p. 528 multiway
if statement
- p. 529
switch statement
- Switch.java (restricted access)
- Is it any better or worse?
- When would you recommend
if, when switch?
- Give an example of an
if statement that can't be rewritten as a switch, or where using if is better than switch
- Hint:
switch (expr) case k: ... requires k to be a constant; the same expr is compared to a constant in each case; and the expr is evaluated only once.
Recursion
We can use the if statement to define recursive functions. - Explain recursion, base case, recursive step. - Example, to be developed in class: computing 2n. - Solution: PowerOf2.java (restricted access)
A6 Methods
- p. 530 Fig. A14: A14Guess.java (restricted access)
- In detail, what happens when execution reaches line 16?
- p. 531 Exercises A14–15 (skip)
A7 Constants
(Ask)
- p. 533 fig. A17:
- Where's the constant?
- Explain everything on the line that declares the constant.
- Where is the constant used?
- Why is it sometimes a good idea to create a constant naming an instance of Scanner?
- Exercise A16
A8 Operators
p. 536: Exercises A17–A20
A9 Debugging
- p. 537 How do we go to a specific line in our text editor?
- Well, it depends on the text editor!
- In Emacs, 3 ways:
- Arrow down, watching line number on status bar
M-g g〈line_number〉RET
- After compiling with
M-x compile RET, use C-x ` (backquote) to go to next error message.
- Have we ever used println statements or equivalent (see bottom of page) for debugging or testing?
- We can change
System.out to System.err ...
- p. 538 Have we ever used assertions?
- What does the assertion do?
- Why are assertions better than
if/print?
- How do we enable assertions?
- Exercise A22
- Have we ever used an interactive debugger?
A10 Coding Conventions
(Have a look at our coding standards.)
- Rules for identifiers:
- constants
- class names ["CamelCase"]
- variable and method names
- "Standard identifiers":
i, j, k, result, etc.
- Otherwise variable names should be meaningful.
- Blocks
- No, you do not always have to use
{} for one-liners.
- Indent correctly!
- Comments
- Access levels
- Arrangement of class members
- These are some good rules; follow them!
- Exercises A23-24.
Revision log
- 2010 Sep 1B. Added link to solution of recursion problem.
- 2010 Sep 1A. Added link to coding standards, and a small section on recursion.
- 2010 Aug 30. Reformatted, with markdown.
- 2008 Aug 25. Minor edit.
- 2007 Sept 5. Initial version?