Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Classes and Objects

Similar presentations


Presentation on theme: "Chapter 3 Classes and Objects"— Presentation transcript:

1 Chapter 3 Classes and Objects
11/29/2018 5:59 AM Chapter 3 Classes and Objects Refer to page 60 in the text. Emphasize that a class is similar to a blueprint. The class defines how each object created from the “blueprint” will look and behave, but a class is not the object itself. © 2007 Lawrenceville Press

2 Chapter 3 Package Refer to page 60 in the text.
11/29/2018 5:59 AM Chapter 3 Package Refer to page 60 in the text. A package contains related classes. It is convenient for a programmer when a single package contains multiple classes for use in an application. Packages allow code (classes) to be reused over and over again. © 2007 Lawrenceville Press

3 Chapter 3 Application Package
11/29/2018 5:59 AM Chapter 3 Application Package Refer to page 60 in the text. An application is contained in a package. A package must contain a controlling class (with a main() method) in order to be an application. The application package can contain additional classes as well. © 2007 Lawrenceville Press

4 Chapter 3 A Java Application
11/29/2018 5:59 AM Chapter 3 A Java Application package name package firstApplication /** * The Greeting class displays a greeting */ public class Greeting { public static void main(String[] args) { System.out.println("Hello,world!"); } comment class declaration method statement Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains five (5) animations. Refer to page 61 in the text. <press space bar> A package name is declared with a package statement. <press space bar> The comment describes the purpose of the application. <press space bar> The class declaration creates a new class named Greeting. <press space bar> The main() method indicates that the class is a controlling class. The main() method will be run automatically when the application is executed. <press space bar> Statements are instructions. The println() statement will display output to the screen when the application is run. © 2007 Lawrenceville Press

5 Chapter 3 Executing a Java Application
11/29/2018 5:59 AM Chapter 3 Executing a Java Application public class Greeting public static void System.out.printl } source code 03 3b ff f9 68 05 1a bytecode compiler JVM ...c cilbup ...48 b3 30 Hello,world! Refer to page 62 in the text. In order to execute a Java application, the source code must be compiled to code that the computer understands. In Java, the source code is converted to bytecode with a compiler. Next, the bytecode is interpreted by the Java Virtual Machine (JVM) to execute the application and produce output. Note that the bytecode, not source code, is what must be run from a computer that has a JVM. © 2007 Lawrenceville Press

6 Chapter 3 Escape Sequences
11/29/2018 5:59 AM Chapter 3 Escape Sequences An escape sequence is a backslash (\) followed by a symbol that together represent a character. Commonly used escape sequences: \n newline \t tab (8 spaces) \\ backslash \" double quotation mark Refer to pages 63 in the text. An escape sequence is a backslash (\) followed by a symbol that together represent a character. © 2007 Lawrenceville Press

7 Chapter 3 The format() Method
11/29/2018 5:59 AM Chapter 3 The format() Method A method in the System class Used to control the way output is displayed Requires a format string and an argument list The format string specifier takes the form: %[alignment][width]s For example System.out.format("%-6s %4s", "Test1", "90"); displays: Test1 90 Refer to page 64 in the text. The format() method can be used in place of the print() or println() methods to control the way output is displayed. © 2007 Lawrenceville Press

8 Chapter 3 Code Conventions
11/29/2018 5:59 AM Chapter 3 Code Conventions An introductory comment should begin a program. Package names should begin with a lowercase letter and then an uppercase letter should begin each word within the name. Class names should be nouns and begin with an uppercase letter and an uppercase letter should begin each word within the name. A comment block should be included before each class. Refer to page 65 in the text. Code conventions are a set of guidelines for writing an application. Code conventions can make modifying and maintaining code faster, easier, and less expensive. © 2007 Lawrenceville Press

9 Chapter 3 Code Conventions (con't)
11/29/2018 5:59 AM Chapter 3 Code Conventions (con't) Comments should not reiterate what is clear from the code. Statements in a method should be indented. An open curly brace ({) should be placed on the same line as the class or method declaration, and the closing curly brace (}) should be on a separate line and aligned with the class or method declaration. Refer to page 65 in the text. © 2007 Lawrenceville Press

10 Chapter 3 Flowchart Symbols
11/29/2018 5:59 AM Chapter 3 Flowchart Symbols start/end input/output Refer to pages 66 and 67 in the text. The oval flowchart symbol indicates start or end. The parallelogram flowchart symbol indicates input or output. © 2007 Lawrenceville Press

11 Chapter 3 The Triangle Flowchart
11/29/2018 5:59 AM Chapter 3 The Triangle Flowchart Refer to page 66 in the text. The Triangle flowchart illustrates the application solution. Solution steps include: 1. displaying one asterisk 2. displaying two asterisks on the next line 3. displaying asterisk space asterisk on the next line 4. displaying asterisk asterisk asterisk asterisk on the last line © 2007 Lawrenceville Press


Download ppt "Chapter 3 Classes and Objects"

Similar presentations


Ads by Google