Introduction to Computers and Programming in
Download
1 / 29

Introduction - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

Introduction to Computers and Programming in JAVA Section 1 Professor: Sana` Odeh [email protected] Introduction. Introductions to Java Programming basics Introduce a basic Java program We will use Jcreator to create/edit/compile/ debug and execute/run our Java programs.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introduction' - prince


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to Computers and Programming in JAVASection 1Professor: Sana` [email protected]


Introduction
Introduction

  • Introductions to Java Programming basics

    • Introduce a basic Java program

    • We will use Jcreator

    • to create/edit/compile/ debug and execute/run our Java programs


2 2 a first program in java printing a line of text
2.2 A First Program in Java: Printing a Line of Text

  • Application

    • Program that executes using the java interpreter

  • Sample program

    • Show program, then analyze each line


Welcome1 java program output

1 // first Java program called Welcome1.java

2 // Text-printing program.

3

4 public class Welcome1 {

5

6 // main method begins execution of Java application

7 public static void main( String args[] )

8 {

9 System.out.println( "Welcome to Java Programming!" );

10

11 } // end method main

12

13 } // end class Welcome1

Welcome1.javaProgram Output

Welcome to Java Programming!


Lets look at the first and second line of the previous program t his is a comment

1 // first Java program called Welcome1.java

2 // Text-printing program.

Lets look at the first and second line of the previous programThis is a comment

  • Comments start with: //

    • Comments ignored during program execution

    • Improves code readability

    • Document and describe code

  • Traditional comments: /* ... */

    /* This is a traditional comment. It can be split over many lines */

  • Another line of comments

  • Note: line numbers not part of program, added for reference


Lets look at the following lines of the previous program

3

