intro programming

23
Intro Programming By Trevor Decker Team 1743

Upload: mele

Post on 24-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Intro Programming

Intro Programming

By Trevor Decker Team 1743

Page 2: Intro Programming

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

Page 3: Intro Programming

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

Page 4: Intro Programming

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”);

}

Page 5: Intro Programming

Functions continued

• Ex function Public void write(string text){

System.out.println(text);

}

Page 6: Intro Programming

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

Page 7: Intro Programming

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();

Page 8: Intro Programming

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

Page 9: Intro Programming

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

Page 10: Intro Programming

Advanced variables

• Instances of classes are referenced by creating a variable

• Cat bob;or

• Cat bob= new cat();

Page 11: Intro Programming

Examples with variables

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

Page 12: Intro Programming

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

Page 13: Intro Programming

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];

Page 14: Intro Programming

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

Page 15: Intro Programming

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

}

Page 16: Intro Programming

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

Page 17: Intro Programming

java docs

• List of built in functions and classes

• http://download.oracle.com/javase/6/docs/api/

Page 18: Intro Programming

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

Page 19: Intro Programming

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

Page 20: Intro Programming

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”);}

Page 21: Intro Programming

Questions?

?

Page 22: Intro Programming

More examples – tick tack toe

• Create a tick tack toe board to look like:• *|*|*• *|*|*• *|*|*

Page 23: Intro Programming

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