CS1102 Tutorial - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS1102 Tutorial

Description:

a.setName('Fred'); b.setName('Peter'); changeName_1(a) ... a.name = 'Fred', b.name = 'David' changeName_2(a); public static void changeName_2(Person a) ... – PowerPoint PPT presentation

Number of Views:134
Avg rating:3.0/5.0
Slides: 31
Provided by: soc128
Category:
Tags: cs1102 | fred | tutorial

less

Transcript and Presenter's Notes

Title: CS1102 Tutorial


1
CS1102 Tutorial
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364

2
about myself
  • Graduated last year in Computer Science
  • First year PhD candidate
  • Research in Protein Sequencing
  • Takes 1 module in addition to CS1102
  • Just a student like yourself! (No staff
    privileges)
  • So, please dont call me Mr. Tan, call me Max

3
about myself
  • Consultation hours Wed 10 12 in my office s15
    03-07
  • Or you can make an appointment with me via email
  • You may download tutorials, answers and this
    slides at a link from CS1102 course from
    http//www.comp.nus.edu.sg/tanhuiyi/
  • Your turn to introduce!

4
about cs1102
  • Disclaimer All statements are my own opinions as
    a fresh grad from SoC
  • Probably the 2nd toughest module after cs1231
    (dont worry!)
  • Requires a strong grasp of programming in Java.
    This is not a Java introductory course

5
about cs1102
  • This course is a pre-req to multiple courses in
    Computer science, Computer Engineering and
    Computational Biology
  • Multiple sources for you to seek help! mm, Dr
    Tan, the various lab TAs

6
how to do well for cs1102
  • Attend all lectures.
  • Attempt all tutorials by yourself and attend all
    tutorials
  • Attempt the labs by yourself.
  • For all of the above, do not seek help until you
    have spent a good amount of time thinking through
    problems!
  • Actively participate in forum discussions. Do not
    post algorithms or answers!

7
tutorial format
  • First 15 minutes Brief overview on some
    difficult lecture materials
  • Next 40 minutes Tutorial question presentations
    you talk. I listen.
  • Remaining time further questions

8
CS1102 Tut 1 Java Revisit
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364

9
Question 1
  • Parameter passing in methods
  • Pass by value or Pass by reference?
  • Very important concept that you need to know..
    Used in EVERY aspect of data structures in this
    course

10
Question 1
  • a.setName(Fred)
  • b.setName(Peter)
  • changeName_1(a)
  • public static void changeName_1(Person a)
  • b new Person()
  • b.setName("David")
  • a b
  • //output Fred David

11
Question 1
12
Question 1
13
Question 1
  • a.setName(Fred)
  • b.setName(Peter)
  • changeName_1(a)
  • //a.name Fred, b.name David
  • changeName_2(a)
  • public static void changeName_2(Person a)
  • a.setName("John")
  • //output John David

14
Question 1
15
Question 2
  • On exception handling and code design
  • In general you wont be tested much on exceptions
    in this course.
  • How elegant is your program?

16
Question 2
  • Where did you throw the exception?
  • You may try to throw the exception in the code
    before you instantiate the class but what happens
    if you have to instantiate at multiple points of
    the code?
  • Better to throw exception at the constructor!

17
Question 2
  • class IdentityCard
  • private int ICNum
  • private String name
  • public IdentityCard(String ICNum, String name)
    throws InvalidICNumberException
  • if(IsInvalidNumber)
  • throw new InvalidICNumberException("Invalid IC
    Number.")
  • try
  • this.ICNum Integer.parseInt(ICNum.substring(1
    ,8))
  • catch(Exception e)
  • throw new InvalidICNumberException("Non valid
    numerical.")
  • this.name name

18
Question 2
  • How did you check for an invalid number?
  • Starts with character s then 7 digits and
    another character.
  • How did you check for the last character ?

19
Question 2
  • Very bad method Use a switch statement for all
    26 letters
  • OK method Convert the character to its integer
    representative and check if it falls between
    integer represented by a and z
  • Smart method Use Java API
  • Character.isDigit(char c)

20
Question 3
  • On Generic Java Programming
  • No emphasis on this in CS1102 either but you need
    to know
  • Coursemarker do not like this warning message and
    will fail your test cases if you have them

21
Question 3
  • import java.util.
  • class TestArrayList
  • public static void main(Stringargs)
  • ArrayListltStringgt v new ArrayListltStringgt()
  • v.add("Hello world")

22
Question 4
  • On method overloading and method overriding
  • You will be doing this a lot in this course

23
Question 4
  • class ContractEmployee extends Employee
  • //Some variables and methods here
  • //No parameter, indicates fixed monthly pay
  • public double computePay()
  • //With parameter, indicates hourly pay
  • public double computePay(int hoursWorked)
  • computePay is overloaded!
  • (Two methods with same name, different
    parameters).

24
Question 4
  • Why method overloading ?
  • Consider the sort method which sort indices from
    a start index to a end index in your ADT
    sort(int start, int end)
  • You also want a sort method that sort all
    elements in your ADT sort()
  • You can simply write the sort method which calls
    the other sort method with two fixed variables!

25
Question 4
  • Example
  • void sort(int start, int end)
  • void sort()
  • sort(0, list.Length() - 1 )
  • Convenience and clarity!

26
Question 4
  • abstract class Employee
  • String name
  • public abstract double computePay(int
    hoursWorked)
  • class FullTimeEmployee extends Employee
  • //Some variables and methods here
  • public double computePay(int hoursWorked)
    //
  • Overridden!

27
Question 4
  • Why method overriding?
  • Subclasses may perform some actions differently
    from super classes!
  • For example if we have a parent class called
    quadrilateral which denotes a set of shapes with
    4 sides and a child class called Square, we want
    to implement the getArea() method differently in
    the child (Square) class!

28
Question 4
  • Can you instantiate an Employee object?
  • No! Employee is an abstract class!
  • When we try to set Employee e as a
    ContractEmployee, we call this polymorphism.
  • Employee e new ContractEmployee()

29
Question 4
  • When we try to call the function
    computePay((int)i) as follows
  • e.computePay(i)
  • At execution time, e is determined to be a
    ContractEmployee object and the method
    implemented by ContractEmployee is used.

30
Q n A
  • Any questions ?
Write a Comment
User Comments (0)
About PowerShow.com