Your SlideShare is downloading. ×
02 prepcode
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,168
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Anatomy of a Class
    LIS4930 © PIC
    The name of the method
    The name of this class
    This is a class
    The return type. Void means there is nothing returned
    Opening of curly brace of the class
    Arguments to the method. This method must be given an array of String, and the array will be called ‘args’
    Public so everyone can access it
    public class MyFirstApp {
    public static void main (String [ ] args ) {
    System.out.println (“I Rule!”) ;
    }
    }
    Opening brace of the method
    We’ll cover this later.
    Every statement MUST end in a semicolon!
    Closing brace of the MyFirstApp class
    A string you want to print
    This says print to standard output (defaults to command-line)
    Closing brace 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.
    LIS4930 © PIC
  • 3. Writing a Class with a Main
    LIS4930 © PIC
    In Java everything goes into a class. You’ll type your source code file (with a .java extension), then compile it into a new class file (with a .class extension). When you run your program, you’re really running a class.
    1
    MyFirstApp.java
    public class MyFirstApp {
    public static void main (String [ ] args) {
    System.out.println(“I Rule!”);
    System.out.println(“The World”);
    }
    }
    Save
    2
    Compile
    javac MyFirstApp.java
    3
    java MyFirstApp
    Run
  • 4. What can the main method do?
    LIS4930 © PIC
    Your code can tell the JVM to:
    do something again and again
    do something under this condition
    do something
    while (x >12) {
    x = x + 1;
    }
    for (intx = 0; x < 10; x = x + 1) {
    System.out.print(“x is now “+ x);
    }
    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”);
    intx = 3;
    String name = “Kyle”;
    x = x +17;
    System.out.print(“x is “ + x);
    double d = Math.random();
    //this is a comment
    1
    2
    3
    Statements: declarations, assignments, method calls, etc.
    Loops: for and while
    Branching: if…else tests
  • 5. 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).
    intweight; //type: int, name: weight
    Classes and methods must be defined within a pair of curly braces.
    public void go( ) { //amazing code here }
    LIS4930 © PIC
  • 6. Looping, Looping, Looping
    LIS4930 © PIC
    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
    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.
    curly braces.
    while (moreBalls == true) {
    keepJuggling( );
    }
  • 7. Simple Boolean Tests
    LIS4930 © PIC
    You can do a simple boolean test by checking the value of a variable, using a comparison operator including:
    < (less than)
    > (greater than)
    == (equality)
    intx = 4; //assign 4 to x
    while (x > 3) {
    // loop code will run because
    // xis greater than 3
    x = x – 1;
    }
    intz = 27;
    while (z == 17) {
    // loop code will not run because
    // z is not equal to 17
    }
    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 
  • 8. Example of a while loop
    LIS4930 © PIC
    public class Loopy {
    public static void main (String[ ] args) {
    intx = 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
  • 9. Conditional Branching
    LIS4930 © PIC
    public class IfTest {
    public static void main (String[ ] args) {
    intx = 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) {
    intx = 3;
    if (x == 3) {
    System.out.println(“x must be 3”);
    }
    System.out.println(“This runs no matter what”);
    }
    }
    What’s different?
  • 10. Conditional Branching
    LIS4930 © PIC
    public class IfTest {
    public static void main (String[ ] args) {
    intx = 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?
  • 11. Coding a Serious Business Application
    LIS4930 © PIC
    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?
  • 12. Be Prepared ~ Robert Baden-Powell
    Before you start programming begin with creating prepcode.
    Use keywords like:
    LIS4930 © PIC
    A form or pseudocode, to help you focus on the logic without stressing about syntax.
  • 13. What would the prepcode look like for “99 bottles of beer on the wall”?
    LIS4930 © PIC
    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.”
  • 14. Try it Yourselves!
    Work with the person sitting next to you (groups of two ONLY).
    Write prepcode, as demonstrated in class, for creating a Java program to print out the lyrics to a popular camp/bus song.
    Use a word processor to type up your final draft and save it for later.
    Raise your hand for the teacher or TA to inspect your group’s final draft and give suggestions on how it might be improved.
    Have the TA signoff the attendance sheet to mark you were present in class and did the work.
    LIS4930 © PIC
  • 15. Homework!
    Practice writing prepcode.
    Install Eclipse.
    Experiment with writing your first Java program.
    Quizzes start next week… (I won’t remind you every again – quizzes are listed on the Course Content page)
    LIS4930 © PIC

×