© 2006 pearson education 1 obj: cont 1.3 and 1.4, to become familiar with identifiers and to...
TRANSCRIPT
© 2006 Pearson Education11
Obj:Obj: cont 1.3 and 1.4, to become familiar cont 1.3 and 1.4, to become familiar with identifiers and to understand how with identifiers and to understand how programming languages workprogramming languages work
HW:HW: p.51 #1.8 – 1.9 (Short Answers) p.51 #1.8 – 1.9 (Short Answers)
Chapter 1 Test in two class daysChapter 1 Test in two class days
Do Now:Do Now:
How is the program on p.33 different from How is the program on p.33 different from the program on p.27? the program on p.27?
C1 D6C1 D6
© 2006 Pearson Education22
IdentifiersIdentifiers
IdentifiersIdentifiers are the words a programmer are the words a programmer uses in a programuses in a program
Some identifiers are already definedSome identifiers are already defined Ex: class, public, static, void, main, println, Ex: class, public, static, void, main, println,
String,…String,…
Some are made up by the programmer:Some are made up by the programmer: Ex: LincolnEx: Lincoln
© 2006 Pearson Education33
An identifier can be made up of letters, An identifier can be made up of letters, digits, the underscore character ( _ ), and digits, the underscore character ( _ ), and the dollar signthe dollar sign
Identifiers cannot begin with a digitIdentifiers cannot begin with a digit
Java is Java is case sensitivecase sensitive - - Total, total, Total, total, andand TOTAL TOTAL are different identifiersare different identifiers
By convention, Java programmers use By convention, Java programmers use different case styles for different types of different case styles for different types of identifiers, such asidentifiers, such as title case title case for class names - for class names - LincolnLincoln
upper caseupper case for constants - for constants - MAXIMUMMAXIMUM
© 2006 Pearson Education44
IdentifiersIdentifiers
Sometimes we choose identifiers Sometimes we choose identifiers ourselves when writing a program (such ourselves when writing a program (such as as LincolnLincoln))
Sometimes we are using another Sometimes we are using another programmer's code, so we use the programmer's code, so we use the identifiers that they chose (such as identifiers that they chose (such as printlnprintln))
© 2006 Pearson Education55
Often we use special identifiers called Often we use special identifiers called reserved wordsreserved words that already have a that already have a predefined meaning in the languagepredefined meaning in the language
A reserved word cannot be used in any A reserved word cannot be used in any other wayother way
© 2006 Pearson Education66
Reserved WordsReserved Words The Java reserved words are listed in your The Java reserved words are listed in your
text on p.31text on p.31 You do not need to You do not need to memorizememorize this list, but this list, but
you should know whether or not a word is you should know whether or not a word is on this list.on this list.
For example, you should know that you For example, you should know that you cannot use the word “while” other than for cannot use the word “while” other than for it’s pre-defined use.it’s pre-defined use.
© 2006 Pearson Education77
White SpaceWhite Space
Spaces, blank lines, and tabs are called Spaces, blank lines, and tabs are called white spacewhite space
White space is used to separate words White space is used to separate words and symbols in a programand symbols in a program
Extra white space is ignoredExtra white space is ignored
A valid Java program can be formatted in A valid Java program can be formatted in many waysmany ways
© 2006 Pearson Education88
Programs should be formatted to enhance Programs should be formatted to enhance readability, using consistent indentationreadability, using consistent indentation
SeeSee Lincoln2.java (page 33) (page 33)
SeeSee Lincoln3.java (page 34) (page 34) These are both the same exact program These are both the same exact program
as on p.27, but they are more difficult for a as on p.27, but they are more difficult for a human to read.human to read.
© 2006 Pearson Education99
Language LevelsLanguage Levels There are four programming language levels:There are four programming language levels:
machine languagemachine language assembly languageassembly language high-level language (ex: Java, understandable by high-level language (ex: Java, understandable by
programmers)programmers) fourth-generation language (doesn’t require fourth-generation language (doesn’t require
knowledge of a programming language)knowledge of a programming language)
See p.36See p.36
© 2006 Pearson Education1010
Each type of CPU has its own specific Each type of CPU has its own specific machine languagemachine language
The other levels were created to make it The other levels were created to make it easier for a human being to read and write easier for a human being to read and write programsprograms
© 2006 Pearson Education1111
Programming LanguagesProgramming Languages
A program must be translated into A program must be translated into machine language before it can be machine language before it can be executed on a particular type of CPUexecuted on a particular type of CPU
This can be accomplished in several waysThis can be accomplished in several ways
A A compilercompiler is a software tool which is a software tool which translates translates source codesource code into a specific into a specific target languagetarget language
© 2006 Pearson Education1212
JCreator, our editor, is actually an IDE JCreator, our editor, is actually an IDE ((Integrated Development EnvironmentIntegrated Development Environment))
It combines an editor, a compiler, and It combines an editor, a compiler, and other Java support tools.other Java support tools.
Other examples of IDEs are NetBeans and Other examples of IDEs are NetBeans and Eclipse. Eclipse.
© 2006 Pearson Education1313
Java TranslationJava Translation
Java sourcecode
Machinecode
Javabytecode
Javainterpreter
Bytecodecompiler
JavaCompiler
(ex: JCreator)
© 2006 Pearson Education1414
Syntax and SemanticsSyntax and Semantics
The The syntax rulessyntax rules of a language define how of a language define how we can put together symbols, reserved we can put together symbols, reserved words, and identifiers to make a valid words, and identifiers to make a valid programprogram
The The semanticssemantics of a program statement of a program statement define what that statement means (its define what that statement means (its purpose or role in a program)purpose or role in a program)
© 2006 Pearson Education1515
A program that is syntactically correct is A program that is syntactically correct is not necessarily logically (semantically) not necessarily logically (semantically) correctcorrect
A program will always do what we tell it to A program will always do what we tell it to do, not what we do, not what we meantmeant to tell it to do to tell it to do
© 2006 Pearson Education1616
ErrorsErrors
A program can have three types of errorsA program can have three types of errors
The compiler will find syntax errors and The compiler will find syntax errors and other basic problems (other basic problems (compile-time errorscompile-time errors))
If compile-time errors exist, an executable If compile-time errors exist, an executable version of the program is not createdversion of the program is not created
© 2006 Pearson Education1717
A problem can occur during program A problem can occur during program execution, such as trying to divide by zero, execution, such as trying to divide by zero, which causes a program to terminate which causes a program to terminate abnormally (abnormally (run-time errorsrun-time errors))
A program may run, but produce incorrect A program may run, but produce incorrect results, perhaps using an incorrect formula results, perhaps using an incorrect formula ((logical errorslogical errors) )
© 2006 Pearson Education1818
Basic Program DevelopmentBasic Program Development
errors
errors
Edit andsave program
Compile program
Execute program andevaluate results
© 2006 Pearson Education1919
If time:If time:
Do p.52 #1.1Do p.52 #1.1