Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 is ignored A valid Java program can be formatted many different ways Programs should be formatted to enhance readability, using consistent indentation

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

3 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 TotalJava is case sensitive, therefore Total and total are different identifiers

4 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

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

6 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

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

8 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

9 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

10 Object-Oriented Programming A class represents a concept and an object represents the realization of that concept Car Class Objects

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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,...

19 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

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

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

22 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

23 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

24 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

25 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

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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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


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

Similar presentations


Ads by Google