1. Anatomy of a Class
public class MyFirstApp {
public static void main (String [ ] args ) {
System.out.println (“I Rule!”) ;
}
}
Public so
everyone can
access it
This is a class The name of this class
Opening of curly
brace of the
class
We’ll cover this later.
The return type.
Void means there
is nothing returned Arguments to
the method. This
method must be
given an array of
String, and the
array will be
called ‘args’
Opening brace
of the method
Every statement
MUST end in a
semicolon!
A string you
want to print
This says print to
standard output
(defaults to
command-line)
Closing brace of
the method
Closing brace of
the MyFirstApp
class
The name of the
method
DONT WORRY ABOUT MEMORIZING ANYTHING RIGHT NOW…
THIS IS JUST SOMETHING TO GET THAT SWEET JAVA AMORA IN THE
AIR
2. The main thing is to keep the main
thing the main thing. ~ Stephen Covey
When the JVM (Java Virtual Machine) starts running, it looks for the class
you give it at the command line. Then it starts looking for a specifically-written
method that looks exactly like:
public static void main (String [ ]
args) {
//your code goes here
}
What’s
this?
This is called the “main” method, and the JVM runs everything between the
curly braces { } of this main method. Every Java application has to have as
least one class, and at least one main method. (NOT one main per class;
just one main per application). The main( ) method tells the computer
where to start, and you only need one starting place.
3. What can the main method
do?
Your code can tell the JVM to:
1 do something
int x = 3;
String name = “Kyle”;
x = x +17;
System.out.print(“x is “ +
x);
double d = Math.random();
//this is a comment
Statements: declarations,
assignments, method
calls, etc.
2
do
something
again and
again
while (x >12) {
x = x + 1;
}
for (int x = 0; x < 10; x = x + 1)
{
System.out.print(“x is now “+
x);
}
Loops: for and while
3
do something
under this
condition
if (x == 10) {
System.out.print(“x must be
10”);
} else {
System.out.print(“x isn’t 10”);
}
if ((x < 3) & (name.equals(“Kyle”)))
{
System.out.println(“Gently”);
}
System.out.print(“This line runs no
matter what”);
Branching: if…else tests
4. Syntax Fun
Each statement must end in a semicolon:
x = x + 1;
A single-line comment begins with two forward slashes:
x = 22; //this is a comment
Most white space doesn’t matter:
x = 3 ;
Variables are declared with a name and a type (you’ll learn about
all the Java types in chapter 3).
int weight; //type: int, name: weight
Classes and methods must be defined within a pair of curly
braces.
public void go( ) { //amazing code here }
5. Looping, Looping, Looping
Java offers three types of looping structures: while, do-while, and for.
We’ll discuss the others later, but for now we will only discuss while.
The while loop keeps looping as long as some condition is true, this is called
the conditional test.
What is done on each loop is found inside the loop block, which is located
after the conditional testc uwriltyh in
braces.
while (moreBalls == true) {
keepJuggling( );
}
The key to a loop is a
conditional test. In Java, a
conditional test is an expression
that results in a boolean value –
in other words, something that
is either true or false.
6. Simple Boolean Tests
You can do a simple boolean test by checking the value of a variable, using a
comparison operator including:
< (less than) > (greater
than)
==
(equality)
Yes that is TWO equal
signs. Notice the
difference: the assignment
operator is = and the
equality operator is ==.
Lots of programmers
accidently type = when
they want ==, but not you
int x = 4; //assign 4 to x
while (x > 3) {
// loop code will run because
// x is greater than 3
x = x – 1;
}
int z = 27;
while (z == 17) {
// loop code will not run
because
// z is not equal to 17
}
7. Example of a while loop
public class Loopy {
public static void main (String[ ] args) {
int x = 1;
System.out.println(“Before the Loop.”);
while (x < 4) {
System.out.println(“In the loop”);
System.out.println(“Value of x is “ +
x);
x = x + 1;
}
System.out.println(“This is after the
loop”);
}
}
Let’s see how it
works
8. Conditional Branching
public class IfTest {
public class IfTest {
public static void main (String[ ] args) {
int x = 3;
if (x == 3) {
System.out.print(“x must be 3”);
}
System.out.print(“This runs no matter what”);
}
}
public static void main (String[ ] args) {
int x = 3;
if (x == 3) {
System.out.println(“x must be 3”);
}
System.out.println(“This runs no matter what”);
}
}
What’s
different?
9. Conditional Branching
public class IfTest {
public static void main (String[ ] args) {
int x = 3;
if (x == 3) {
System.out.println(“x must be 3”);
} else {
System.out.println(“x is NOT 3”);
}
System.out.println(“This runs no matter what”);
}
}
What about this
one?
What is
this?
10. Coding a Serious
Business Application
With the tools we have covered up to this point you have just about
enough skills to code your first program.
Who knows the lyrics to “99 Bottles of Beer on the Wall”?
So how do we do it, what
do we need?
11. Be Prepared ~ Robert Baden-Powell
Before you start programming begin with creating
prepcode.
A form or pseudocode, to help you focus on the
logic without stressing about syntax.
Use keywords like:
METHOD IF ELSE
GET DECLARE RETURN
ASSIGN CONVERT WHILE
PRINT INCREMENT DECREMENT
COMPARE CHECK SET
REPEAT EQUALS
12. What would the prepcode look
like for “99 bottles of beer on the
wall”?
Prepcode for “99 bottles of beer on the wall”.
DECLARE counter SET to 99
REPEAT until counter EQUALS 1
PRINT counter + “bottles of beer on the wall,”
PRINT counter + “bottles of beer.”
PRINT “Take one down and pass it around,”
DECREMENT counter
IF counter EQUALS 1
PRINT counter + “bottle of beer on the wall.”
END IF
ELSE
PRINT counter + “bottles of beer on the wall.”
END ELSE
END REPEAT
PRINT counter + “bottle of beer on the wall,”
PRINT counter + “bottle of beer.”
PRINT “Take one down and pass it around,”
PRINT “no more bottles of beer on the wall.”
13. Homework!
Practice writing prepcode.
Install Eclipse.
Experiment with writing your first Java program.
Quizzes start next week