SlideShare uma empresa Scribd logo
1 de 13
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
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
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
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
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
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( ); }
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 
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
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?
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?
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?
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.
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.”

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Loops c++
Loops c++Loops c++
Loops c++
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Loops in c
Loops in cLoops in c
Loops in c
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
exceptions in java
exceptions in javaexceptions in java
exceptions in java
 
The Loops
The LoopsThe Loops
The Loops
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 
Class method object
Class method objectClass method object
Class method object
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
 
NinjaSynch
NinjaSynchNinjaSynch
NinjaSynch
 
170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements
 

Destaque (7)

13 interfaces
13 interfaces13 interfaces
13 interfaces
 
Chapter.10
Chapter.10Chapter.10
Chapter.10
 
09 polymorphism
09 polymorphism09 polymorphism
09 polymorphism
 
Html5
Html5Html5
Html5
 
Javascript2
Javascript2Javascript2
Javascript2
 
8 Typography Notes
8 Typography Notes8 Typography Notes
8 Typography Notes
 
Web architecture
Web architectureWeb architecture
Web architecture
 

Semelhante a 02 prepcode

Semelhante a 02 prepcode (20)

02 - Prepcode
02 - Prepcode02 - Prepcode
02 - Prepcode
 
Java
JavaJava
Java
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
 
01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one01-ch01-1-println.ppt java introduction one
01-ch01-1-println.ppt java introduction one
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Java concurrency questions and answers
Java concurrency questions and answers Java concurrency questions and answers
Java concurrency questions and answers
 
14b exceptions
14b exceptions14b exceptions
14b exceptions
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
J Unit
J UnitJ Unit
J Unit
 
Programming in Java: Organising Your Code
Programming in Java: Organising Your CodeProgramming in Java: Organising Your Code
Programming in Java: Organising Your Code
 
Introduction to java programming part 2
Introduction to java programming  part 2Introduction to java programming  part 2
Introduction to java programming part 2
 
Java programs
Java programsJava programs
Java programs
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstraction
 
Exploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systemsExploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systems
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Thread
ThreadThread
Thread
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 

Mais de Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Sdlc
SdlcSdlc
Sdlc
 
Mysocial
MysocialMysocial
Mysocial
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
15b more gui
15b more gui15b more gui
15b more gui
 

02 prepcode

  • 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