230 likes | 348 Views
Intro Programming . By Trevor Decker Team 1743. 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
E N D
Intro Programming By Trevor Decker Team 1743
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
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
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”); }
Functions continued • Ex function Public void write(string text){ System.out.println(text); }
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
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();
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 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
Advanced variables • Instances of classes are referenced by creating a variable • Cat bob; or • Cat bob= new cat();
Examples with variables Int a = 5; int b; b = a+1; b = a-1 String c; String c = integer.parseint(a);
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 */
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];
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
Ex code: if(a == 1){ // Do something if a = 1; }else if(a == 2||a==3){ // Do something ifa = 2 or a = 3; }else{ && /*Do something if a does not = 1,2 or 3; */ }
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
java docs • List of built in functions and classes • http://download.oracle.com/javase/6/docs/api/
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 Print box
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
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”); }
More examples – tick tack toe • Create a tick tack toe board to look like: • *|*|* • *|*|* • *|*|*
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]);