Chapter 2 JAVA FUNDAMENTALS

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
Simplifications of Context-Free Grammars
Variations of the Turing Machine
AP STUDY SESSION 2.
1 WORKING WITH 2007 WORD Part 1 Developed October 2007 with lots of help from.
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
David Burdett May 11, 2004 Package Binding for WS CDL.
Problem Solving & Program Design in C
Create an Application Title 1Y - Youth Chapter 5.
CALENDAR.
Programming Language Concepts
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
The 5S numbers game..
1.
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Briana B. Morrison Adapted from William Collins
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
 Copyright I/O International, 2013 Visit us at: A Feature Within from Item Class User Friendly Maintenance  Copyright.
Chapter 1: Expressions, Equations, & Inequalities
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
Artificial Intelligence
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
: 3 00.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Chapter Three Arithmetic Expressions and Assignment Statements
Types of selection structures
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Clock will move after 1 minute
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
Physics for Scientists & Engineers, 3rd Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Chapter 4 FUGACITY.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CMT Programming Software Applications
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 110 – Intro to Computing - Programming
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
Presentation transcript:

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) System.out.println("Have a good semester!"); System.exit(0); } 2

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

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

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

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

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

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

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

THE PARTS OF A JAVA PROGRAM Class Definition No semicolon here! The class header is only the beginning of the 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. 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); } 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

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

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

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

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

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

Notice the semicolon here! 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! 18

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

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

THE print AND println METHODS OF THE System 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

THE print AND println METHODS OF THE System 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

THE print AND println METHODS OF THE System THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Membership operators 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 23

THE print AND println METHODS OF THE System 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

THE print AND println METHODS OF THE System 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

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

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

THE print AND println METHODS OF THE System 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

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

THE print AND println METHODS OF THE System 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

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

THE print AND println METHODS OF THE System 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

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

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

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

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

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

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

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

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

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

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

Note: The variable must be on the left side of the 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

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

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

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

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

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

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

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

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”. 51

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

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

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 12.56636, the new string created is "The area of the first circle is 12.56636". This string is the argument sent to the println method for display. System.out.println("The area of the first circle is " + area); 54

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

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

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 57

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

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 59

IDENTIFIERS Rules for Naming Identifiers 60

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

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

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