white space spaces, blank lines, and tabs are collectively called white space and are used to...

34
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

Upload: william-sherman

Post on 20-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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

Page 2: 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

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 */

Page 3: 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

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

• Java is case sensitive, therefore TotalTotal and total are different identifiers

Page 4: 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

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 instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient true try var void volatile while

Page 5: 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

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."

Page 6: 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

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 does not

• See Countdown.java

Page 7: 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

Errors

• A program can have three types of errors

• The compiler will find problems with syntax and other basic issues (compile-time 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 abnormally (run-time errors)

• A program may run, but produce incorrect results (logical errors)

Page 8: 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

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

Page 9: 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

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

Page 10: 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

Object-Oriented Programming

• A class represents a concept and an object represents the realization of that concept

• Car Class Objects

Page 11: 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

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

Page 12: 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

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 relate in some way

Page 13: 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

The Java API Packages

• Some packages in the Java API: java.applet java.awt

java.beans java.io

java.security java.sql

java.lang java.math

java.net java.rmi

java.text java.util

• java.lang package: free gift

Page 14: 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

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 imported into every Java program

Page 15: 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

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, boolean,

char

Page 16: 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

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 Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 Max Value 127 32,767 2,147,483,647 > 9 x 1018

Page 17: 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

•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

Page 18: 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

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, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ...

Page 19: 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

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

Primitive Type

int

double

char

boolean

Wrapper Class

Integer

Double

Character

Boolean

Page 20: 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

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());

Page 21: 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

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.)

Page 22: 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

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)

Parentheses can be used to force precedence

Appendix D contains a complete operator precedence chart for all Java operators

Page 23: 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

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 it is false, the statement is skipped

This provides basic decision making capabilities

Page 24: 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

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

Page 25: 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

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

statement2;

If the condition is true, statement1 is executed; if the condition is false, statement2 is executed

See Temperature3.java and Right_Triangle.java

Page 26: 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

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 last unmatched if (no matter what the indentation implies)

Page 27: 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

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 statement is executed over and over until the condition becomes false

Page 28: 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

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

Page 29: 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

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 -- always double check that your loops will terminate normally

See Forever.java

Page 30: 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

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 good starting point

Page 31: 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

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 project

Page 32: 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

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

Page 33: 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

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 documentation

Page 34: 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

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