Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.

Similar presentations


Presentation on theme: "Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting."— Presentation transcript:

1 Chapter 3 Introduction To Java

2 OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting Output Style Algorithms

3 Classes and Objects

4 Class Libraries Class Library – is a set of classes that supports the development of programs. The Java standard class Library is a useful set of classes that anyone can use when writing Java programs. Java APIs – Application Programmer Interfaces. Class library made up of several sets of related classes.

5 Packages Group of related classes by one name. Eamples: java.lang – java.util

6 Import Declaration import – keyword used to identify packages and classes that will be used by the program. import java.util.Random; –Gains access to the Random Class import java.util.*; –Gains access to the package that contains class Random

7 java.lang.*; Automatically imported Classes in java.lang package –String –System –Double –Integer –Comparable –Math –Object –Etc.

8 java.util.*; Random ArrayList HashMap HashSet Iterator LinkedList List ListIterator Map Set TreeMap TreeSet

9 Package 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. (Refer to page 60 in the text.)

10 Application Package 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. (Refer to page 60 in the text).

11 A Java Application package firstApplication /** * The Greeting class displays a greeting */ public class Greeting { public static void main(String[] args) { System.out.println("Hello,world!"); } package name comment class declaration method statement

12 Comments Provides information about the program to the reader of the code. Comments have no affect on the program. Allows a reader to understand the logic behind the code. Ambiguous statements Can be used to debug a program

13 Three Types of Comments //  Single line comment /*…*/  Multiline comments /** */  used for documentation. –Javadoc tools copies documentation comments into a separate HTML document to create an instructional manual or external documentation. Used for classes and methods.

14 Executing a Java Application public class Greeting public static void System.out.printl } source code 03 3b 84 01 ff f9 68 05 1abytecode... c cilbup Hello,world!... 48 b3 30 compiler JVM

15 println() & print() Output stream sends data to an output device. System.out class and method to display data to the standard output stream. print() method – displays data and leaves the insertion point at the end of the output. println() method – moves the insertion point to the next line after displaying output.

16 The println Method In the Lincoln program from Chapter 1, we invoked the println method to print a character string The System.out object represents a destination (the monitor screen) to which we can send output System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters)

17 Escape Sequences What if we wanted to print a the quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string System.out.println ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character ( \ ) System.out.println ("I said \" Hello \” to you."); escape characters

18 Escape Sequences An escape sequence is a backslash ( \ ) followed by a symbol that together represent a character. Located in double quotes. Commonly used escape sequences: \n newline (AP EXAM) \t tab (8 spaces) (AP EXAM) \\ backslash (AP EXAM) \" double quotation mark (AP EXAM) \b  backspace \r  carriage return \’  single quote

19 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  NOT ON AP EXAM

20 Code Conventions or Style  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.

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

22 Flowchart Symbols input/output start/end

23 The Triangle Flowchart


Download ppt "Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting."

Similar presentations


Ads by Google