4 public class Welcome1 {

Lets look at the following lines of the previous program

  • Blank line

    • Makes program more readable

    • Blank lines, spaces, and tabs are white-space characters

      • Ignored by compiler

  • Begins class declaration for class Welcome1

    • Public & class are reserved key words and have to be included in every program

  • Reserved Keyword: words reserved for use by Java.

    • class keyword followed by class name ( in this case it is Welcome1) is referred to as an identifier.

    • An identifier is user defined

    • Welcome1 is the name of the program and should be same as the file name Welcome1.java

  • Naming classes: capitalize every word

    • SampleClassName


Reserved words
Reserved Words

  • Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program.

  • For example, when the compiler sees the word class, it understands that the word after class is the name for the class.

  • Other reserved words in Example 1 are public, static, and void. Their use will be introduced later in the book.


Lets look at the following lines of the previous program1

4 public class Welcome1 {

Lets look at the following lines of the previous program

  • Name of class called identifier ( in this case its Welcome1 )

    • Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ )

    • Does not begin with a digit, has no spaces

    • Examples: Welcome1, $value, _value, button7

      • 7button is invalid

    • Java is case sensitive (capitalization matters)

      • a1 and A1 are different

  • For chapters 2 to 7, use public keyword

    • Certain details not important now

    • Mimic certain features, discussions later


2 2 a simple program printing a line of text

4 public class Welcome1 {

7 public static void main( String args[] )

2.2 A Simple Program: Printing a Line of Text

  • Saving files

    • File name must be class name with .java extension

    • Welcome1.java

  • Left brace {

    • Begins body of every class

    • Right brace ends declarations (line 13)

  • Part of every Java application

    • Applications begin executing at main

      • Parenthesis indicate main is a method (ch. 6)

      • Java applications contain one or more methods


Main method
main Method

  • The main method provides the control of program flow.

  • The Java interpreter executes the application by invoking the main method.

  • The main method looks like this:

    public static void main(String[] args) {

    Statements;

    }


Blocks
Blocks

A pair of braces in a program forms a block that groups components of a program.


Statements
Statements

  • A statementrepresents an action or a sequence of actions.

  • The statement System.out.println("Welcome to Java!");

  • in the program in Example 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).


Classes
Classes

  • The class is the essential Java construct.

  • A class is a template or blueprint for objects.

  • To program in Java, you must understand classes and be able to write and use them.

  • The mystery of the class will continue to be unveiled throughout this book.

  • For now, though, understand that a program is defined by using one or more classes.


2 2 a simple program printing a line of text1

7 public static void main( String args[] )

8 {

2.2 A Simple Program: Printing a Line of Text

  • Exactly one method must be called main

  • Methods can perform tasks and return information

    • void means main returns no information

    • For now, mimic main's first line

  • Left brace begins body of method declaration

    • Ended by right brace } (line 11)


  • 2 2 a simple program printing a line of text2

    9 System.out.println( "Welcome to Java Programming!" );

    2.2 A Simple Program: Printing a Line of Text

    • Instructs computer to perform an action

      • Prints string of characters

        • String - series characters inside double quotes

      • White-spaces in strings are not ignored by compiler

    • System.out

      • Standard output object

      • Print to command window (i.e., MS-DOS prompt)

    • Method System.out.println

      • Displays line of text

      • Argument inside parenthesis

    • This line known as a statement

      • Statements must end with semicolon ;


    2 2 a simple program printing a line of text3

    11 } // end method main

    13 } // end class Welcome1

    2.2 A Simple Program: Printing a Line of Text

    • Ends method declaration

    • Ends class declaration

    • Can add comments to keep track of ending braces

    • Lines 8 and 9 could be rewritten as:

    • Remember, compiler ignores comments

    • Comments can start on same line after code


    Compiling a java program
    Compiling a Java program

    • Compiling a program

      • OpenJCreator

        • See website for instructions

        • http://www.cs.nyu.edu/courses/spring09/V22.0002-001/software.htm

      • Create a new project, then a new java file/class and will save it.

      • Type the program and compile it

      • If no errors, Welcome1.class created

        • Has bytecodes that represent application

        • Bytecodes passed to Java interpreter, the Java Virtual Machine (JVM)

      • If no errors, we will run or execute our program to get the output.


    Executing program lets take a look how to do this using jcreator
    Executing program.. Lets take a look how to do this using JCreator

    • Executing a program

      • TypejavaWelcome1

        • Interpreter loads .class file for class Welcome1

        • .class extension omitted from command

      • Interpreter calls method main


    Lets modify our first java program
    Lets Modify Our First Java Program JCreator

    • Modify previous example to print same contents using different code


    Modifying our first java program

    9 JCreator System.out.print( "Welcome to " );

    10 System.out.println( "Java Programming!" );

    Modifying Our First Java Program

    • Modifying programs

      • Welcome2.java produces same output as Welcome1.java (Fig. 2.1)

      • Using different code

      • Line 9 displays “Welcome to ” with cursor remaining on printed line

      • Line 10 displays “Java Programming! ” on same line with cursor on next line


    Methods
    Methods JCreator

    • What is System.out.println() ?

      • It is a method: a collection of statements that performs a sequence of operations to display a message on the console.

    • It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument.

    • The string argument is enclosed within parentheses.

    • In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.


    System.out.print JCreator keeps the cursor on the same line, so System.out.printlncontinues on the same line.

    1 // Fig. 2.3: Welcome2.java

    2 // Printing a line of text with multiple statements.

    3

    4 public class Welcome2 {

    5

    6 // main method begins execution of Java application

    7 public static void main( String args[] )

    8 {

    9 System.out.print( "Welcome to " );

    10 System.out.println( "Java Programming!" );

    11

    12 } // end method main

    13

    14 } // end class Welcome2

    Welcome2.java1. Comments2. Blank line3. Begin class Welcome23.1 Method main4. Method System.out.print4.1 Method System.out.println5. end main,Welcome2

    Welcome to Java Programming!


    2 3 modifying our first java program

    9 JCreator System.out.println( "Welcome\nto\nJava\nProgramming!" );

    2.3 Modifying Our First Java Program

    • Newline characters (\n)

      • Interpreted as “special characters” by methods System.out.print and System.out.println

      • Indicates cursor should be on next line

      • Welcome3.java (Fig. 2.4)

      • Line breaks at \n

    • Usage

      • Can use in System.out.println or System.out.print to create new lines

        • System.out.println("Welcome\nto\nJava\nProgramming!" );


    Welcome3 java 1 main 2 system out println uses n for new line program output

    Notice how a new line is output for each JCreator\n escape sequence.

    1 // Fig. 2.4: Welcome3.java

    2 // Printing multiple lines of text with a single statement.

    3

    4 public class Welcome3 {

    5

    6 // main method begins execution of Java application

    7 public static void main( String args[] )

    8 {

    9 System.out.println( "Welcome\nto\nJava\nProgramming!" );

    10

    11 } // end method main

    12

    13 } // end class Welcome3

    Welcome3.java1. main2. System.out.println (uses \n for new line)Program Output

    Welcome

    to

    Java

    Programming!


    2 3 modifying our first java program1
    2.3 Modifying Our First Java Program JCreator

    Escape characters

    • Backslash ( \ )

    • Indicates special characters be output


    Find the error sample 1
    Find the Error JCreatorSample 1

    1 Printing multiple lines of text with a single statement.

    2

    3

    4 public class Welcome3 {

    5

    6 // main method begins execution of Java application

    7 public static void main( String args[] )

    8 {

    9 System.out.println( "Welcome\nto\nJava\nProgramming!" );

    10

    11 } // end method main

    12

    13 } // end class Welcome3


    Find the error sample 2
    Find the Error JCreatorSample 2

    1 //Printing multiple lines of text with a single statement.

    2

    3

    4 public Welcome3 {

    5

    6 // main method begins execution of Java application

    7 public static void main( String args[] )

    8 {

    9 System.out.println( "Welcome\nto\nJava\nProgramming!" );

    10

    11 } // end method main

    12

    13 } // end class Welcome3


    Find the errors 7
    Find the Errors (7) JCreator

    1 //Printing multiple lines of text with a single statement.

    2

    3

    4 Public class 1firstprogram {

    5

    6 // main method begins execution of Java application

    7 public static void main( String args[]

    8 {

    9 System.out.print( "Welcome\nto\nJava\nProgramming! )

    10

    11 } // end method main

    12

    13 // end class Welcome3


    Anatomy of a java program
    Anatomy of a Java Program JCreator

    • Comments

    • Reserved words

    • Statements

    • Blocks

    • Classes

    • Methods

    • The main method


    ad