02 - Prepcode
-
Upload
thewhiteafrican -
Category
Education
-
view
50 -
download
0
description
Transcript of 02 - Prepcode
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
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
}
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.
What’s this?
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.
2do 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
3do 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
Syntax FunEach 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 }
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 test within
curly 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.
Simple Boolean TestsYou 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 xwhile (x > 3) {
// loop code will run because
// x is greater than 3x = x – 1;
}int z = 27;while (z == 17) {
// loop code will not run because
// z is not equal to 17}
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
Conditional Branching
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 class IfTest {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?
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?
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?
Be Prepared ~ Robert Baden-Powell
Before you start programming begin with creating prepcode.
Use keywords like:
A form or pseudocode, to help you focus on the logic without stressing about
syntax.
METHOD IF ELSE
GET DECLARE RETURN
ASSIGN CONVERT WHILE
PRINT INCREMENT DECREMENT
COMPARE CHECK SET
REPEAT EQUALS
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 1PRINT 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 1PRINT counter + “bottle of beer on the wall.”END IFELSEPRINT counter + “bottles of beer on the wall.”END ELSEEND 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.”
Homework!
Practice writing prepcode.
Install Eclipse.
Experiment with writing your first Java program.
Quizzes start next week