Download Slides (pdf file)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS256 Computer Science I
Kevin Sahr, PhD
Lecture 4:
Basic Program Components
1
Our First Java Program
//
//
//
//
File: Hello.java
CS256 Lab 1A
Author: Kevin Sahr
Created: September 20, 2006
public class Hello
{
public static void main (String [] args)
{
Output.showMessage("Hello world!");
} // method main
} // class Hello
2
2
Comments
✴documentation inside the source code
• provides information about the program
• also used to explain complex statements
✴for humans only
• ignored by the Java compiler
✴syntax: comments begin with // and continue to the
end of the current line
1-
3
1-
4
Required Comments for this Class
✴at top of every source code file:
•
•
•
•
file name
lab number
your name
the date
✴following every close curly brace }
• describe what the {}’s contain
Comments
//
//
//
//
File: Hello.java
CS256 Lab 1A
Author: Kevin Sahr
Created: September 20, 2006
public class Hello
{
public static void main (String [] args)
{
Output.showMessage("Hello world!");
} // method main
} // class Hello
5
5
White Space
✴blank lines, tabs, and spaces
//
//
//
//
File: Hello.java
CS256 Lab 1A
Author: Kevin Sahr
Created: September 20, 2006
public class Hello
{
public static void main (String [] args)
{
Output.showMessage("Hello world!");
} // method main
} // class Hello
6
6
White Space
✴enhances human readability
• Java requires minimal white space
✴will discuss rules later; for now try to follow my
example
✴JGrasp works best using tabs for indenting
• do not mix tabs and spaces when indenting
1-
7
1-
8
Class
✴a Java program consists of one or more classes
✴usually one class per file
✴the class SomeClass must be defined in a source
code file named SomeClass.java
✴remember: Java is case sensitive
• EX: defining SomeClass in a source code file named
someClass.java would cause a compiler error
Major Class Parts
✴the class header
• specifies the class name
✴the class body
• contains the contents of the class
• delimited with curly braces { }
1-
9
Class Syntax
public class SomeClassName
{
Class Header
Class Body
} // class SomeClassName
10
10
Hello Class Parts
//
//
//
//
File: Hello.java
CS256 Lab 1A
Author: Kevin Sahr
Created: September 20, 2006
Class Header
public class Hello
{
public static void main (String [] args)
{
Output.showMessage("Hello world!");
} // method main
Class Body
} // class Hello
11
11
Methods
✴a Java class contains one or more methods
✴a method consists of a series of statements
✴a statement is a complete instruction to the
computer
• every Java statement must end with a semi-colon ;
✴when the method is executed (or invoked) each of
the statements inside the method is executed in
order
1-
12
Major Method Parts
✴the method header
• specifies the method name
✴the method body
• contains the method statements
• delimited with curly braces { }
✴The Hello class contains a single method named
main
1-
13
Hello Class Methods
//
//
//
//
File: Hello.java
CS256 Lab 1A
Author: Kevin Sahr
Created: September 20, 2006
the main method
public class Hello
{
public static void main (String [] args)
{
Output.showMessage("Hello world!");
} // method main
} // class Hello
14
14
Method Parts
Method Header
Method Name
public static void main (String [] args)
{
Output.showMessage("Hello world!");
} // method main
Statement
Method Body
15
15
Java Applications
✴a Java application program must have one, and
only one, method named main
✴when a Java application program is executed the
main method is invoked, executing the statements it
contains
✴the main method in our Hello program contains a
single statement that outputs Hello World!
• most main methods contain multiple statements
1-
16
Main Method with Two
Statements
//
//
//
//
File: GoRaiders.java
CS256 Example
Author: Kevin Sahr
Created: September 20, 2006
public class GoRaiders
{
public static void main (String [] args)
{
Output.showMessage("GO GO GO");
Output.showMessage("RAIDERS!!");
} // method main
} // class GoRaiders
17
17
Method Invocation Statements
✴programs can invoke methods defined in other classes
✴a method invocation statement is a program statement
that invokes a method
✴all of the statements in Hello.java and GoRaiders.java
are method invocation statements
✴
1-
18
Method Invocation Statements
the dot operator
✴syntax:
✴
ClassName.methodName(arguments);
Name of class
that contains the
method
information
needed by the
method
✤
19
19
Method Invocation Statements
✴syntax:
✴
ClassName.methodName(arguments);
✴EX:
✴
Output.showMessage(“Hello World!”);
defined in
Output class
tells showMessage
what to output
20
20
Memorize Syntax
✴EX:
✴
Tove.gyre(“Ooga”);
• what is the method name?
✦gyre
• in what class is that method defined?
✦Tove
• what argument(s) does the method take?
✦a single text string
1-
21
1-
22
Lecture 4 Vocabulary
comments
method header
white space
method name
class
method body
class name
method invocation
class header
class body
method invocation
statement
method
dot operator
statement
arguments
Related documents