Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.

Similar presentations


Presentation on theme: "Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used."— Presentation transcript:

1 Chapter 3 Introducing Java

2 Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used programming language. 3. Understand sequential programming constructs. 4. Identify a class declaration. 5. Demonstrate writing, compiling, and executing a Java application. 6. Explain the function of a virtual machine. 7. Identify and correct syntax errors. 8. Format program output. 9. Display program output using the appropriate programming construct for console output (System.out.print/println). 10. Annotate code properly with comments and indentation. 11. Write statements that demonstrate sequential control. 12. Write class declarations using appropriate code conventions. 13. Demonstrate algorithm design as a problem-solving strategy. 14. Use visual organizers to design solutions.

3 Why Program in Java? Object-Oriented language – ???????? Platform independent applications – able to run on more than one operating system.

4 Objects, Classes, & Packages Object –consists of related data and the instructions for performing actions on the data. Class – Design for an object. The class defines the type of data and actions that will be associated with the object. Student class Name Age ID Bob 16 12345 Sam 17 76543

5 Package & Library Package “Library” – is a group of related classes. Example:

6 Application It contains a controlling class and can contain other classes as well. Usually contains a main method.

7 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

8 Java Application Statements – set of instructions ending in semi-colon. Main Method – in the controlling class and it’s statements are automatically run. Comments – information about a program. Has no affect on the program. Three types of comments: –Single Line – // - for one line of code –Multi-line – /*..*/ used for multiple lines of code. –Documentation – /**…*/ - describes a class or method used in an HTML document.

9 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

10 Executing a Java Application Execute or Run – operate the program. Source code – Program typed by the programmer. (.java) Compile – Translating source code to code that the machine can understand. Bytecode – code from the compiler “machine code”. (.class) Interpreter – runs each bytecode instruction as it is read. Syntax Error – Statements that violates the rules of Java.

11 Displaying Output Output stream – sends data to an output device. Output stream – sends data to an output device. Java provides the “System” class with methods to display output. Java provides the “System” class with methods to display output. System class methods System class methods print() method – displays data and leaves the insertion point at the end of the output. 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 println() method – moves the insertion point to the next line

12 Example public class Ex { public static void main(String[] args) {System.out.print(“Hello”);System.out.print(“World!”); System.out.println(“I’m a”); System.out.println(“Programmer”);}} Output: Hello World! I’m a Programmer Programmer

13 argument Is the data passed to a method for processing. Arguments can be found in side the () of a method. –methodName(argument); String Literals – is a set of characters, which are enclosed by quotation marks.

14 Escape Sequences An escape sequence is a backslash ( \ ) followed by a symbol that together represent a character. Found inside double quotes “ ”. Commonly used escape sequences: \n newline \t tab (8 spaces) \\ backslash \" double quotation mark Example: System.out.println(“Hello \t World”); Output: HelloWorld

15 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

16 Format string specifier %[alignment][width]s % - indicates the start of a specifier [alignment] – “+/-” – minus for left alignment. Leave empty for right alignment. [width] – the number of characters to use for output. s – indicates that the corresponding argument is a string.

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

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

19 Algorithm Design Algorithm – is a set of steps that outline how to solve a problem. Two types of Algorithms: Pseudocode – is a mix of English and program code Flowcharts – use symbols and text to give a visual representation of a solution.

20 Flowchart Symbols input/output start/end

21 The Triangle Flowchart


Download ppt "Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used."

Similar presentations


Ads by Google