Would the program continue to work if you omitted the line starting with //?
Answer: Yes – the line starting with // is a comment, intended for human readers.
The compiler ignores comments.
Self Check 1.12
What does the following set of statements print?
System.out.print("My lucky number is");
System.out.println(3 + 4 + 5);
Answer: The printout is
My lucky number is12
It would be a good idea to add a
space after the is.
Editing a Java Program
Use an editor to enter and modify the program text
Java is case-sensitive
Be careful to distinguish between upper- and lowercase letters
Lay out your programs so that they are easy to read
Compiling and Running a Java Program
The Java compiler translates source code into class files that contain instructions for the Java virtual machine
A class file has extension .class
The compiler does not produce a class file if it has found errors in your program
The Java virtual machine loads instructions from the program's class file, starts the program, and loads the necessary library files as they are required
HelloPrinter in a Console Window
HelloPrinter in an IDE
From Source Code to Running Program
Self Check 1.13
Can you use a word processor for writing Java programs?
Answer: Yes, but you must remember to save your file as plain text.
Self Check 1.14
What do you expect to see when you load a class
file into your text editor?
Answer: A sequence of random characters, some funny-looking.
Class files contain virtual machine instructions that are encoded as binary
numbers.
Errors
Compile-time error: A violation of the programming language rules that is detected by the compiler
Example:
System.ou.println("Hello, World!);
Syntax error
Run-time error: Causes the program to take an action
that the programmer did not intend
Use defensive programming strategies to minimize the likelihood and impact of errors
Apply testing and debugging strategies to flush out those errors that remain
Self Check 1.15
Suppose you omit the // characters from the HelloPrinter.java program
but not the remainder of the comment. Will you get a compile-time error or a run-time
error?
Answer: A compile-time error. The compiler will not know what to do with the word
Display.
Self Check 1.16
When you used your computer, you may have experienced a program that
crashed (quit spontaneously) or hung (failed to respond to your input). Is that behavior a compile-time error or a run-time error?
Answer: It is a run-time error. After all, the program had been compiled in order for you to run it.
Self Check 1.17
Why can't you test a program for run-time errors when it has compiler errors?
Answer: When a program has compiler errors, no class file is produced, and there is nothing to run.
Algorithms
Algorithm: A sequence of steps that is:
unambiguous
executable
terminating
Algorithm for deciding which car to buy, based on total cost:
For each car, compute the total cost as follows:
annual fuel consumed = annual miles driven / fuel efficiency
annual fuel cost = price per gallon x annual fuel consumed
operating cost = 10 x annual fuel cost
total cost = purchase price + operating cost
If total cost1 < total cost2
Choose car1
Else
Choose car2
Pseudocode
Pseudocode: An informal description of an algorithm
Describe how a value is set or changed:
total cost = purchase price + operating cost
Describe decisions and repetitions:
For each car
operating cost = 10 x annual fuel cost
total cost = purchase price + operating cost
Use indentation to indicate which statements should be selected or repeated
Indicate results:
Choose car1
Program Development Process
Self Check 1.18
Investment Problem: You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original?
Algorithm:
Start with a year value of 0 and a balance of $10,000.
Repeat the following steps while the balance is less than $20,000.
Add 1 to the year value.
Multiply the balance value by 1.05 (a 5 percent increase).
Suppose the interest rate was 20 percent. How long would it take for the investment to double?
Answer: 4 years:
0 10,000
1 12,000
2 14,400
3 17,280
4 20,736
Self Check 1.19
Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of
calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees.
Give an algorithm to compute the monthly charge for a given number
of minutes.
Answer:
Is the number of minutes at most 300?
a. If so, the answer is $29.95 × 1.125 = $33.70.
b. If not,
1. Compute the difference: (number of minutes) – 300.
2. Multiply that difference by 0.45.
3. Add $29.95.
4. Multiply the total by 1.125. That is the answer.