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.
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
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
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
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
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
• 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
• 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
• 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
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.
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.
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.
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.
- 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
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
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
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.
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.
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)
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)
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)
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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