Java Program Structure - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Java Program Structure

Description:

System.out.println('Goodbye, ' who) ... Goodbye, Bangkok. General Program Structure. package greeter; import javax.swing.JOptionPane; ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 23
Provided by: Jim478
Category:

less

Transcript and Presenter's Notes

Title: Java Program Structure


1
Java Program Structure
  • This program does not use object-oriented style.
    It uses a simple imperative (procedural)style to
    introduce the Java program structure.
  • James Brucker

2
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

This is a Javadoc comment.
Any text placed inside / .... / is ignored by
the compiler.
This is a comment.
A comment can be placed anywhere -- even in the
middle of a statement! A comment begins with
/ It continues until a / is found.
3
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

This defines the start of a "class" named
Greeting1.
In Java, all program statements must be inside of
a class. The class name can be (almost) anything,
but it should begin with a capital letter and not
contain any spaces public class Bank Account
Error! Space in name
4
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

The and braces indicate the start and end
of the code for this class.
In Java, ... defines a block of code. You
will see blocks used in many ways.
5
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

This defines the start of a method named "main".
This defines the start of a method named
main. When you run a program, execution begins in
a method named "main". This method must be
declared public static void main(String args)
6
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

The and braces indicate the start and end
of the code for the method "main".
In Java, ... define a block of code. In this
case, the block of code is the method definition.
7
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

Defines a variable named who that is of type
String. Variable names should start with a
lowercase letter and must not contain spaces.
It also assigns a value of "Bangkok" to the
variable.
You could do the same thing using two
statements String who / define who / who
"Bangkok" / assign /
8
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

String is the name of a Java class. It is part
of the Java software installed on your
computer. "who" is an object of the String class,
defined by your program. It has all the
properties and behavior defined for Strings.
Objects are also called instances since they are
an instantiation of the class.
Congratulations! You just met your first object.
9
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

Every Java statement must end with a semi-colon
()
For example int x 12 int y 7 int z z x
y
10
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

This is another comment.
Anything after // until the end of the line is
part of the comment, and ignored by the compiler.
11
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

System.out.println( ... ) is a Java method that
displays a String on the console.
The argument(s) to a method are placed inside (
... ) after the method name. System.out.println("H
ello") would print Hello on a line by itself.
The argument to method println.
12
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println("Goodbye, "
  • who)

"Hello, " who joins 2 Strings together to
produce a single String.
The compiler with join the String constant
"Hello, " and the value of the String variable
who. The result is "Hello, " who -gt "Hello,
Bangkok" who "Nights" -gt "BangkokNights"
Java doesn't insert any space!
13
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println( "Goodbye, "
  • who )

System.out.println( ... ) a statement can occupy
as many lines as you want, and can contain spaces.
  • This statement is the same as
  • System.out.println("Goodbye, "who)
  • Spaces can help make statements easier to read.

14
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println( "Goodbye, "
  • who )

A semi-colon indicates the end of the statement.
15
First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println( "Goodbye, "
  • who )

Right brace "" indicates the end of the
definition of the "main" method.
16
Saving the First Program
Filename Greeting1.java
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println( "Goodbye, "
  • who )

The name of the Java file must be the same as the
name of the class in the file.
In order to locate (find) classes in files, Java
requires that the filename exactly match the
class name.
Filename Bank.java class Bank ... WRONG (2
errors) Filename bank.java class Banking
...
17
Compiling the First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println( "Goodbye, "
  • who )

To compile Greeting1.java, use a command line
prompt and type javac Greeting1.java
If there are no errors, the compiler (javac) will
create a file named Greeting1.class containing
Java byte-code for this program.
18
Running the First Program
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting1
  • / execution starts in the main method
  • The header (signature) line must
  • be as shown here.
  • /
  • public static void main( String args)
  • String who "Bangkok"
  • // print a message on terminal
  • System.out.println("Hello, "who)
  • System.out.println( "Goodbye, "
  • who )

To run Greeting1.classenter the command java
Greeting1
the output will be shown
Hello, Bangkok Goodbye, Bangkok
Notice that when compiling you must include
".java" with the filename. When running, just
type the class name -- no ".class".
19
General Program Structure
package name is lowercase
  • package greeter
  • import javax.swing.JOptionPane
  • / Print an impersonal greeting message
  • _at_author James Brucker
  • /
  • public class Greeting
  • public static final Strng GREET "Hello"
  • private static int counter 0
  • / instance variable /
  • private String name
  • / constructor for new objects
  • _at_param name is person to greet
  • /
  • public Greeting ( String name )
  • this.name name
  • public void greet( )
  • JOptionPane.showMessageDialog(...)

class javadoc with author tag is required
1. define constants first 2. static variables 3.
instance variables 4. constructor(s) 5. public
methods 6. private methods method names camelCase
20
Using Dr. Java
  • DrJava is a free integrated development
    environment (IDE) for Java from
    http//www.drjava.org
  • You can edit, compile, and run your program in
    DrJava.

"run" command is in Tools.
Compile
Console I/O.
21
Review
  • In Java, all code must be part of a class.
  • A class begins with the declaration
  • public class SomeClassName
  • followed by the class definition delimited by
    ...
  • The "public" means that this class is visible to
    other classes -- other classes can create objects
    from it.
  • Inside a class, code is contained in methods.
  • A method definition is delimited by ...
  • This main method is where program execution
    begins. The main method must have this header
    line
  • public static void main( String args )

22
Review and Thought Question
  • Inside a method we can define variables and
    assign values to them.
  • To define a String variable and assign a value,
    use
  • String variableName "some value"
  • To display a String on the console, we use
  • System.out.println( "What is the meaning?" )

Q What is println ? Q What is System.out ?
Write a Comment
User Comments (0)
About PowerShow.com