Slide 1

Slide 1 text

Introduction to Java Programming 25 June 2012: Day 1 John Taylor Doug Stephen 1 Wednesday, June 27, 12

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

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)

Slide 21

Slide 21 text

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)

Slide 22

Slide 22 text

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)

Slide 23

Slide 23 text

Example 8 Wednesday, June 27, 12

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Programming Flow 9 Wednesday, June 27, 12

Slide 33

Slide 33 text

Programming Flow • Programs really are lists of instructions like we just saw 9 Wednesday, June 27, 12

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Programming Flow • Decisions/”Conditionals” 10 Wednesday, June 27, 12

Slide 37

Slide 37 text

Programming Flow • Decisions/”Conditionals” if(this is true) { do this } else { do this instead } 10 Wednesday, June 27, 12

Slide 38

Slide 38 text

Programming Flow • Decisions/”Conditionals” 11 Wednesday, June 27, 12

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Programming Flow • Repetitions/”Loops” 12 Wednesday, June 27, 12 Counter is an integer variable

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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.

Slide 43

Slide 43 text

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.

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

/* 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

Slide 51

Slide 51 text

/* 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

Slide 52

Slide 52 text

/* 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

Slide 53

Slide 53 text

/* 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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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.

Slide 56

Slide 56 text

Common Programming Tricks • Try it out! 18 Wednesday, June 27, 12 This *will* mess you up.

Slide 57

Slide 57 text

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.

Slide 58

Slide 58 text

Quiz Time! 19 Wednesday, June 27, 12

Slide 59

Slide 59 text

Question 20 Wednesday, June 27, 12

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Question What are the three “types” we talked about for storing numbers in variables? int, float, and double 20 Wednesday, June 27, 12

Slide 62

Slide 62 text

Question 20 Wednesday, June 27, 12

Slide 63

Slide 63 text

Question 21 Wednesday, June 27, 12

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Question 21 Wednesday, June 27, 12

Slide 67

Slide 67 text

Question 22 Wednesday, June 27, 12

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

Question 22 Wednesday, June 27, 12

Slide 71

Slide 71 text

Question 23 Wednesday, June 27, 12

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Question 23 Wednesday, June 27, 12

Slide 75

Slide 75 text

Question 24 Wednesday, June 27, 12

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

Question 24 Wednesday, June 27, 12

Slide 79

Slide 79 text

Question 25 Wednesday, June 27, 12

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

Question 25 Wednesday, June 27, 12

Slide 83

Slide 83 text

Question 26 Wednesday, June 27, 12

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

Question 26 Wednesday, June 27, 12

Slide 87

Slide 87 text

Question 27 Wednesday, June 27, 12

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

Question 27 Wednesday, June 27, 12

Slide 91

Slide 91 text

Question 28 Wednesday, June 27, 12

Slide 92

Slide 92 text

Question What is wrong with this statement: int x = 2.5 28 Wednesday, June 27, 12

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Question 28 Wednesday, June 27, 12

Slide 95

Slide 95 text

Question 29 Wednesday, June 27, 12

Slide 96

Slide 96 text

Question int, double, and float are _____ 29 Wednesday, June 27, 12

Slide 97

Slide 97 text

Question int, double, and float are _____ types 29 Wednesday, June 27, 12

Slide 98

Slide 98 text

Question 29 Wednesday, June 27, 12

Slide 99

Slide 99 text

Round Two It’s about to get real 30 Wednesday, June 27, 12

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

Write a method to reverse the sign of an integer and return it! int reverseSign(int x) { return -x; } 32 Wednesday, June 27, 12

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

Debug This Code 34 Wednesday, June 27, 12

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

What is wrong with this code? 35 Wednesday, June 27, 12

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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