1 / 35

CS1101 Group1

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.

dorismoore
Download Presentation

CS1101 Group1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1101 Group1 Discussion 1 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101

  2. Programming background?

  3. 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

  4. 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

  5. Questions from lectures?

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

  7. 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

  8. Java Programming Style / Convention

  9. 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;

  10. 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

  11. CS1101 Labs

  12. 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!)

  13. 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(), …

  14. Learn how to use the Java API • Scanner • Import details : java.util.Scanner • Constructornew Scanner(…); • Methodsreturn type and method nameint : nextInt();String : nextLine(); • Math

  15. Numerical Data int - 32 bit 231 + 1 + (231 - 1) = 232 231 = -2147483648231 - 1 = -2147483647

  16. 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

  17. Things you should know by now…

  18. 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

  19. Reserved Words • Reserved words cannot be used as identifiers (variables names)

  20. 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

  21. 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

  22. 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) );

  23. Arithmetic Operators • + , - , * • / and % • These 2 operations are usually used when dealing with getting the digits of a number • Type promotions

  24. Type Promotions 1 / 2 = 0 ! 4 / 2.0 = 2.0 not 2

  25. 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);}

  26. 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

  27. 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

  28. 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

  29. 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

  30. Boolean Operations • more e.gfalse && true (false and anything) -> false, so no point evaluating the 2nd part

  31. Relational Operators • > , < • Op= operators>=, <= , ==, !=

  32. 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

  33. 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;

  34. Precedence and Associativity

More Related