Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.

Similar presentations


Presentation on theme: "Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743."— Presentation transcript:

1 Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743

2 What you must do At the end of every piece of code type a “;” – This signifies that the line of code is over All pieces of code that do something need to be in a function – You can declare variables outside a function but if you want to change a value you need it inside a function At the end of every piece of code type a “;” – This signifies that the line of code is over All pieces of code that do something need to be in a function – You can declare variables outside a function but if you want to change a value you need it inside a function

3 Functions Functions are pieces of code which return a value – You can call a function from another function What function returns is denoted by the code return (what you want to return); – The return has to be of a certain type Functions are pieces of code which return a value – You can call a function from another function What function returns is denoted by the code return (what you want to return); – The return has to be of a certain type

4 Function’s continued Form of a function Public (what to return) (function name)(variables that are being passed to the function to use){ //the code for the function goes here Return (a variable or value of the same type as above, is not needed if above is text “void”); } Form of a function Public (what to return) (function name)(variables that are being passed to the function to use){ //the code for the function goes here Return (a variable or value of the same type as above, is not needed if above is text “void”); }

5 Functions continued Ex function Public void write(string text){ System.out.println(text); } Ex function Public void write(string text){ System.out.println(text); }

6 Public, private, ? Public – is not as secure, any class can read it’s input output Private – is more secure, only this class can read its input Public – is not as secure, any class can read it’s input output Private – is more secure, only this class can read its input

7 classes Each class has a separate document Class can reference each other or themselves – When a class references itself, it is recursive Classes are initiated using the code – Class name = new class(); – Cat Steve = new cat(); Each class has a separate document Class can reference each other or themselves – When a class references itself, it is recursive Classes are initiated using the code – Class name = new class(); – Cat Steve = new cat();

8 Variables Variables are created by using a reserved word then the name of the variable then a ; Reserved words for variables are words that you can not use as the name of something since it means you are declaring something – Int declares an integer, a whole number - 2,147,483,648 and +2,147,483,647 – String declares a String of text – Double is a number with a decimal it takes up more memory then an int Variables are created by using a reserved word then the name of the variable then a ; Reserved words for variables are words that you can not use as the name of something since it means you are declaring something – Int declares an integer, a whole number - 2,147,483,648 and +2,147,483,647 – String declares a String of text – Double is a number with a decimal it takes up more memory then an int

9 Variables continued The value of variable can be set at any time inside a class The value of a variable can change inside a function If the variable is declared inside a function it can only be seen in that function If a variable is declared inside a class it can only be seen in that instance of that class – Functions inside the class can reference and change values of the variable The value of variable can be set at any time inside a class The value of a variable can change inside a function If the variable is declared inside a function it can only be seen in that function If a variable is declared inside a class it can only be seen in that instance of that class – Functions inside the class can reference and change values of the variable

10 Advanced variables Instances of classes are referenced by creating a variable Cat bob; or Cat bob= new cat(); Instances of classes are referenced by creating a variable Cat bob; or Cat bob= new cat();

11 Examples with variables Int a = 5; int b; b = a+1; b = a-1 String c; String c = integer.parseint(a); Int a = 5; int b; b = a+1; b = a-1 String c; String c = integer.parseint(a);

12 Comments There are two types of comments – Single line which is done by typing // before the text which you want the compiler to ignore Ex // this code is important Ex a= 1; //this code makes a = 1 – Paragraph comments are opened with /* and closed with */ every thing in between is ignored by the compiler. – Ex /* this is some text*/ – Ex /* this is some more text it is very long and take up 2 lines */ There are two types of comments – Single line which is done by typing // before the text which you want the compiler to ignore Ex // this code is important Ex a= 1; //this code makes a = 1 – Paragraph comments are opened with /* and closed with */ every thing in between is ignored by the compiler. – Ex /* this is some text*/ – Ex /* this is some more text it is very long and take up 2 lines */

13 Arrays = matrix Type [] name = new Type[]; Can be any number of dimensions String[][] anArray= new String[3][3]; Can call value by anArray[2][0]; Type [] name = new Type[]; Can be any number of dimensions String[][] anArray= new String[3][3]; Can call value by anArray[2][0];

14 If Test for a condition – If it is true a block of code is executed, if it is not true the code is not executed. Else – Goes after an if block and is executed if the if is false Else if – A combination of else and if, if is only tested if the above is false, can be followed by another else if or else Contends are tested with == for = and != for not = You can test multiple things by using || for or, && for and Test for a condition – If it is true a block of code is executed, if it is not true the code is not executed. Else – Goes after an if block and is executed if the if is false Else if – A combination of else and if, if is only tested if the above is false, can be followed by another else if or else Contends are tested with == for = and != for not = You can test multiple things by using || for or, && for and

15 Ex code: if(a == 1){ // Do something if a = 1; }else if(a == 2||a==3){ // Do something if a = 2 or a = 3; }else{ && /*Do something if a does not = 1,2 or 3; */ } if(a == 1){ // Do something if a = 1; }else if(a == 2||a==3){ // Do something if a = 2 or a = 3; }else{ && /*Do something if a does not = 1,2 or 3; */ }

16 Loops While- while(a<10){.... – Executes code while a value is true, no variable creation or variable changes Do while –do{.... } while(a<10); – Executes at least once, if the while statement is true then the code will execute again, else will not execute anymore For – for(int a=0;a<10;a++){.... – Quick and dirty, creates a variable and keeps running until second part is not true, last part tells program what to do at end of each loop’s run through While- while(a<10){.... – Executes code while a value is true, no variable creation or variable changes Do while –do{.... } while(a<10); – Executes at least once, if the while statement is true then the code will execute again, else will not execute anymore For – for(int a=0;a<10;a++){.... – Quick and dirty, creates a variable and keeps running until second part is not true, last part tells program what to do at end of each loop’s run through

17 java docs List of built in functions and classes http://download.oracle.com/javase/6/docs/a pi/ http://download.oracle.com/javase/6/docs/a pi/ List of built in functions and classes http://download.oracle.com/javase/6/docs/a pi/ http://download.oracle.com/javase/6/docs/a pi/

18 Example code continued hello world just like the picture type System.out.println(“hello world”); in the main function Press f11 to see your program run System.out.println(); is a function that is built into java, you passed the string “hello world” to it and the function displayed hello world in the print box just like the picture type System.out.println(“hello world”); in the main function Press f11 to see your program run System.out.println(); is a function that is built into java, you passed the string “hello world” to it and the function displayed hello world in the print box Print box

19 Example two- Math Homework – Write a program which can check to see if a* b = c – When you input a,b and c The next slide gives a possible solution – Write a program which can check to see if a* b = c – When you input a,b and c The next slide gives a possible solution

20 Math homework answer Int a = 1; Int b = 2; Int c = 3; If(a*b == c){ System.out.println(“a*b = c”); }else{ System.out.println(“a*b not = c”); } Int a = 1; Int b = 2; Int c = 3; If(a*b == c){ System.out.println(“a*b = c”); }else{ System.out.println(“a*b not = c”); }

21 Questions? ? ?

22 More examples – tick tack toe Create a tick tack toe board to look like: *|*|* Create a tick tack toe board to look like: *|*|*

23 tick tack toe continued //first line System.out.print(board[0][0]+”|”); System.out.print(board[0][1]+”|”); System.out.println(board[0][2]); //second line System.out.print(board[1][0]+”|”); System.out.print(board[1][1]+”|”); System.out.println(board[1][2]); //third line System.out.print(board[2][0]+”|”); System.out.print(board[2][1] +”|”); System.out.println(board[2][2]); //first line System.out.print(board[0][0]+”|”); System.out.print(board[0][1]+”|”); System.out.println(board[0][2]); //second line System.out.print(board[1][0]+”|”); System.out.print(board[1][1]+”|”); System.out.println(board[1][2]); //third line System.out.print(board[2][0]+”|”); System.out.print(board[2][1] +”|”); System.out.println(board[2][2]);


Download ppt "Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743."

Similar presentations


Ads by Google