Download White Space

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
White Space
• Spaces, blank lines, and tabs are
collectively called white space and are used
to separate words and symbols in a program
• Extra white space is ignored
• A valid Java program can be formatted
many different ways
• Programs should be formatted to enhance
readability, using consistent indentation
Comments
• Comments in a program are also called
inline documentation
• They should be included to explain the
purpose of the program and describe
processing steps
• Java comments can take two forms:
– // comment runs to the end of the line
– /* comment runs to terminating symbol, even
across line breaks */
Identifiers
• Identifiers are the words a programmer uses
in a program
• Most identifiers have no predefined
meaning except as specified by the
programmer
• An identifier can be made up of letters,
digits, the underscore character (_), and the
dollar sign
• They cannot begin with a digit
Reserved Words
• Some identifiers, called reserved words,
have specific meanings in Java and cannot
be used in other ways
• abstract boolean break byte
byvalue case cast catch char
class const continue default do
double else extends false final
finally float for future generic
goto if implements import inner
Literals
• A literal is an explicit data value used in a
program
• Integer literals: 25 69 -4288
• Floating point literals: 3.14159 42.075 0.5
• String literals: "The result is: " "To
thine own self be true."
The Java API
• The Java Application Programmer Interface
(API) is a collection of classes that can be
used as needed
• The println and print methods are part of the
Java API; they are not part of the Java
language itself
• Both methods print information to the
screen; the difference is that println
moves to the next line when done, but print
Errors
• A program can have three types of errors
• The compiler will find problems with
syntax and other basic issues (compiletime errors)
• If compile-time errors exist, an executable
version of the program is not created
• A problem can occur during program
execution, such as trying to divide by
zero, which causes a program to terminate
Command Line Arguments
• See Name_Tag.java
• The main method accepts extra information
on the command line when a program is
executed
• > java Name_Tag John
• Each extra value is called command line
argument
• In Java, command line arguments are
always read as a list of character strings
Object-Oriented Programming
• Java is object-oriented language
• Programs are made from software
components called objects
• An object contains data and methods
• An object is defined by a class
• Multiple objects can be created from the
same class
Object-Oriented Programming
• A class represents a concept and an object
represents the realization of that concept
• Car Class Objects
Object-Oriented Programming
• Objects can also be derived from each other
using a process called inheritance
• Objects, classes, and inheritance will be
discussed in greater detail later
Class Libraries
• The Java API is a class library, a group of
classes that support program
development Classes in a class hierarchy
are often related by inheritance
• The classes in the Java API is separated into
packages
• The System class, for example, is in
package java.lang
• Each package contains a set of classes that
The Java API Packages
• Some packages in the Java API:
java.applet
java.beans
java.security
java.lang
java.net
java.text
java.awt
java.io
java.sql
java.math
java.rmi
java.util
• java.lang package: free gift
Importing Packages
• Using a class from the Java API can be
accomplished by
–
using its fully qualified name:
java.lang.System.out.println ();
–
Or, the package can be imported using an
import statement, which has two forms:
–
import java.applet.*;
–
import java.util.Random;
• The java.lang package is automatically
Primitive Data Types
• A data type is defined by a set of values and
the operators you can perform on them
• Each value stored in memory is associated
with a particular data type
• The Java language has several predefined
types, called primitive data types. The
following reserved words represent eight
different primitive types:
– byte, short, int, long, float, double,
Integers
• There are four separate integer primitive
data types
They differ by the amount of memory
used to store them
Type
byte
short
int
long
•Boolean
•
A boolean value represents a true or false
condition
•
They can also be used to represent any
two states, such as a
light bulb being on or off
•
The reserved words true and false are the
only valid values
for a boolean type
Characters
• The ASCII character set is still the basis for
many other programming languages
• ASCII is a subset of Unicode, including:
– uppercase letters
– lowercase letters
– punctuation
digits
special symbols
control characters
A, B, C, …
Wrappers
• For each primitive data type there is a
corresponding wrapper class.
• For example:
Wrapper classes are useful in situations
where you need an object instead of a
primitive type
They also contain some useful methods
Numeric Input
• Converting a string that holds an integer into
the integer value can be done with a method in
the Integer wrapper class:
value=Integer.parseInt(my_string);
• A value can be read and converted in one line:
num=
Integer.parseInt (stdin.readLine());
Expressions
• An expression is a combination of
operators and operands
– The arithmetic operators include addition (+),
subtraction (-),multiplication (*), and division
(/)
– Operands can be literal values, variables, or
other sources of data
– The programmer determines what is done with
the result of an expression (stored, printed, etc.)
Operator Precedence
• The order in which operands are evaluated
in an expression
is determined by a well-defined
precedence hierarchy
Operators at the same level of
precedence are evaluated
according to their associativity (right to
left or left to right)
The if Statement
•
The Java if statement has the following
syntax:
if (condition)
statement;
If the boolean condition is true, the
statement is executed; if
Block Statements
• Several statements can be grouped together
into a block statement
– Blocks are delimited by braces
– A block statement can be used wherever a
statement is called for in the Java syntax
The if-else Statement
• An else clause can be added to an if
statement to make it an
if-else statement:
if (condition)
statement1;
else
Nested if Statements
• The body of an if statement or else clause
can be another if
statement
These are called nested if statements
See Football_Choice.java
Note: an else clause is matched to the
The while Statement
• A while statement has the following syntax:
while (condition)
statement;
If the condition is true, the statement is
executed; then the
condition is evaluated again
The while Statement
• If the condition of a while statement is false
initially, the statement is never executed
Therefore, we say that a while statement
executes zero or more times
Infinite Loops
• The body of a while loop must eventually
make the condition
false
If not, it is an infinite loop, which will
execute until the user
interrupts the program
This is a common type of logical error --
Program Development
• The creation of software involves four basic
activities:
–
–
–
–
establishing the requirements
creating a design
implementing the code
testing the implementation
• The development process is much more
involved that this, but these basic steps are a
Requirements
• Requirements specify the tasks a program
must accomplish (what to do, not how to do
it)
– They often address the user interface
– An initial set of requirements are often
provided, but usually must be critiqued,
modified, and expanded
– It is often difficult to establish detailed,
unambiguous, complete requirements
– Careful attention to the requirements can save
significant time and money in the overall
Design
• A program follows an algorithm, which is a
step-by-step process for solving a problem
• The design specifies the algorithms and data
needed
• In object-oriented development, it
establishes the classes, objects, and methods
that are required
• The details of a method may be expressed
in pseudo-code, which is code-like, but does
not necessarily follow any specific syntax
Implementation
• Implementation is the process of translating
a design into source code
• Most novice programmers think that writing
code is the heart of software development,
but it actually should be the least creative
• Almost all important decisions are made
during requirements analysis and design
• Implementation should focus on coding
details, including style guidelines and
Testing
• A program should be executed multiple
times with various input in an attempt to
find errors
• Debugging is the process of discovering the
cause of a problem and fixing it
• Programmers often erroneously think that
there is "only one more bug" to fix
• Tests should focus on design details as well
as overall requirements