23
Week 2-3 Type object, conversion, scope variable, printing, String, Conventions

Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Embed Size (px)

Citation preview

Page 1: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Week 2-3Type object, conversion, scope variable, printing, String,

Conventions

Page 2: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

! public static void main(String[] args) !! {!! ! System.out.println(“Hello!");!! }!

Page 3: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Variables

int Lifes;!Lifes = 0;!Lifes = Lifes + 1;

Page 4: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Boolean expression

boolean b;!!b = mouse.down;!b = 5<3; // false!!b = mouse.down || true;!!b = mouse.edge;!!

Page 5: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Conditionsif (expression)!{!! //Something!}!else!{!! //Something else!}!

Page 6: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Loopsfor(int i=0;i<10;i++)!{!}!!while(expression)!{!}!!while(true)!{!}

Page 7: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

double a1, a2, a3, a4, a5, midterm, final;!!double finalMark1 = (a1 + a2 + a3 + a4 + a5) * !! 0.4 + midterm * 0.2 + final * 0.4;!!double finalMark2 = (a1 + a2 + a3 + a4 + a5) * !! 0.45 + final * 0.55;!!double final = finalMark1;!!if (finalMark2>final)!{!! final = finalMark2; !}!!System.out.println(final);

Grading Algorithm

Page 8: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

InputScanner sc = new Scanner(System.in); !int i = sc.nextInt();

Page 9: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Type objectScanner sc = new Scanner(System.in); !Random r = new Random();!MyClass c = new MyClass(“rock”);!Vector3D v = new Vector3D(0,0,0);!!

Page 10: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Looping keywordswhile(true)!{!! break;!! //Will break the loop and ! !! //continue on the program!}!for(int i = 0; i<10; i++)!{!! if(i%2 == 0)!! ! continue; !! //Will skip the rest of the loop!! System.out.println(i); !}

Page 11: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Type

int -> 1!double -> 1.0!String -> “2”!boolean -> true or false!

Page 12: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Converting

//int -> double!//int can go in a double !//but double cannot go in an int !//without casting!!int x = (int)1.0;!double d = (double)1;!!

Page 13: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Existence of variable

for (int x = 0; x<10; x++)!{!! //Looping!}!x = 10;!//Does variable x exists?

Page 14: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

{ } convention//add four spaces (tabulation) after!//a {!!for(int i = 0; i<10; i++)!{! //Yeah I added 4 spaces!} !//remove 4 spaces before putting your!//closing bracket!!//By convention{} have to be vertically !//aligned!

Page 15: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Printing

String s = “ “;!System.out.print(s);!//print a space and does not jump !//line!!System.out.println(s);!//print space and jump line!!System.out.print(“\n”);!//print a jump line command

Page 16: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

String typeString s = “1”; !int i = 1;!!boolean b = (s == i); !//impossible to compare; not the !//same type!!//One variable has to be translated!String s2 = “”+i; !//or !String s2 = String.valueOf(i);!

Page 17: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

String equalityString s1 = “1”;!String s2 = “2”;!!//Cannot use == for string !//Have to use equals method!boolean b = s1.equals(s2);!!//valid as well and equivalent !b = s2.equals(s1); !

Page 18: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Variable naming//Unicode letters and digits + $ + _!//case sensitive !!//Camel Case!int legoSetNumberBricks = 10; !!//One letter variable is generally bad!int i = 0; //but sometimes useful

Page 19: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know
Page 20: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Variables using//Beginning program = Variables!int x;!Scanner sc = new Scanner(System.In);!!//Start process!While(x<50)!{!! x = sc.nextInt(); !}

Page 21: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Tutorials//Scratch tutorial!!String tuto1 = “Friday 14h”;!!String tuto2 = “Tuesday 11h”;!!//HELP DESK!!!

Page 22: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Quizz

• Write a program that prints the next 20 leap years

• Write a number guessing game. Let the user know if her guess is too big or too small. At the end print the number of tries.

• Write a program that generates multiplication table for any number.

Page 23: Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that prints the next 20 leap years! • Write a number guessing game. Let the user know

Resources

• Java review - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/

• Scanner tutorial - http://www.youtube.com/watch?v=RhtBOOOGGd8

• Java convention - http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html