350 likes | 377 Views
This discussion session will cover previous and current week's labs, go over concepts from the materials, and include quizzes and mini in-class exercises. Other important information and tips will also be provided.
E N D
CS1101 Group1 Discussion 1 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101
Style of discussion • Discuss previous week lab • Discuss current week lab(good to read up the lab question before coming class) • Go through some concepts of materials cover during that week • Quiz • Mini “in-class” exercises
Other Issues • Please do NOT call me sir • I’m also a student! • Check your email regularly • Check out course website regularly • My cs1101 website (for discussion notes and related materials) • Encourage you all to participate actively on the IVLE forum • At any point of time, you can ask question, please let me know whether I’m too soft, mumbling etc
Be passionate about programming • Find sources of practice • Try out codes in the textbook • Past yet cs1101/c PE questions • google for more questions • http://www.comp.nus.edu.sg/~cs1101x • Know the concepts • Look for a src.zip in your jdk folderSystem.out.println(“hello world”); is reallySystem.out.print(“hello world”);newLine();
Nature of CS1101 • This module can be very easy or very tough • Its not one module where you can do last min effort, in fact if you can follow, its one module where you can feel quite relax just before exams • Practice make perfect • Read before hand later topics • It’s difficult to relate a certain syntax to how its typically being used • Seek help if you are facing problem, don’t wait until test/exams then you start to panic
Java Programming Style /Convention • Please read throughhttp://www.comp.nus.edu.sg/~cs1101/3_ca/labs/styleguide/styleguide.html • Indentation • Comment your code • Classes should start with uppercase • Variables should start with lowercase • Methods should start with lowercase • Final variables/constants should be all uppercasee.g final int PI = 3.14;
Java Programming Style /Convention • Variable Names • Case-sensitive • Begins with a letter, dollar sign($) or underscore • White spaces are not permitted • Subsequent characters may be a letter, digit, dollar sign or underscore
CS1101 Labs • !!! Very important - 20% • Make sure you indent your code • If you are using Dr Java, there’s a indentation feature, just select all your code and press Tab • Provide comments (in English please!) • Do early, email me early if you encounter problems • Do NOT copy from anyone!(changing variables names, spacing, shifting codes around is not going to work!)
Learn how to use the Java API • http://java.sun.com/javase/6/docs/api/ • Go to google -> Search for java 1.6 api-> Press I’m feeling Lucky • E.gScanner sc = new Scanner(System.in);int I = sc.nextInt();there’s also nextDouble(), nextFloat(), …
Learn how to use the Java API • Scanner • Import details : java.util.Scanner • Constructornew Scanner(…); • Methodsreturn type and method nameint : nextInt();String : nextLine(); • Math
Numerical Data int - 32 bit 231 + 1 + (231 - 1) = 232 231 = -2147483648231 - 1 = -2147483647
Question Q. What is the result of : System.out.println(011); 9 How do we get 9? Octal(base 8) 1 * 81 + 1 * 80 = 9
Primitive Data Types • boolean • char • int • float • double Did you know that char is actually of a integer type char a = 'A'; System.out.println(a + 0); //print 65 System.out.println((char)(a + 1)); //print B
Reserved Words • Reserved words cannot be used as identifiers (variables names)
Statements • Statements are code that must do something • Some typical statements you will see up to now: • Variable declarationint num; • Assignment statementsnum = 1; • Prefix / Postfix statements++x; x--; etc • Method calls
Statements • Java also allow you to define blocks public static void main(String args[]){ { int num = 1; System.out.println(num); } int num = 2; System.out.println(num); } prints 1 2
Expressions • A set of codes that returns a valueE.g(1 + 1)this piece of code returns/gives you a value of 2 • Could be something likeSystem.out.println( Math.max(1,2) );
Arithmetic Operators • + , - , * • / and % • These 2 operations are usually used when dealing with getting the digits of a number • Type promotions
Type Promotions 1 / 2 = 0 ! 4 / 2.0 = 2.0 not 2
Casting Compilation Error! • (<data type>) <expression> • Q. What do you think is the output of public static void main(String args[]){ int a = 10; int b = (int) a / 2.0; System.out.println(b);}
Increment & Decrement Operators Q.What is the output of public static void main(String args[]){ int a = 1; System.out.println(a++); System.out.println(a); } 1 2
Increment & Decrement Operators Q.What is the output of public static void main(String args[]){ int a = 1; System.out.println((a++) + (a++)); System.out.println(a); } 3 3
Increment & Decrement Operators Q.What is the output of public static void main(String args[]){ int a = 1; System.out.println((a++) + (a++)); System.out.println(a); } A 1 2 3 1 2 3 3
Boolean Operations Precedence Order • && , ||, ! • Expressions with these operators are evaluated by “short-circuit evaluation” • Or lazy evaluationi.e. expression not evaluation if we already know the results • e.gtrue || false (true or anything) -> true, so no point evaluating the 2nd part
Boolean Operations • more e.gfalse && true (false and anything) -> false, so no point evaluating the 2nd part
Relational Operators • > , < • Op= operators>=, <= , ==, !=
Assignment Operator • a= 1 + 3 • Step by Step: • a= • (we now need to find a value which will be given to the LHS) • a = 4
Assignment Operator • Try out withint a = 1;a = (a + (a = (a + 1))); • a = (1 + (a = (1 + 1))); • a = (1 + (a = 2)); //assign 2 to a and return value of a • a = (1 + 2); • a = 3;