1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

Similar presentations

Presentation on theme: "1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good."— Presentation transcript:

1
1 Chapter 2 JAVA FUNDAMENTALS

2
2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); }

3
3 THE PARTS OF A JAVA PROGRAM Comments // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } These are comments. They are ignored by the compiler.

4
4 THE PARTS OF A JAVA PROGRAM Comments Comments are notes of explanation used to document programs, sections of programs, or program statements for the humans who must read programs. Every program should start with a comment that supplies the name of the author of the program and a description of the program. DisplayMessage.java begins with the following comments: // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen

5
5 THE PARTS OF A JAVA PROGRAM Comments The compiler, the program that translates Java programs to byte code, ignores everything from the two forward slashes to the end of the line. Comments do not end with a semicolon.

6
6 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } The compiler ignores blank lines. It is a good idea to use blank lines to make your program easier to read.

7
7 THE PARTS OF A JAVA PROGRAM Class Definition // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } This is the class header for the class named DisplayMessage. This is a class definition.

8
8 THE PARTS OF A JAVA PROGRAM Class Definition Every Java program must have at least one class definition. We will use a class as a container for the statements that comprise our program. Notice that the class named DisplayMessage contains the statements that make up our program. public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); }

9
9 THE PARTS OF A JAVA PROGRAM Class Definition public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } The word public is an access specifier that controls where the class can be accessed from. The key word public in the class header specifies that the class is unrestricted (i.e. the class can be accessed from any other class).

10
10 THE PARTS OF A JAVA PROGRAM Class Definition public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } The key word class marks the beginning of the class. The class name, DisplayMessage, was chosen by the programmer. It is a user-defined identifier. The class header does not end with a semicolon. It is only the beginning of a class definition; it is not a complete statement. No semicolon here! The class header is only the beginning of the class definition.

11
11 THE PARTS OF A JAVA PROGRAM Class Definition public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } A file can contain more that one class, but there cannot be more than one public class in a file. When a Java file contains a public class, the name of the public class and the filename (without the. java extension) must be the same. The source file containing the class shown above must be named DisplayMessage.java

12
12 THE PARTS OF A JAVA PROGRAM Class Definition public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } The { is an opening brace. The } is the closing brace. All the statements in a class definition are enclosed in a set of braces {}. We say that the braces delimit the body of the class. It is considered good programming style to indent the statements inside a set of braces one level.

13
13 THE PARTS OF A JAVA PROGRAM Method Definition // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } This is the method header for the method named main. This is a method definition.

14
14 THE PARTS OF A JAVA PROGRAM Method Definition The method definition shown below is from DisplayMessage.java: public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } A method is a named block of statements that perform a specific task when executed. Every Java application program must have a method called main. Java programs begin executing at the main method.

15
15 THE PARTS OF A JAVA PROGRAM Method Definition The method definition shown below is from DisplayMessage.java: public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } The method header is not a complete statement, so it does not end with a semicolon. The statements in a method must be enclosed in a set of braces {}. Again, indent the statements inside the braces. No semicolon here! The method header is only the beginning of the method definition.

16
16 THE PARTS OF A JAVA PROGRAM The println Method of the out Object of the System Class // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } This statement tells the computer to display the message “Have a good semester!” on a line on the computer screen.

17
17 THE PARTS OF A JAVA PROGRAM The println Method of the out Object of the System Class System.out.println("Have a good semester!"); A sequence of characters between the pair of quotation marks is called a string literal. The quotation marks delimit the string literal, they will not be displayed on the screen when the println method is executed.

18
18 THE PARTS OF A JAVA PROGRAM The println Method of the out Object of the System Class System.out.println("Have a good semester!"); The println statement ends with a semicolon. It is a complete Java statement that instructs the computer to display a message on the computer screen. Notice the semicolon here!

19
19 THE PARTS OF A JAVA PROGRAM The exit Method of the System Class // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } This statement tells the computer to end the execution of the process/program.

20
20 THE PARTS OF A JAVA PROGRAM The exit Method of the System Class System.exit(0); The System classes exit method ends the execution of a process/program. The use of the exit method is optional here. When we display the output of our program in a dialog box, it will be necessary to use this method.

