280 likes | 492 Views
Java Software Solutions. Chapter 1.3 Computer Systems (continued). External Documentation javadoc. The Java SDK (Software Development Kit) includes a means of creating external documentation using a software tool called javadoc . Here is how it works:
E N D
Java Software Solutions Chapter 1.3 Computer Systems (continued)
External Documentationjavadoc • The Java SDK (Software Development Kit) includes a means of creating external documentation using a software tool called javadoc . Here is how it works: • If you place a second asterisk following the /* at the beginning of a comment, the content of the comment can be used to automatically generate external documentation about your program using a tool called javadoc . • Example: /** javadoc will print this comment for use as external documentation. */ (Much more later).
Good Programming Practices • Appendix G of our text presents guidelines for good programming practices and includes specific techniques for documenting programs. • You can assume the reader is computer literate and familiar with Java. • You will be expected to apply the coding guidelines in as presented on my web page and in an appendix in the rear of this textbook as you write your programs.
Identifiers and Reserved Words • Words used when writing programs: identifiers. (algebraic ‘variables?’) • Three categories of identifiers: • Words that we make up (Lincoln and args in program 1.1) • Words that another programmer chose (String, System, out, println, and main). • Often these are words that were chosen by a programmer for code that became part of a Java library; • Now available for use by all Java programmers. • Words that are reserved for specialpurposes in the language (class, public, static, and void). • The designers of the Java language chose these words.
Examples: The Reserved Words • Reserved words are identifiers that have special meaning in a programming language. • In the Lincoln program, they are class, public, static, and void. //******************************************************************** // Lincoln.java Author: Lewis/Loftus // // Demonstrates the basic structure of a Java application. //******************************************************************** public class Lincoln { //----------------------------------------------------------------- // Prints a presidential quote. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one."); }// end main() } // end class Lincoln
Reserved Words • The Java reserved words – these words cannot be used for any other purpose, such as naming a class or method. abstract boolean break byte case catch char class const continue default do double else extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
Rules for Naming Identifiers • An identifier (created by us) can be composed letters, digits, the underscore character ( _ ), and the dollar sign ($), but it cannot begin with a digit. • Identifier = variable = property = data name, etc. • Java is case sensitive, which means that two identifier names that differ only in the case of their letters are considered to be different identifiers. • Total, Total, ToTaL, and TOTAL are all different identifiers. • e.g. Total = 4; • e.g. ToTAL = 6; // entirely different data names!
Conventions for Naming Identifiers • Use a consistent case format: • Titlecase (uppercase for the first letter of each word) is conventional for class names. • Example: class GraduateStudent (no space) • Lowercase is used for the first letter of method names and variable (identifier) names. However, the first letter of each additional word that is part of the identifier is capitalized. • Example: payRate and computeSalary()… • Appendix G presents guidelines for naming identifiers. • Your programs must follow those guidelines
Choosing Identifier Names • Identifier names should be descriptive • Avoid meaningless names as a or x. • Unless the name is actually descriptive, such as using x and y to represent (x, y) coordinates. • Avoid using unnecessarily long names.
White Space • White space: blanks, tabs, and newlinecharacters. • White space is used to separate words and symbols • A programmer can use white space to emphasize parts of the program and to make the program easier to read. • Except when it’s used to separate words, the computer ignores white space. • Allows programmers flexibility in formatting a program.
Programming Languages • Programming languages are often categorized into the following four classifications: • Machine Languages • Assembly Languages • High-Level Languages • Fourth-Generation Languages • In order for a program to run on a computer, it must ultimately be expressed in that computer’s machine language.
From Machine Languageto Assembly Language • Each machine language instruction can accomplish only one simple task. • For example, copy a value into a register or compare two values or add two numbers together…. • Machine language code is expressed as a series of binary digits and is extremely difficult for humans to read and write. • Enter: Assembly Language • These are symbolicversions of machine language statements using mnemonics, short English-like words that represent commands or data. • E.g. Instead of: • 01011010100000000101000000000100 (32 bits) • 5A805004 in hex may be rewritten in Assembler language as A 8,4(5,0) or, A 8,X Add what’s ‘at’ X to register 8 LR 4,5 (load register 4 from register 5)
From Low-level Languages to High-level Languages • Both assembly language and machine language are considered low-level languages. • Generally, each assembly language instruction corresponds to an equivalent machine language instruction. • Today most programmers use high-level languages • A high-level language is expressed in English-like phrases, approaching natural language. • A single high-level language programming statement can accomplish the equivalent of many – perhaps hundreds – of machine language instructions. • Examples: Java, C, C++, COBOL, Pascal, C#
Fourth-Generation Languages • Some programming languages operate at an even higher level. • They might include automatic report generation or interaction with a database. • These languages are called fourth-generation languages, or simply 4GLs, because they followed first three generations of computer programming: machine, assembly, and HLL. • Examples: RPG, SQL, Forth, etc…
Compilers and Interpreters(general) • Initially, you will use an editoras you type a program into a computer and store it in a file. • We use the editor built into the NetBeans IDE. • After editing and saving your program, you attempt to translate it from this high-level code Java code (source language statements) into a form that can be executed, that is ultimately into machine language. • The translation (we call it ‘compilation’) may result in errors, in which case you return to the editor to make changes to the code to fix the problem and then try again. • These errors are called ‘syntax’ errors and they normally represent errors in the usage of the language itself, such as not ending a statement in a semicolon or similar rules.
errors errors Edit and Save Program Translate Program into Executable Form Execute Program and Evaluate Results Editing and Running Your Program • Once the translation occurs successfully, you can execute the program and evaluate the results. • If your results are not what you want (or if you want to enhance your existing program), you again return to the editor to make changes. CompilationExecution In a file using an editor For us: NetBeans IDE
Translation of Source Code • The translation of source code into (ultimately) machine language for execution on a particular type of CPU can occur in a variety of ways. • By using a compiler. • By using an interpreter. • By using both a compiler and interpreter • This is how Java does it.
A Compiler • A compiler is a program that translates code from one language to an equivalent code in another language. • The original code is called source-code. • The language to which it is translated is the target language. • For many traditional compilers, the source code is translated directly into a particular machine language. • In that case, the translation process occurs once and the resulting executable program can be saved and then run whenever needed.
An Interpreter • An interpreter is similar to a compiler but has importantdifferences… • Interpreters interleave translation and execution activities. • A small part of the source code, such as one statement, is translated and executed. • Then another part is translated and executed, etc. • This eliminates the need for a separate compilation phase; however, the program runs more slowly because translation must occur during each execution.
Java Uses Both a Compiler and Interpreter • The Java compiler translates the entire Java source code into something called: Java ‘bytecode.’ (produces a ‘byte-code’ file) • Bytecode is a representation of the program in a low-level code similar (but not the same as) machine language code. • The Java interpreter then reads a short segment of Java bytecode,translatesthat segment into machine language and executes it. • This process is repeated as program executes.
The Advantage of Java Bytecode • Difference between machinelanguage code and bytecode is that Java bytecode is not tied to any particular processor type. Machine Independent! • Most compilers generate machine code that can only be executed on a particular machine /series • This makes Java “architecture neutral” This feature makes Java portable from one machine to another. • All you need is a Java interpreter or bytecode compiler for each processor type on which the bytecode is to be executed. • Web Browsers – java-enabled. That way they can process java code transmitted to them in bytecode formats.
Integrated Development Environments (IDEs) • IDEs combine an editor, compiler, and other (Java) support tools into a single program. • NetBeans contains a number of super nice features including the ability to step through a program one statement at a time and see how values are being generated, set ‘breakpoints,’ and much more. • There are many IDEs including Borland’s JBuilder and IBM’s Eclipse, JGrasp (developed by Auburn), the medEditor, … • Ours is NetBeans 6.8
More on: Syntax and Semantics • Each programming language has its own syntax. • The syntax constitutes the rules of a language that dictate exactly how the vocabulary elements of the language can be combined to form statements. • (Just like grammar rules in English.) • During compilation, all syntax rules are checked. • If a program is not syntactically correct, the compiler gives error messages and won’t produce the bytecode file (that is, the .classfile). • Syntax errors must be eliminated before the good bytecodes are produced that allows us to try to ‘execute’ the program.
Semantics (meaning) • The semantics of a statement in a programming language define what will happen when that statement is executed. • Programming languages are generally unambiguous, which means that there is one and only one interpretation for each statement. • The same exact English sentence can have multiple valid meanings. • A computer language cannot allow such ambiguities to exist.
You will encounter three kinds of errors as you develop programs: Compile-time errors Runtime errors Logical errors Errors
Compile-time and Run-time Errors • An error identified by the compiler is called a compile-time error. (These are syntax errors) • If a compile-time error occurs, an executable version of the program is not created. • Use your editor to correct the error, then recompile your program. • A runtime error causes the program to terminate abnormally during execution. • An example is an attempt to divide by zero. • In Java, many runtime errors are represented as exceptions that can be caught and dealt with on the chapter dealing with Exceptions. • In this instance, your program will ‘bomb.’
Logical Errors • When your program has a logical error, it will compile and execute, but produces incorrect results. (These are called runtime errors too!) • A logical error occurs when a value is calculated incorrectly. • A programmer must test the program thoroughly, comparing the expected results to those that actually occur. • e.g., z=a+b; instead of z=a*b; • The process of finding and correcting defects in a program is called debugging.