Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that...
Transcript of Week 2-3 - cs.mcgill.cacs202/2014-01/web/Material/Week2-3.pdf · Quizz • Write a program that...
![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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/1.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/2.jpg)
! 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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/3.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/4.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/5.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/6.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/7.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/8.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/9.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/10.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/11.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/12.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/13.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/14.jpg)
{ } 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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/15.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/16.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/17.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/18.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/19.jpg)
![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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/20.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/21.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/22.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022041209/5d670fe588c9931d358b8720/html5/thumbnails/23.jpg)
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