cs1101 group1 discussion 10 lek hsiang hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

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

Upload: richard-fowler

Post on 19-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

CS1101 Group1

Discussion 10

Lek Hsiang Hui

lekhsian @ comp.nus.edu.sg

http://www.comp.nus.edu.sg/~lekhsian/cs1101

Page 2: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Scope of discussion

• Lab8 : MyString

• Lab9 : Taxi

• PE

• Revision

• Notes about final exam

Page 3: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Lab8 : MyString

• http://www.comp.nus.edu.sg/~lekhsian/d10/MyString.java

• Check out also the implementation of StringBuffer and StringBuilder– E.g.

C:\Program Files\Java\jdk1.6.0_03\src.zip– \java\lang

Page 4: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Lab9 : Taxi

• Use of BufferedReader and StringTokenizer• Actually Scanner provide similar capability too• Code is not modular (i.e. divide your codes into

different functions)– Should have a method for typeA, typeB, typeC– No check for number of parameters

using args.length

• Take note that we are counting in blocks of distance– i.e.

distance 178mwould be considered as a block of 200m

Page 5: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

StringTokenizer

• http://www.comp.nus.edu.sg/~lekhsian/d10/StringTokenizerExample.java

• More importantly, http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

Page 6: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Lab9 : Taxi

• Should be only 1 java file with both classes kept in it

• However when you compile it there will still be 2 class files created

• Of course how you place the classes is quite important, the one with the main method should come first, etc

• http://www.comp.nus.edu.sg/~lekhsian/d10/FareCalculator.java

Page 7: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

PE

• http://www.comp.nus.edu.sg/~lekhsian/d10/

Page 8: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Interface

• A construct that defines the behavior of a different type of Object

public interface CdPlayer{String TYPE = “CD PLAYER”;

//constants (all variables will be implicitly final)public void play(); //empty method bodypublic void stop();

}

• Point to know is that all the methods in an interface are abstract

• CD Player Example

Page 9: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Primitive Data Type– What’s the range?– Declaring them (duh?!)– Comparison (char is compare using ‘ ‘ etc)– Never use float/double in loops

e.g.for (double a = 0 ; a < 1.3 ; a+= 0.1){ … }

– Octal, Decimal, Hexadecimal– float ( exponential case: 15.5E2f)

• Remember you need the ‘f’ if the value is floating value if not it will be treated as type error (double)

Page 10: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Escape characters \t \n \r \\ \’ etc…

• Valid identifier name– Begin with _ $ letter– Followed by _ $ letter number– Case sensitive

• Reserved Words

• Remember your “;” when writing your codes

Page 11: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Operators– Favorite - % /– ++x, x++– Logical operators– && || !

• Short-circuit evaluation– When comparing values (use == not =)

• But for String remember to use .equals()– Op=

• >=, <=, != , =, <, >, %=,+= etc

• Precedence and associativity(refer to table)

• casting

Page 12: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Scanner– next(), nextLine(), nextInt() etc

• if-else– Do you know how to identify a particular else

is for which if when it is badly indented? (dangling else)

• switch– Expression must be char, byte, short, int– Be careful of the break;

Page 13: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• ?:(cond) ? true_return : false_return;

• while, for, do-while (don’t forget the last ; for do-while)– break– continue

• Arrays (1D, 2D arrays)– traversing, revise your labs!– Copying arrays, sort, searching

Page 14: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• ?:(cond) ? true_return : false_return;

• while, for, do-while (don’t forget the last ; for do-while)– break– continue

• Arrays (1D, 2D arrays)– traversing, revise your labs!– Copying arrays, sort, searching

Page 15: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Functions / Procedures

• Local variables?

• Formal parameters / actual parameters

• Return value (remember to return if the function is not void)

• pass by value– Case where you pass an int to a method– Case where you pass int[] into a method etc

Page 16: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Selection Sort

• Bubble Sort

• Linear Search

• Binary Search

• Recursion

Page 17: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Creating Classes– Constructor– Instance & class variables– extends / implements

• Access Rights (public, private, protected)

• Getters & Setters

Page 18: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• String– Comparing String (equals)– charAt … etc

• Math– Random()

• BufferedReader , InputStreamReader, FileReader, PrintWriter, RandomAccessFile

• StringTokenizer• Exceptions

– throw new Exception()– try-catch– throws– What type of exception will be thrown

Page 19: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Revision

• Interface

• Comparable

• Comparator

• Serializable– Why need serializable?

Page 20: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Notes about final exam

• Check out http://www.comp.nus.edu.sg/~chenxia2/

• For suggested answers for some of the past year papers

• Revise all the stuff you did (*including labs*)

Page 21: CS1101 Group1 Discussion 10 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg lekhsian/cs1101

Thank you

All the best for your exams!