Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS256 Computer Science I
Kevin Sahr, PhD
Lecture 4:
Basic Program Components 
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
1-
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-
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
1
2
3
4
// 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
Comments
5
// 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
White Space
✴blank lines, tabs, and spaces
1-
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-
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
5
6
7
8
1-
Major Class Parts
✴the class header
• specifies the class name
✴the class body
• contains the contents of the class
• delimited with curly braces { }
Class Body
Class Headerpublic class SomeClassName
{
   
} // class SomeClassName
Class Syntax
10
Class Body
Class Header
// 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
Hello Class Parts
11
1-
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
9
10
11
12
1-
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
the main method
// 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
Hello Class Methods
14
Method Body
Method Header
Statement
Method Name
Method Parts
15
   public static void main (String [] args)
  {
      Output.showMessage("Hello world!");
  } // method main
1-
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
13
14
15
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
1-
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
✴
Method Invocation Statements
✴syntax:
✴ ClassName.methodName(arguments);
✤
19
Name of class 
that contains the 
method
the dot operator
information 
needed by the 
method
Method Invocation Statements
20
tells showMessage
what to output
defined in 
Output class
✴syntax:
✴ ClassName.methodName(arguments);
✴EX:
✴ Output.showMessage(“Hello World!”);
17
18
19
20
1-
✴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
Memorize Syntax
1-
Lecture 4 Vocabulary
comments
white space
class
class name
class header
class body
method
statement
method header
method name
method body
method invocation
method invocation 
statement
dot operator
arguments
21
22