Building Java Programs
Building Java Programs
Building Java Programs
Chapter 1: Introduction to
Java Programming
compile execute
source code byte code output
Hello.java Hello.class
compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:5: ';' expected
}
^
2 errors
Copyright 2006 by Pearson Education 13
Fixing syntax errors
Error messages do not always help us understand what is wrong:
The compiler does tell us the line number on which it found the error...
But it is not always the true source of the problem.
System.out.println();
Prints a blank line on the console.
Examples:
"hello"
"This is a string"
"This, too, is a string. It can be very long!"
A string may not contain a " character. (The ' character is okay)
"This is not a "legal" String either."
"This is 'okay' though."
System.out.println("\ta\tb\tc");
System.out.println("\\\\");
System.out.println("'");
System.out.println("\"\"\"");
System.out.println("C:\nin\the downward spiral");
/ \ // \\ /// \\\
a b c
\\
'
"""
C:
in he downward spiral
reading: 1.4
...
Copyright 2006 by Pearson Education 24
Redundancy in algorithms
How would we bake a double batch of sugar cookies?
Unstructured: Structured:
Mix the dry ingredients. 1. Make the cookie batter.
Cream the butter and sugar. 2a. Bake the first batch of
Beat in the eggs. cookies.
Stir in the dry ingredients. 2b. Bake the second batch
Set the oven ... of cookies.
Set the timer.
3. Add frosting and sprinkles.
Place the first batch of cookies
into the oven.
Allow the cookies to bake. Observations about the
Set the oven ... structured algorithm:
Set the timer. It is hierarchical, therefore
Place the second batch of easier to understand.
cookies into the oven. Higher-level operations help
Allow the cookies to bake.
eliminate redundancy.
Mix ingredients for frosting.
Output:
Now this is the story all about how
My life got flipped turned upside-down
Example:
public static void printWarning() {
System.out.println("This product is known to cause");
System.out.println("cancer in lab rats and humans.");
}
Copyright 2006 by Pearson Education 28
Calling a static method
Syntax for calling a static method (cooking using the recipe):
In another method such as main, write:
<method name> ();
Example:
printWarning();
Resulting output:
This product is known to cause
cancer in lab rats and humans.
This product is known to cause
cancer in lab rats and humans.
Program's output:
Now this is the story all about how
My life got flipped turned upside-down
Lollipop, lollipop
Oh, lolli lolli lolli
reading: 1.2
can you explain why each of the above identifiers is not legal?
You may not use char or while for the name of a class
or method; Java reserves those to mean other things.
You could use CHAR or While, because Java is case-sensitive.
However, this could be confusing and is not recommended.
Examples:
/* A comment goes here. */
/* It can even span
multiple lines. */
// This is a one-line comment.
sing();
}
Example:
// This method prints the lyrics to the first verse
// of my favorite TV theme song.
// Blank lines separate the parts of the verse.
public static void verse1() {
System.out.println("Now this is the story all about how");
System.out.println("My life got flipped turned upside-down");
System.out.println();
System.out.println("And I'd like to take a minute,");
System.out.println("just sit right there");
System.out.println("I'll tell you how I became the prince");
System.out.println("of a town called Bel-Air");
}
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
______
/ \
/ \
+--------+
______
/ \ Second version of program
/ \
\ / (structured with redundancy):
\______/
Identify the structure of the output.
\ /
\______/ Divide the main method into several static
+--------+ methods based on this structure.
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
______
/ \ The structure of the output:
/ \
initial "egg" figure
\ /
\______/ second "teacup" figure
______
/
/ \
\
This structure can be represented by methods:
| STOP | drawEgg
\ /
drawTeaCup
\______/
drawStopSign
______
/ \ drawHat
/ \
+--------+
______
/ \ Third version of program
/ \
\ / (structured without redundancy):
\______/
Identify any redundancy in the output, and further
\ /
\______/
divide the program into static methods to
+--------+ eliminate as much redundancy as possible.
______
/
/ \
\
Add comments to the program to improve its
| STOP | readability.
\ /
\______/
______
/ \
/ \
+--------+
______
/ \ The redundancy in the output:
/ \
\ /
\______/
top half of egg: reused on stop sign, hat
\ /
bottom half of egg: reused on teacup, stop sign
\______/ divider line: used on teacup, hat
+--------+
a single line, so making it a method is optional
______
/ \
/ \ This redundancy can be fixed by methods:
| STOP |
drawEggTop
\ /
\______/ drawEggBottom