Upgrade to Pro — share decks privately, control downloads, hide ads and more …

techConnect 2012: Intro to Java Programming - D...

techConnect 2012: Intro to Java Programming - Day 1

Slides for techConnect 2012's Intro to Java Programming series, Day 1. Topics include variables and other basic programming structures like conditionals, loops, and methods.

Doug Stephen

June 27, 2012
Tweet

More Decks by Doug Stephen

Other Decks in Programming

Transcript

  1. Introduction to Java Programming 25 June 2012: Day 1 John

    Taylor Doug Stephen 1 Wednesday, June 27, 12
  2. What is Java? 2 Wednesday, June 27, 12 - Object-Oriented

    programming lines up well with how we think about the world (nouns and verbs) - Java is the 1st or 2nd most popular, depending on who you ask and when
  3. What is Java? • Java is a Programming Language 2

    Wednesday, June 27, 12 - Object-Oriented programming lines up well with how we think about the world (nouns and verbs) - Java is the 1st or 2nd most popular, depending on who you ask and when
  4. What is Java? • Java is a Programming Language •

    Java is Object-Oriented 2 Wednesday, June 27, 12 - Object-Oriented programming lines up well with how we think about the world (nouns and verbs) - Java is the 1st or 2nd most popular, depending on who you ask and when
  5. What is Java? • Java is a Programming Language •

    Java is Object-Oriented • Java is one of the most popular programming languages in the world 2 Wednesday, June 27, 12 - Object-Oriented programming lines up well with how we think about the world (nouns and verbs) - Java is the 1st or 2nd most popular, depending on who you ask and when
  6. What can you do with Java? 3 Wednesday, June 27,

    12 - Cross platform with minimal adjustments - Android is built on Java - Many famous web companies write their “back end” code in Java or Java-based tech; Google, Twitter, Foursquare to name a few - All of our software is created using Java
  7. What can you do with Java? • Create “cross-platform” programs

    3 Wednesday, June 27, 12 - Cross platform with minimal adjustments - Android is built on Java - Many famous web companies write their “back end” code in Java or Java-based tech; Google, Twitter, Foursquare to name a few - All of our software is created using Java
  8. What can you do with Java? • Create “cross-platform” programs

    • Create apps for Android phones and tablets 3 Wednesday, June 27, 12 - Cross platform with minimal adjustments - Android is built on Java - Many famous web companies write their “back end” code in Java or Java-based tech; Google, Twitter, Foursquare to name a few - All of our software is created using Java
  9. What can you do with Java? • Create “cross-platform” programs

    • Create apps for Android phones and tablets • Make awesome web apps 3 Wednesday, June 27, 12 - Cross platform with minimal adjustments - Android is built on Java - Many famous web companies write their “back end” code in Java or Java-based tech; Google, Twitter, Foursquare to name a few - All of our software is created using Java
  10. What can you do with Java? • Create “cross-platform” programs

    • Create apps for Android phones and tablets • Make awesome web apps • Build robots! 3 Wednesday, June 27, 12 - Cross platform with minimal adjustments - Android is built on Java - Many famous web companies write their “back end” code in Java or Java-based tech; Google, Twitter, Foursquare to name a few - All of our software is created using Java
  11. The Basics of Programming 4 Wednesday, June 27, 12 -

    Before we learn Java, we have to learn the basics of Programming. There will be a quiz at the end! - Computers will only do *exactly* what they are told; no more, no less.
  12. The Basics of Programming • Writing a program is like

    giving instructions to a baby 4 Wednesday, June 27, 12 - Before we learn Java, we have to learn the basics of Programming. There will be a quiz at the end! - Computers will only do *exactly* what they are told; no more, no less.
  13. The Basics of Programming • Writing a program is like

    giving instructions to a baby • The programmer must provide information and instructions with what to do with said info 4 Wednesday, June 27, 12 - Before we learn Java, we have to learn the basics of Programming. There will be a quiz at the end! - Computers will only do *exactly* what they are told; no more, no less.
  14. The Basics of Programming • Writing a program is like

    giving instructions to a baby • The programmer must provide information and instructions with what to do with said info • Info is data. Instructions on what to do with data are called algorithms. 4 Wednesday, June 27, 12 - Before we learn Java, we have to learn the basics of Programming. There will be a quiz at the end! - Computers will only do *exactly* what they are told; no more, no less.
  15. Giving the Computer Some Data 5 Wednesday, June 27, 12

    - Java can do arithmetic with integers and decimals, negative numbers, etc. - Java can do things with words like alphabetize, uppercase, lowercase, and compare other words to see if they are the same
  16. Giving the Computer Some Data • Most programming languages can

    handle letters and numbers 5 Wednesday, June 27, 12 - Java can do arithmetic with integers and decimals, negative numbers, etc. - Java can do things with words like alphabetize, uppercase, lowercase, and compare other words to see if they are the same
  17. Giving the Computer Some Data • Most programming languages can

    handle letters and numbers 5 + 3 Java knows this is 8 “Hello!” Java knows that this is a word and can do word-y things with it 5 Wednesday, June 27, 12 - Java can do arithmetic with integers and decimals, negative numbers, etc. - Java can do things with words like alphabetize, uppercase, lowercase, and compare other words to see if they are the same
  18. Giving the Computer Some Data Giving the Computer Some Data

    6 Wednesday, June 27, 12 - Strings, Numbers, or anything else. Some programming languages will figure this out on their own, some own’t . Java needs to know ahead of time.
  19. Giving the Computer Some Data • 99.99999999% of the time,

    it’s useful to give data a name so we can come back to it. • These are variables, just like Algebra • Most programming languages want to know what sort of thing a variable contains Giving the Computer Some Data 6 Wednesday, June 27, 12 - Strings, Numbers, or anything else. Some programming languages will figure this out on their own, some own’t . Java needs to know ahead of time.
  20. Giving the Computer Some Data Giving the Computer Some Data

    7 Wednesday, June 27, 12 Variable Naming Conventions: -Variable names must start with a lowercase letter. -Variable names can’t have spaces in them -Variable names can have numbers in them, but they can’t start with them -Variables can’t have any symbols in them except for an underscore ( the _ character)
  21. Giving the Computer Some Data • We do this in

    Java by using “Types” Giving the Computer Some Data 7 Wednesday, June 27, 12 Variable Naming Conventions: -Variable names must start with a lowercase letter. -Variable names can’t have spaces in them -Variable names can have numbers in them, but they can’t start with them -Variables can’t have any symbols in them except for an underscore ( the _ character)
  22. Giving the Computer Some Data • We do this in

    Java by using “Types” Type Name /* Numbers */ int x /* Integer */ float y /* Decimal */ double z /* Decimal, too */ /* Words */ String name /* Words */ char favoriteLetter /* Letter */ Giving the Computer Some Data 7 Wednesday, June 27, 12 Variable Naming Conventions: -Variable names must start with a lowercase letter. -Variable names can’t have spaces in them -Variable names can have numbers in them, but they can’t start with them -Variables can’t have any symbols in them except for an underscore ( the _ character)
  23. Example int x = 10; int y = 5; int

    z = x + y; z = ? 8 Wednesday, June 27, 12
  24. Example int x = 10; int y = 5; int

    z = x + y; z = 15 8 Wednesday, June 27, 12
  25. Example int x = 10; int y = 5; int

    z = x + y; x = 25; z = 15 8 Wednesday, June 27, 12
  26. Example int x = 10; int y = 5; int

    z = x + y; x = 25; z = 15 8 Wednesday, June 27, 12
  27. Example int x = 10; int y = 5; int

    z = x + y; x = 25; z = x + y; z = 15 8 Wednesday, June 27, 12
  28. Example int x = 10; int y = 5; int

    z = x + y; x = 25; z = x + y; z = 30 8 Wednesday, June 27, 12
  29. Example int x = 10; int y = 5; int

    z = x + y; x = 25; z = x + y; z = z + 10; z = 30 8 Wednesday, June 27, 12
  30. Example int x = 10; int y = 5; int

    z = x + y; x = 25; z = x + y; z = z + 10; z = 40 8 Wednesday, June 27, 12
  31. Programming Flow • Programs really are lists of instructions like

    we just saw • Pretty tedious to do simple things 9 Wednesday, June 27, 12
  32. Programming Flow • Programs really are lists of instructions like

    we just saw • Pretty tedious to do simple things • We have structures to make this easier 9 Wednesday, June 27, 12
  33. Programming Flow • Decisions/”Conditionals” if(this is true) { do this

    } else { do this instead } 10 Wednesday, June 27, 12
  34. Programming Flow • Decisions/”Conditionals” if(x > 10) { System.out.println(“x is

    big!”); } else { System.out.println(“x is small!”); } /* System.out.println() just tells Java to print what’s in the quotes. */ 11 Wednesday, June 27, 12
  35. Programming Flow • Repetitions/”Loops” while(this is true) { keep doing

    this until it isn’t true anymore } /* Or repeat something an exact number of times */ for(counter = 0; counter < 10; counter = counter + 1) { do this every time } 12 Wednesday, June 27, 12 Counter is an integer variable
  36. Programming Flow • Repetitions/”Loops” 13 Wednesday, June 27, 12 -

    For now, we’ll pretend that nextNumber comes from somewhere. We’ll talk about how this works later. - / is division.
  37. Programming Flow • Repetitions/”Loops” /* Find the average of 10

    of numbers */ int counter; float sum = 0; for(counter = 0; counter < 10; counter = counter + 1) { sum = sum + nextNumber; } float average = sum / 10; 13 Wednesday, June 27, 12 - For now, we’ll pretend that nextNumber comes from somewhere. We’ll talk about how this works later. - / is division.
  38. Programming Flow 14 Wednesday, June 27, 12 - There are

    many situations where you will write code that you want to use again, executing the same instructions but on a different set of data. This is what functions are for. - In the System.out.println() example, the String in quotes is the argument. The job of System.out.println() is to print its argument to the screen - Arguments can be variables or constants, it depends on the method
  39. Programming Flow • Jumps/”Methods” or “Functions” • Reusable, named chunks

    of code • You’ve seen a Method already: System.out.println() • Methods can take in arguments, represents data that the method will manipulate 14 Wednesday, June 27, 12 - There are many situations where you will write code that you want to use again, executing the same instructions but on a different set of data. This is what functions are for. - In the System.out.println() example, the String in quotes is the argument. The job of System.out.println() is to print its argument to the screen - Arguments can be variables or constants, it depends on the method
  40. Programming Flow • Jumps/”Methods” or “Functions” 15 Wednesday, June 27,

    12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  41. Programming Flow • Jumps/”Methods” or “Functions” /* First we must

    define a method */ int addTwoIntegers(int a, int b) { int sum = a + b; return sum; } 15 Wednesday, June 27, 12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  42. Programming Flow • Jumps/”Methods” or “Functions” /* First we must

    define a method */ int addTwoIntegers(int a, int b) { int sum = a + b; return sum; } Return Type Method Name Arguments Return Value Method Body 15 Wednesday, June 27, 12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  43. Programming Flow • Jumps/”Methods” or “Functions” 15 Wednesday, June 27,

    12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  44. /* First we must define a method */ int addTwoIntegers(int

    a, int b) { return a + b; } Programming Flow • Jumps/”Methods” or “Functions” 15 Wednesday, June 27, 12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  45. /* First we must define a method */ int addTwoIntegers(int

    a, int b) { return a + b; } /* Then we use the method */ addTwoIntegers(10,15); /* 25! */ Programming Flow • Jumps/”Methods” or “Functions” 15 Wednesday, June 27, 12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  46. /* First we must define a method */ int addTwoIntegers(int

    a, int b) { return a + b; } /* Then we use the method */ addTwoIntegers(10,15); /* 25! */ /* We can treat a method like it is a variable of its return type */ int x = addTwoIntegers(10, 15); /* x is 25 */ Programming Flow • Jumps/”Methods” or “Functions” 15 Wednesday, June 27, 12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  47. /* First we must define a method */ int addTwoIntegers(int

    a, int b) { return a + b; } /* Then we use the method */ addTwoIntegers(10,15); /* 25! */ /* We can treat a method like it is a variable of its return type */ int x = addTwoIntegers(10, 15); /* x is 25 */ int y = addTwoIntegers(2,6) + addTwoIntegers(8,12); /* y is 28 */ Programming Flow • Jumps/”Methods” or “Functions” 15 Wednesday, June 27, 12 - The return type is the type of the value that the method spits out. - The method name is what you use to “call” the method - The arguments describe what the method needs to work - The method body can be as long as you want, and contains all of the instructions that the method should carry out. This can include the other methods, loops, ifs, etc. - The return value is the value that the method spits back out once it has completed its instructions
  48. Common Programming Tricks • Increment and Decrement an integer (add

    or subtract 1) • Instead of x = x + 1 we use x++ or ++x (use -- for decrement) • All of the arithmetic operators can be “combined” with an equal sign. • Instead of x = x + 12 we can use x += 12. Also -=, *=, /=. • The - sign is used for subtraction as well as negative numbers 16 Wednesday, June 27, 12
  49. Common Programming Tricks • Equals like assignment, vs. equals like

    comparison • We use = to assign a value to a variable, so how do we ask if something is equal to something else? • == means “is equal to” • When you’re “reading” code, you should read “=” as “becomes” and “==” as “is equal to”. 17 Wednesday, June 27, 12 This *will* mess you up.
  50. Common Programming Tricks • Try it out! int x =

    10 The integer x becomes 10 if(x == 10) { ... } if the integer x is equal to 10, do this... 18 Wednesday, June 27, 12 This *will* mess you up.
  51. Question What are the three “types” we talked about for

    storing numbers in variables? 20 Wednesday, June 27, 12
  52. Question What are the three “types” we talked about for

    storing numbers in variables? int, float, and double 20 Wednesday, June 27, 12
  53. Question System.out.println() is an example of a reusable chunk of

    code called a ________ 21 Wednesday, June 27, 12
  54. Question System.out.println() is an example of a reusable chunk of

    code called a ________ method 21 Wednesday, June 27, 12
  55. Question If we need our code to behave differently depending

    on some condition, we use the word _____ 22 Wednesday, June 27, 12
  56. Question If we need our code to behave differently depending

    on some condition, we use the word _____ if 22 Wednesday, June 27, 12
  57. Question If we need to repeat an action over and

    over again but we aren’t sure how many times, we use a _____ loop 23 Wednesday, June 27, 12
  58. Question If we need to repeat an action over and

    over again but we aren’t sure how many times, we use a _____ loop while 23 Wednesday, June 27, 12
  59. Question If we need to repeat an action over and

    over again and we know how many times, we use a _____ loop 24 Wednesday, June 27, 12
  60. Question If we need to repeat an action over and

    over again and we know how many times, we use a _____ loop for 24 Wednesday, June 27, 12
  61. Question True or False: Methods can only contain simple instructions,

    you cannot use loops or decisions 25 Wednesday, June 27, 12
  62. Question True or False: Methods can only contain simple instructions,

    you cannot use loops or decisions False 25 Wednesday, June 27, 12
  63. Question True or False: If we want to make a

    decision based on whether or not a variable is equal to something, we use “=” 26 Wednesday, June 27, 12
  64. Question True or False: If we want to make a

    decision based on whether or not a variable is equal to something, we use “=” False 26 Wednesday, June 27, 12
  65. Question True or False: Java makes it very easy to

    develop programs that run on all operating systems. 27 Wednesday, June 27, 12
  66. Question True or False: Java makes it very easy to

    develop programs that run on all operating systems. True 27 Wednesday, June 27, 12
  67. Question What is wrong with this statement: int x =

    2.5 x is an integer but 2.5 is a float/double 28 Wednesday, June 27, 12
  68. What does this block of code do? int i =

    0; while(i < 10) { System.out.println(i); i = i + 1; } a.) Print “i” 10 times b.) Prints the numbers 1 - 10 c.) Prints the numbers 0 - 10 d.) Prints the numbers 0 - 9 31 Wednesday, June 27, 12
  69. What does this block of code do? int i =

    0; while(i < 10) { System.out.println(i); i = i + 1; } a.) Print “i” 10 times b.) Prints the numbers 1 - 10 c.) Prints the numbers 0 - 10 d.) Prints the numbers 0 - 9 d 31 Wednesday, June 27, 12
  70. Write a method to reverse the sign of an integer

    and return it! 32 Wednesday, June 27, 12
  71. Write a method to reverse the sign of an integer

    and return it! int reverseSign(int x) { return -x; } 32 Wednesday, June 27, 12
  72. In Java, there is a type called “boolean” that can

    be either “true” or “false”. Given two variables int gallonsOfFuel and boolean fuelIsLow write a method that checks whether or not gallonsOfFuel is less than or equal to 5. If is is, then set fuelIsLow to true, if it is not then set fuelIsLow to false. 33 Wednesday, June 27, 12
  73. In Java, there is a type called “boolean” that can

    be either “true” or “false”. Given two variables int gallonsOfFuel and boolean fuelIsLow write a method that checks whether or not gallonsOfFuel is less than or equal to 5. If is is, then set fuelIsLow to true, if it is not then set fuelIsLow to false. boolean isFuelLow(int gallonsOfFuel) { boolean fuelIsLow; if(gallonsOfFuel <= 5) { fuelIsLow = true; } else { fuelIsLow = false; } return FuelIsLow } 33 Wednesday, June 27, 12
  74. Debug This Code doSomething( int number, index) { if( number

    = 3) { number = number +1 } for(i = 0,i < 20000; i ++) { System.out.println(counting); index++; { return index } 34 Wednesday, June 27, 12
  75. What is wrong with this code? int i = 5;

    if(i = 5) System.out.println("i"); 35 Wednesday, June 27, 12
  76. What is wrong with this code? int i = 5;

    if(i = 5) System.out.println("i"); What will it do if you run it like this? 35 Wednesday, June 27, 12
  77. Two Types of Errors • Compiler Error • Your code

    won’t run • Run Time Error • Your code will run but it won’t do it’s job properly 36 Wednesday, June 27, 12