Introduction to Computers and Programming in JAVA Section 1 Professor: Sana` Odeh [email protected]  2003 Prentice Hall, Inc.

Download Report

Transcript Introduction to Computers and Programming in JAVA Section 1 Professor: Sana` Odeh [email protected]  2003 Prentice Hall, Inc.

1
Introduction to Computers and
Programming in JAVA
Section 1
Professor: Sana` Odeh
[email protected]
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
2
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
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
3
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
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
4
1
2
3
4
5
6
Outline
// first Java program called Welcome1.java
// Text-printing program.
Welcome1.java
public class Welcome1 {
// main method begins execution of Java application
7
public static void main(
8
9
10
11
12
13
{
String args[]
)
System.out.println( "Welcome to Java Programming!" );
} // end method main
} // end class Welcome1
Welcome to Java Programming!
Program Output
 2003 Prentice Hall, Inc.
All rights reserved.
5
Lets look at the first and second line of the previous program
This is a comment
1
// first Java program called Welcome1.java
– 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 */
2
// Text-printing program.
– Another line of comments
– Note: line numbers not part of program, added for reference
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
6
Lets look at the following lines of the
previous program
3
– Blank line
• Makes program more readable
• Blank lines, spaces, and tabs are white-space characters
– Ignored by compiler
– Begins class declaration for class Welcome1
4
public 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
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
7
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.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
8
Lets look at the following lines of the
previous program
4
public class Welcome1 {
– 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
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
9
2.2
4
A Simple Program: Printing a Line of
Text
public class Welcome1 {
– 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)
7
public static void main( String args[] )
– 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
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
10
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;
}
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
11
Blocks
A pair of braces in a program
forms a block that groups
components of a program.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
12
Statements
•A statement represents 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 (;).
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
13
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.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
14
2.2
7
A Simple Program: Printing a Line of
Text
public static void main( String args[] )
• 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
8
{
– Left brace begins body of method declaration
• Ended by right brace } (line 11)
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
15
2.2
9
A Simple Program: Printing a Line of
Text
System.out.println( "Welcome to Java Programming!" );
– 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 ;
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
16
2.2
11
A Simple Program: Printing a Line of
Text
} // end method main
– Ends method declaration
13
–
–
–
–
–
} // end class Welcome1
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
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
17
Compiling a Java program
• Compiling a program
– OpenJCreator
• See website for instructions
• http://www.cs.nyu.edu/courses/spring09/V22.0002001/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.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
18
Executing program.. Lets take a look how to
do this using JCreator
• Executing a program
– Type java Welcome1
• Interpreter loads .class file for class Welcome1
• .class extension omitted from command
– Interpreter calls method main
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
19
Lets Modify Our First Java Program
• Modify previous example to print same contents
using different code
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
20
Modifying Our First Java Program
• Modifying programs
– Welcome2.java produces same output as Welcome1.java
(Fig. 2.1)
– Using different code
9
10
System.out.print( "Welcome to " );
System.out.println( "Java Programming!" );
– Line 9 displays “Welcome to ” with cursor remaining on
printed line
– Line 10 displays “Java Programming! ” on same line with
cursor on next line
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
21
Methods
•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.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Fig. 2.3: Welcome2.java
// Printing a line of text with multiple statements.
public class Welcome2 {
// main method begins execution of Java application
public static void main( String args[] )
{
System.out.print( "Welcome to " );
System.out.println( "Java Programming!" );
} // end method main
} // end class Welcome2
System.out.print keeps the cursor on
the same line, so System.out.println
continues on the same line.
Welcome to Java Programming!
Outline
Welcome2.java
1. Comments
2. Blank line
3. Begin class
Welcome2
3.1 Method main
4. Method
System.out.prin
t
4.1 Method
System.out.prin
tln
5. end main,
Welcome2
 2003 Prentice Hall, Inc.
All rights reserved.
23
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)
9
System.out.println( "Welcome\nto\nJava\nProgramming!" );
– 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!" );
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
24
Outline
1
2
3
4
5
6
7
8
9
10
11
12
13
// Fig. 2.4: Welcome3.java
// Printing multiple lines of text with a single statement.
Welcome3.java
public class Welcome3 {
1. main
// main method begins execution of Java application
public static void main( String args[] )
{
System.out.println( "Welcome\nto\nJava\nProgramming!" );
} // end method main
2.
System.out.prin
tln (uses \n for new
line)
} // end class Welcome3
Program Output
Welcome
to
Java
Programming!
Notice how a new line is output for each \n
escape sequence.
 2003 Prentice Hall, Inc.
All rights reserved.
25
2.3
Modifying Our First Java Program
Escape characters
– Backslash ( \ )
– Indicates special characters be output
Esc a p e
De sc rip tio n
se q ue nc e
\n
Newline. Position the screen cursor at the beginning of the
next line.
\t
Horizontal tab. Move the screen cursor to the next tab stop.
\r
Carriage return. Position the screen cursor at the beginning of
the current line; do not advance to the next line. Any
characters output after the carriage return overwrite the
characters previously output on that line.
\\
Backslash. Used to print a backslash character.
\"
Double quote. Used to print a double-quote character. For
example,
System.out.println( "\"in quotes\"" );
displays
"in quotes"
Fig. 2.5 So m e c o m m o n e sc a p e se q ue nc e s.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
26
Find the Error
Sample 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  2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
27
Find the Error
Sample 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  2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
28
Find the Errors (7)
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  2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.
29
Anatomy of a Java Program
•
•
•
•
•
•
•
Comments
Reserved words
Statements
Blocks
Classes
Methods
The main method
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class.