CS1101 Group1

Download Report

Transcript CS1101 Group1

CS1101 Group1
Discussion 10
Lek Hsiang Hui
lekhsian @ comp.nus.edu.sg
http://www.comp.nus.edu.sg/~lekhsian/cs1101
Scope of discussion
•
•
•
•
•
Lab8 : MyString
Lab9 : Taxi
PE
Revision
Notes about final exam
Lab8 : MyString
• http://www.comp.nus.edu.sg/~lekhsian/d1
0/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
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 178m
would be considered as a block of 200m
StringTokenizer
• http://www.comp.nus.edu.sg/~lekhsian/d1
0/StringTokenizerExample.java
• More importantly,
http://java.sun.com/javase/6/docs/api/java/
util/StringTokenizer.html
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/d1
0/FareCalculator.java
PE
• http://www.comp.nus.edu.sg/~lekhsian/d1
0/
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 body
public void stop();
}
• Point to know is that all the methods in an
interface are abstract
• CD Player Example
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)
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
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
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;
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
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
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
Revision
•
•
•
•
•
Selection Sort
Bubble Sort
Linear Search
Binary Search
Recursion
Revision
• Creating Classes
– Constructor
– Instance & class variables
– extends / implements
• Access Rights (public, private, protected)
• Getters & Setters
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
Revision
•
•
•
•
Interface
Comparable
Comparator
Serializable
– Why need serializable?
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*)
Thank you
All the best for your exams!