21
21 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Many of the programs we write will display console output. Console output is plain text. The standard output device is a console window. We will send strings of text to a console window using an object from the Java API. The Java API (Application Programmer Interface) is a standard library of prewritten classes available to Java programs for performing common operations.

22
22 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API The program named DisplayMessage.java contains the statement: System.out.println("Have a good semester!"); This statement uses the System class from the Java API. The System class contains objects and methods that perform system level tasks. The out object is a member of the System class that contains the methods named print and println. These methods perform the task of writing strings of characters in a console window on the computer screen.

23
23 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API System.out.println("Have a good semester!"); The beginning of the line above is read System dot out dot print line. The dot is the membership operator in Java. It is used to specify that println is a member method of the out object which is a member of the System class. *** See Figure 2-3 from the text Membership operators

24
24 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API System.out.println("Have a good semester!"); We place the value that is to be displayed on the computer screen inside parentheses to pass it to the println method. A value passed to a method is called an argument. The argument passed to the println method in this example is the string literal "Have a good semester!".

25
25 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API The println method displays a stream of characters and then advances the cursor to the beginning of the next line. The print method displays a stream of characters, but does not advance the cursor to the next line.

26
26 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgA.java on webct

27
27 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgB.java on webct

28
28 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Notice the difference in the output produced by GettysburgA.java and GettysburgB.java. The characters sent to the console are displayed in a continuous stream. A subsequent insertion begins where the previous insertion left off. Even if the output is broken into several print statements, one on each line, the output is displayed as one long continuous stream, unless we specify otherwise. Spaces we want included in the output must be included in the literals we send to the method.

29
29 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgC.java on webct

30
30 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API It is possible to mix the use of the print and println methods to get the desired output.

31
31 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgD.java on webct

32
32 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Escape Sequences Another way to tell the computer to move the cursor to the beginning of a new line is to embed the newline escape sequence in a string literal at the point you want the new line to begin. The newline escape sequence is \n. In general, an escape sequence is a backslash, \, followed by one or more characters. Escape sequences are used to control the way output is displayed.

33
33 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Escape Sequences

34
34 THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgE.java on webct

35
35 VARIABLES A variable is a named location in memory that can store a data value that may be changed as the program executes. Part of the job of programming is determining what values need to be stored. The values stored in variables are often the inputs of the program and the results of calculations/processing.

36
36 VARIABLES In Java, variables must be declared before they are used. You declare a variable by writing a statement called a variable declaration. A variable declaration tells the compiler that storage is needed for a value of a particular data type. A variable declaration consists of the data type of the variable (the kind of data it can store) followed by the name of the variable. A variable declaration ends with a semicolon.

37
37 VARIABLES The statement below declares a variable named radius that can be used to store a double value. Variables of type double can store real numbers (numbers that may include a decimal point and a fractional portion). double radius; The declaration begins with the data type. The key word double is a data type for real numbers.

38
38 VARIABLES *** See the program AreaOf2Circles.java on webct

39
39 VARIABLES You may declare several variables of the same data type in a single statement by separating the variable names with commas.

41
41 VARIABLES In AreaOf2Circles.java we have the declarations: double radius; double area; These statements could be replaced with the following statement: double radius, area;

42
42 VARIABLES Values may be assigned to variables using an assignment statement. An assignment copies the value of the expression on the right of the assignment operator into the location corresponding to the variable that is on the left of the assignment operator.

43
43 VARIABLES Given the declaration: double radius; We can assign the value 3.1 to the variable named radius with the following statement: radius = 3.1; Note: The variable must be on the left side of the assignment operator. 3.1 = radius; is erroneous.

44
44 VARIABLES When a numeric variable is used in an arithmetic expression, the value stored in the memory location corresponding to the variable is used in the calculation.

45
45 VARIABLES The statement below is from AreaOf2Circles.java. When this statement is executed is multiplied by the value of radius and then the result of this calculation is multiplied by the value of radius and finally that result is stored in the area variable. area = * radius * radius;

