Your SlideShare is downloading. ×
0
Anatomy of a Class<br />LIS4930 © PIC<br />The name of the method<br />The name of this class<br />This is a class<br />Th...
The main thing is to keep the main thing the main thing. ~ Stephen Covey<br />When the JVM (Java Virtual Machine) starts r...
Writing a Class with a Main<br />LIS4930 © PIC<br />In Java everything goes into a class. You’ll type your source code fil...
What can the main method do?<br />LIS4930 © PIC<br />Your code can tell the JVM to: <br />do something again and again<br ...
Syntax Fun<br />Each statement must end in a semicolon:<br />x = x + 1;<br />A single-line comment begins with two forward...
Looping, Looping, Looping<br />LIS4930 © PIC<br />Java offers three types of looping structures: while, do-while, and for....
Simple Boolean Tests<br />LIS4930 © PIC<br />You can do a simple boolean test by checking the value of a variable, using a...
Example of a while loop<br />LIS4930 © PIC<br />public class Loopy {<br />	public static void main (String[ ] args) {<br /...
Conditional Branching<br />LIS4930 © PIC<br />public class IfTest {<br />	public static void main (String[ ] args) {<br />...
Conditional Branching<br />LIS4930 © PIC<br />public class IfTest {<br />	public static void main (String[ ] args) {<br />...
Coding a Serious Business Application<br />LIS4930 © PIC<br />With the tools we have covered up to this point you have jus...
Be Prepared ~ Robert Baden-Powell<br />Before you start programming begin with creating prepcode.<br />Use keywords like:<...
What would the prepcode look like for “99 bottles of beer on the wall”?<br />LIS4930 © PIC<br />Prepcode for “99 bottles o...
Upcoming SlideShare
Loading in...5
×

02 prepcode

1,187

Published on

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

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

No notes for slide

Transcript of "02 prepcode"

  1. 1. Anatomy of a Class<br />LIS4930 © PIC<br />The name of the method<br />The name of this class<br />This is a class<br />The return type. Void means there is nothing returned<br />Opening of curly brace of the class<br />Arguments to the method. This method must be given an array of String, and the array will be called ‘args’ <br />Public so everyone can access it<br />public class MyFirstApp {<br /> public static void main (String [ ] args ) {<br /> System.out.println (“I Rule!”) ;<br /> }<br />}<br />Opening brace of the method<br />We’ll cover this later.<br />Every statement MUST end in a semicolon!<br />Closing brace of the MyFirstApp class<br />A string you want to print<br />This says print to standard output (defaults to command-line)<br />Closing brace of the method<br />DONT WORRY ABOUT MEMORIZING ANYTHING RIGHT NOW… <br />THIS IS JUST SOMETHING TO GET THAT SWEET JAVA AMORA IN THE AIR<br />
  2. 2. The main thing is to keep the main thing the main thing. ~ Stephen Covey<br />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: <br />public static void main (String [ ] args) {<br /> //your code goes here<br />}<br />What’s this?<br />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.<br />LIS4930 © PIC<br />
  3. 3. Writing a Class with a Main<br />LIS4930 © PIC<br />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.<br />1<br />MyFirstApp.java<br />public class MyFirstApp {<br /> public static void main (String [ ] args) {<br /> System.out.println(“I Rule!”);<br /> System.out.println(“The World”);<br /> }<br />}<br />Save<br />2<br />Compile<br />javac MyFirstApp.java<br />3<br />java MyFirstApp<br />Run<br />
  4. 4. What can the main method do?<br />LIS4930 © PIC<br />Your code can tell the JVM to: <br />do something again and again<br />do something under this condition<br />do something<br />while (x >12) {<br />x = x + 1;<br />}<br />for (intx = 0; x < 10; x = x + 1) {<br />System.out.print(“x is now “+ x);<br />}<br />if (x == 10) {<br />System.out.print(“x must be 10”);<br />} else {<br />System.out.print(“x isn’t 10”);<br />}<br />if ((x < 3) & (name.equals(“Kyle”))) {<br />System.out.println(“Gently”);<br />}<br />System.out.print(“This line runs no matter what”);<br />intx = 3;<br />String name = “Kyle”;<br />x = x +17;<br />System.out.print(“x is “ + x);<br />double d = Math.random();<br />//this is a comment<br />1<br />2<br />3<br />Statements: declarations, assignments, method calls, etc. <br />Loops: for and while<br />Branching: if…else tests<br />
  5. 5. Syntax Fun<br />Each statement must end in a semicolon:<br />x = x + 1;<br />A single-line comment begins with two forward slashes:<br />x = 22; //this is a comment<br />Most white space doesn’t matter:<br />x = 3 ;<br />Variables are declared with a name and a type(you’ll learn about all the Java types in chapter 3).<br />intweight; //type: int, name: weight<br />Classes and methods must be defined within a pair of curly braces.<br />public void go( ) { //amazing code here }<br />LIS4930 © PIC<br />
  6. 6. Looping, Looping, Looping<br />LIS4930 © PIC<br />Java offers three types of looping structures: while, do-while, and for. <br />We’ll discuss the others later, but for now we will only discuss while.<br />The while loop keeps looping as long as some condition is true, this is called the conditional test.<br />What is done on each loop is found inside the loop block, which is located after the conditional test within <br />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.<br />curly braces.<br />while (moreBalls == true) {<br />keepJuggling( );<br />}<br />
  7. 7. Simple Boolean Tests<br />LIS4930 © PIC<br />You can do a simple boolean test by checking the value of a variable, using a comparison operator including:<br />< (less than)<br />> (greater than)<br />== (equality)<br />intx = 4; //assign 4 to x<br />while (x > 3) {<br /> // loop code will run because<br /> // xis greater than 3<br />x = x – 1;<br />}<br />intz = 27;<br />while (z == 17) {<br /> // loop code will not run because<br /> // z is not equal to 17<br />}<br />Yes that is TWO equal signs. Notice the difference: the assignment operator is = and the equality operator is ==.<br />Lots of programmers accidently type = when they want ==, but not you <br />
  8. 8. Example of a while loop<br />LIS4930 © PIC<br />public class Loopy {<br /> public static void main (String[ ] args) {<br />intx = 1;<br />System.out.println(“Before the Loop.”);<br /> while (x < 4) {<br />System.out.println(“In the loop”);<br />System.out.println(“Value of x is “ + x);<br />x = x + 1;<br /> }<br />System.out.println(“This is after the loop”);<br /> }<br />}<br />Let’s see how it works<br />
  9. 9. Conditional Branching<br />LIS4930 © PIC<br />public class IfTest {<br /> public static void main (String[ ] args) {<br />intx = 3;<br /> if (x == 3) {<br />System.out.print(“x must be 3”);<br /> }<br />System.out.print(“This runs no matter what”);<br /> }<br />}<br />public class IfTest {<br /> public static void main (String[ ] args) {<br />intx = 3;<br /> if (x == 3) {<br />System.out.println(“x must be 3”);<br /> }<br />System.out.println(“This runs no matter what”);<br /> }<br />}<br />What’s different?<br />
  10. 10. Conditional Branching<br />LIS4930 © PIC<br />public class IfTest {<br /> public static void main (String[ ] args) {<br />intx = 3;<br /> if (x == 3) {<br />System.out.println(“x must be 3”);<br /> } else {<br />System.out.println(“x is NOT 3”);<br /> }<br />System.out.println(“This runs no matter what”);<br /> }<br />}<br />What about this one?<br />What is this?<br />
  11. 11. Coding a Serious Business Application<br />LIS4930 © PIC<br />With the tools we have covered up to this point you have just about enough skills to code your first program. <br />Who knows the lyrics to “99 Bottles of Beer on the Wall”? <br />So how do we do it, what do we need?<br />
  12. 12. Be Prepared ~ Robert Baden-Powell<br />Before you start programming begin with creating prepcode.<br />Use keywords like:<br />LIS4930 © PIC<br />A form or pseudocode, to help you focus on the logic without stressing about syntax.<br />
  13. 13. What would the prepcode look like for “99 bottles of beer on the wall”?<br />LIS4930 © PIC<br />Prepcode for “99 bottles of beer on the wall”.<br />DECLARE counter SET to 99<br />REPEAT until counter EQUALS 1<br />PRINT counter + “bottles of beer on the wall,”<br />PRINT counter + “bottles of beer.”<br />PRINT “Take one down and pass it around,”<br />DECREMENT counter <br />IF counter EQUALS 1<br />PRINT counter + “bottle of beer on the wall.”<br />END IF<br />ELSE<br />PRINT counter + “bottles of beer on the wall.”<br />END ELSE<br />END REPEAT<br />PRINT counter + “bottle of beer on the wall,”<br />PRINT counter + “bottle of beer.”<br />PRINT “Take one down and pass it around,”<br />PRINT “no more bottles of beer on the wall.”<br />
  14. 14. Try it Yourselves!<br />Work with the person sitting next to you (groups of two ONLY).<br />Write prepcode, as demonstrated in class, for creating a Java program to print out the lyrics to a popular camp/bus song.<br />Use a word processor to type up your final draft and save it for later.<br />Raise your hand for the teacher or TA to inspect your group’s final draft and give suggestions on how it might be improved.<br />Have the TA signoff the attendance sheet to mark you were present in class and did the work. <br />LIS4930 © PIC<br />
  15. 15. Homework!<br />Practice writing prepcode.<br />Install Eclipse.<br />Experiment with writing your first Java program.<br />Quizzes start next week… (I won’t remind you every again – quizzes are listed on the Course Content page)<br />LIS4930 © PIC<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×