Section 1.8 Chapter Summary
Subsection 1.8.1 Technical Terms
application program | assignment statement | comment |
compound statement (block) | data type | declaration statement |
default constructor | executable statement | expression |
identifier | literal value | object instantiation |
operator | package | parameter |
primitive data type | pseudocode | qualified name |
semantics | statement | stepwise refinement |
syntax |
Subsection 1.8.2 Summary of Important Points
Good program design requires that each object and method have a well-defined role and clear definition of what information is needed for the task and what results will be produced.
Good program design is important; the sooner you start coding, the longer the program will take to finish. Good program design strives for readability, clarity, and flexibility.
Testing a program is very important and must be done with care, but it can only reveal the presence of bugs, not their absence.
An algorithm is a step-by-step process that solves some problem. Algorithms are often described in pseudocode, a hybrid language that combines English and programming language constructs.
A syntax error occurs when a statement breaks a Java syntax rules. Syntax errors are detected by the compiler. A semantic error is an error in the program's design and cannot be detected by the compiler.
Writing Java code should follow the stepwise refinement process.
Double slashes (//) are used to make a single-line comment. Comments that extend over several lines must begin with /* and end with */.
An identifier must begin with a letter of the alphabet and may consist of any number of letters, digits, and the special characters _ and $. An identifier cannot be identical to a Java keyword. Identifiers are case sensitive.
A keyword is a term that has special meaning in the Java language (TableĀ 1.5.5).
Examples of Java's primitive data types include the
int
,boolean
, anddouble
types.A variable is a named storage location. In Java, a variable must be declared before it can be used.
A literal value is an actual value of some type, such as a
String
("Hello") or anint
(5).-
A declaration statement has the form:
Type VariableName ;
-
An assignment statement has the form:
VariableName = Expression ;
When it is executed it determines the value of the Expression on the right of the assignment operator (\(=\)) and stores the value in the variable named on the left.
Java's operators are type dependent, where the type is dependent on the data being manipulated. When adding two
int
values (7 + 8
), the \(+\) operation produces anint
result.A class definition has two parts: a class header and a class body. A class header takes the form of optional modifiers followed by the word
class
followed by an identifier naming the class followed, optionally, by the keywordextends
and the name of the class's superclass.There are generally two kinds of elements declared and defined in the class body: variables and methods.
Object instantiation is the process of creating an instance of a class using the
new
operator in conjunction with one of the class's constructors.Dot notation takes the form qualifiers.elementName. The expression
System.out.print("hello")
uses Java dot notation to invoke theprint()
method of theSystem.out
object.A Java application program runs in stand-alone mode.
A Java source program must be stored in a file that has a
.java
extension. A Java bytecode file has the same name as the source file but a.class
extension. It is an error in Java if the name of the source file is not identical to the name of the public Java class defined within the file.Java programs are first compiled into bytecode and then interpreted by the Java Virtual Machine (JVM).
Subsection 1.8.3 Solutions to Self-Study Exercises
The value 12 is stored in
num
. int num2 = 711 + 712;int num2 = 711 + 712;
The definition of the
OldMacDonald
class is:public class OldMacDonald { public static void main(String args[]) // Main method { System.out.println("Old MacDonald had a farm"); System.out.println("E I E I O."); System.out.println("And on his farm he had a duck."); System.out.println("E I E I O."); System.out.println("With a quack quack here."); System.out.println("And a quack quack there."); System.out.println("Here a quack, there a quack,"); System.out.println("Everywhere a quack quack."); System.out.println("Old MacDonald had a farm"); System.out.println("E I E I O."); System.out.println("Old MacDonald had a farm"); System.out.println("E I E I O."); System.out.println("And on his farm he had a pig."); System.out.println("E I E I O."); System.out.println("With an oink oink here."); System.out.println("And an oink oink there."); System.out.println("Here an oink, there an oink,"); System.out.println("Everywhere an oink oink."); System.out.println("Old MacDonald had a farm"); System.out.println("E I E I O."); } // End of main } // End of OldMacDonald
The definition of the
Pattern
class is:public class Pattern { public static void main(String args[])// Main method { System.out.println("**********"); System.out.println("* ** ** *"); System.out.println("* ** *"); System.out.println("* * * *"); System.out.println("* **** *"); System.out.println("**********"); } // End of main } // End of Pattern