Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 2/3 Introducing Java Chapter Outline I. II. III. Why program in Java? a. Java is an Object Oriented Programming language b. Java is also a platform – can be combined like an operating system c. Java software used to run Java applications d. Can run on all major operating systems Objects, Classes and Packages a. Objects i. Consists of related data and instructions for performing actions on that data ii. Design for an object is a class iii. Many objects can have the same class b. Classes i. Defines the type of data and actions that will be associated w/ an object of that class, but not the actual data for an individual object ii. Required to create objects iii. Can create many objects c. Packages i. Also called a library ii. Contains Groups of related classes iii. Importable – can use existing classes from another package A Java Application – package w/ at least one class that contains a main() method a. Statements i. Set of instructions ii. Semicolon is required to indicate the end of a statement iii. Related statements are enclosed by curly braces { } to begin and end the instructions b. Controlling Class i. Program’s starting point ii. Contains the main() method c. Method i. A named set of statements that perform a single, well defined task ii. Public static void main (String[] args ) defines the main() method iii. main() method is placed in the controlling class iv. statements are automatically run when the program executes d. Comments i. Contained w/in the program ii. Provide information about the program to the reader of the code iii. Have no effect on the program iv. Allows for easier modifications and can decrease the number of mistakes v. 3 types of comments 1. /* */ - used to enclose single or multiline comments 2. // - used for adding a comment to the end of a statement or to create a single line comment 3. /** */ - used for documentation IV. V. VI. Executing a Java Application a. Code types by the programmer is source code b. To execute or run the source code, it must be translated to code the computer understands which is the process of compiling c. Complied Java source code is called bytecode i. Executing means bytecode is interpreted with the Java Virtual Machine ii. Interpreter runs each bytecode instruction as it is read d. Machine Code is comprised of 1’s and 0’s and is different depending on the computer platform e. Programs containing syntax errors will not compile i. Syntax Error occurs in a statement that violates the rules of Java ii. Syntax Errors keep the compiler from generating bytecode Displaying Output a. Output Stream i. Sends data to an output device ii. To display data to the standard output stream, use System.out 1. out contains print() and println() methods b. Print() / Println() i. print() displays data and leaves the insertion point at the end of the output ii. println() moves the insertion point to the next line after displaying output c. Argument i. Data passed to a method for processing ii. print() and println() require arguments d. String i. A set of characters, which are enclosed by quotation marks ii. print() and println() arguments are strings e. Escape Sequence i. A backslash (\) followed by a symbol that together represents a character ii. \n = newline iii. \t = tab (8 spaces) iv. \\ = backslash v. \” = double quotation marks Formatting Output a. format() method can be used in place of the print() or println() methods to control the way output is displayed b. format() method arguments include format string and argument list c. format string contains specifiers that indicate how the corresponding strings in the argument list should be displayed d. format string specifiers i. % - indicates the start of a specifier ii. [alignment] - skip for right alignment 1. Include a minus sign (-) for left alignment iii. [width] – the number of characters to use for output iv. s – indicates that the corresponding argument is a string VII. VIII. Code Conventions a. Set of guidelines for writing an application b. Provide details about commenting, rules for naming methods, classes, packages, and statement formatting c. Makes code easier to read and understand d. Code conventions in this chapter are i. Introductory comment 1. Should include info such as your name, class, date, and brief statement about the program ii. Package names 1. Should begin with lowercase letter & then an uppercase letter should begin each word within the name 2. Should not contain spaces iii. Class names 1. Should be nouns and begin with an uppercase letter and an uppercase letter should begin each word w/in the name 2. Should not contain spaces iv. Comment block 1. Should be included before each class and method 2. Typically not place before the main() method v. Comments (general) 1. Should not reiterate what is clear from the code vi. Statements 1. Should be indented if in a method vii. Open and Closed curly braces 1. Open curly braces { should be placed on the same line as the class or method declaration 2. Closed curly braces } should be on a separate line and aligned with the class or method declaration Algorithm Design a. A set of steps that outline how to solve a problem b. Witten in plain English, pseudocode and or using flowcharts i. Pseudocode is a mixture of English and program code and is useful for refining a plain English algorithm ii. Flowcharts use symbols and text to give a visual representation of a solution c. Step for creating an Algorithm i. Analyzing the problem ii. Use more than one method to ensure accuracy