0% found this document useful (0 votes)
9 views12 pages

Lec 2

Uploaded by

Thet Hsu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Lec 2

Uploaded by

Thet Hsu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Building Java Programs

Comments
comment: A note written in source code by the
programmer to describe or clarify the code.
 Not executed when your program runs.

Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */
Examples:
// This is a one-line comment.
/* This is a
two-line comment. */

2
Using comments
Where to place comments:
 at the top of each file (a "comment header")
 at the start of every method (seen later)
 to explain complex pieces of code

Comments are useful for:


 Understanding larger, more complex programs.
 Multiple programmers working together, who must understand
each other's code.

3
Comments example
/* Suzy Student, CSE142, Spring 2009
This program prints lyrics about ... something. */

public class BaWitDaBa {


public static void main(String[] args) {
// first verse
System.out.println("Bawitdaba");
System.out.println("da bang a dang diggy diggy");
System.out.println();

// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}

4
Static methods
static method: A named group of statements.
 denotes the structure of a program
class
 eliminates redundancy by code reuse
method A

statement
 procedural decomposition: 
statement
dividing a problem into methods 
statement
method B

statement
Writing a static method is like 
statement
adding a new command to Java. method C

statement

statement

statement
5
Using static methods
1. Design the algorithm.
 Look at the structure, and which commands are repeated.
 Decide what are the important overall tasks.

2. Define (write down) the methods.


 Arrange statements into groups and give each group a name.

3. Call (run) the methods.


 The program's main method executes the other methods to
perform the overall task.

6
Declaring a method
Gives your method a name so it can be executed
 Syntax:
public static void name() {
statement;
statement;
...
statement;
}

 Example:
public static void printWarning() {
System.out.println("This product causes cancer");
System.out.println("in lab rats and humans.");
}

7
Calling a method
Executes the method's code
Syntax:
name();

 You can call the same method many times.

Example:
printWarning();

 Output:

This product causes cancer


in lab rats and humans.

8
Program with static method
public class RepeatIt {
public static void main(String[] args) {
rap(); // Calling (running) the rap method
System.out.println();
rap(); // Calling the rap method again
}

// This method prints the lyrics to my favorite song.


public static void rap() {
System.out.println("Now this is the story all about how");
System.out.println("My life got flipped turned upside-down");
}
}

Output:
Now this is the story all about how
My life got flipped turned upside-down

Now this is the story all about how


My life got flipped turned upside-down
9
Methods calling methods
public class MethodsExample {
public static void main(String[] args) {
message1();
message2();
System.out.println("Done with main.");
}
public static void message1() {
System.out.println("This is message1.");
}
public static void message2() {
System.out.println("This is message2.");
message1();
System.out.println("Done with message2.");
}
}
Output:
This is message1.
This is message2.
This is message1.
Done with message2.
Done with main. 10
Control flow
When a method is called, the program's execution...
 "jumps" into that method, executing its statements, then
 "jumps" back to the point where the method was called.

public class MethodsExample {


public static void main(String[] args) {
public static void message1() {
message1(); System.out.println("This is message1.");
}

message2(); public static void message2() {


System.out.println("This is message2.");
message1();

System.out.println("Done System.out.println("Done
with main."); with message2.");
} }

public static void message1() {


... System.out.println("This is message1.");
}
}
11
When to use methods
Place statements into a static method if:
 The statements are related structurally, and/or
 The statements are repeated.

You should not create static methods for:


 An individual println statement.
 Unrelated or weakly related statements.
(Consider splitting them into two smaller methods.)

The order of methods in a class does not matter to Java


 Pick a sensible order for humans
 Example: main either at top or bottom (let’s say top)

12

You might also like