1 / 63

Chapter 2 JAVA FUNDAMENTALS

Chapter 2 JAVA FUNDAMENTALS. 1. 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) {

gaille
Download Presentation

Chapter 2 JAVA FUNDAMENTALS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2JAVA FUNDAMENTALS 1

  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); } } 2

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

  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 4

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

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

  7. THE PARTS OF A JAVA PROGRAMClass 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. 7

  8. THE PARTS OF A JAVA PROGRAMClass 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); } } 8

  9. THE PARTS OF A JAVA PROGRAMClass 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). 9

  10. THE PARTS OF A JAVA PROGRAMClass 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. 10

  11. THE PARTS OF A JAVA PROGRAMClass 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 11

  12. THE PARTS OF A JAVA PROGRAMClass 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. 12

  13. THE PARTS OF A JAVA PROGRAMMethod 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. 13

  14. THE PARTS OF A JAVA PROGRAMMethod 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. 14

  15. THE PARTS OF A JAVA PROGRAMMethod 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. 15

  16. THE PARTS OF A JAVA PROGRAMThe 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. 16

  17. THE PARTS OF A JAVA PROGRAMThe 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. 17

  18. THE PARTS OF A JAVA PROGRAMThe 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! 18

  19. THE PARTS OF A JAVA PROGRAMThe 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. 19

  20. THE PARTS OF A JAVA PROGRAMThe 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. 20

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

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

  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 23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  40. VARIABLES The statement below declares four separate variables of data type int. Integer variables like these can store whole numbers. int payments, month, day, year; The statement above is equivalent to the group of statements below: int payments; int month; int day; int year; 40

  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; 41

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

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

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

  45. VARIABLES The statement below is from AreaOf2Circles.java. When this statement is executed 3.14159 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 = 3.14159 * radius * radius; 45

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

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

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

  49. VARIABLES The statement: area = 3.14159 * radius * radius; From AreaOf2Circles.java, could not be written as: area = 3.14159 * "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. 49

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

More Related