46
46 VARIABLES When you send the name of a variable to the print or println method the contents of the variable are displayed.

47
47 VARIABLES The following statement from AreaOf2Circles.java displays the value in the area variable: System.out.println(area);

48
48 VARIABLES When using a variable, be sure not to enclose the name of the variable in quotation marks. Remember, a sequence of characters enclosed in quotation marks is a string literal, not the name of a variable.

49
49 VARIABLES The statement: area = * radius * radius; From AreaOf2Circles.java, could not be written as: area = * "radius" * "radius"; // This is a syntax error The "radius" is a string literal. It is not legal to perform a mathematical operation on a string.

50
50 THE STRING CONCATENATION OPERATOR (+) The + symbol represents two operations: –The addition operation on numeric values –The concatenation operation when at least one of its operands is a string The concatenation operator creates a new string containing the characters representing both of its operands.

51
51 THE STRING CONCATENATION OPERATOR (+) In the statement below, the concatenation operator creates the new string "Hello World" which is then passed as the argument to the println method for display in a console window. System.out.println("Hello " + "World"); Note: A space character was placed inside the quotation marks after the word Hello. If this space was not there the string created by the concatenation operator would be “HelloWorld”.

52
52 THE STRING CONCATENATION OPERATOR (+) If only one of the operands of the concatenation operator is a string, the other operand is converted to a string and then the two strings are concatenated.

53
53 THE STRING CONCATENATION OPERATOR (+) For example, the following two statements are from the program AreaOf2Circles.java: System.out.print("The area of the first circle is "); System.out.println(area); These two statements could be replaced with the single statement: System.out.println("The area of the first circle is " + area); The resulting output is the same.

54
54 THE STRING CONCATENATION OPERATOR (+) In this statement, the numeric value stored in the variable area is converted to a string and this string is concatenated to the string "The area of the first circle is ". If the value stored in area is , the new string created is "The area of the first circle is ". This string is the argument sent to the println method for display. System.out.println("The area of the first circle is " + area);

55
55 THE STRING CONCATENATION OPERATOR (+) In Java, a string literal cannot extend over a line. The following statement will result in an error: System.out.println("Four score and seven years ago our fathers brought forth on this continent, a new nation,\nconceived in liberty, and dedicated to the proposition that all men are created equal."); It is an error to extend a string literal across a line.

56
56 THE STRING CONCATENATION OPERATOR (+) We can use the concatenation operator to combine a number of smaller string literals into a single string. System.out.println("Four score and seven years ago our fathers brought forth " + "on this continent, a new nation,\n" + "conceived in liberty, and dedicated to the proposition that" + " all men are created equal."); Notice the style used when extending a statement over a line. Indent the second and subsequent lines.

57
57 LITERALS A literal is a constant value written in the code of a program. Literals are frequently used as: –The values assigned to variables –The operands in a arithmetic expressions –Display values

58
58 LITERALS Highlighted below are some of the literals used in the program AreaOf2Circles.java: radius = 2; area = * radius * radius; System.out.print("The area of the first circle is ");

59
59 IDENTIFIERS An identifier is a programmer-defined name that corresponds to some element of a program. In this class the elements we will give names include: –Variables and named constants –The class containing our application program –Methods

60
60 IDENTIFIERS Rules for Naming Identifiers

61
61 IDENTIFIERS Conventions for Naming Identifiers It is good programming style to give your identifiers meaningful names. This means giving them names that give an indication of their usage.

62
62 IDENTIFIERS Conventions for Naming Variables Write variable names using lowercase letters, capitalizing the first letter of the second and subsequent words that make up the variable name. This helps to visually break up the words since spaces cannot be used. Example: double accountBalance, interestRate;

63
63 IDENTIFIERS Conventions for Naming Classes It is standard practice to begin a class name with an uppercase letter and capitalize the first letter of subsequent words that make up the class name. For example, notice the name of the class in the class header from the application program that calculates and displays the area of two circles: public class AreaOf2Circles This is the name of the class that is being defined.