CS1102 Tutorial - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

CS1102 Tutorial

Description:

Completed all his modules, but just a student like yourself! ... i personally dislike programming. So this module is just too difficult for me. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 46
Provided by: dcsa
Category:

less

Transcript and Presenter's Notes

Title: CS1102 Tutorial


1
CS1102 Tutorial
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • COM1-01-09 Tel65164364

2
about myself
  • Graduated in 2006 in Computer Science
  • Final Year Masters Student
  • Research in Protein Sequencing
  • Completed all his modules, but just a student
    like yourself!
  • So, please dont call me Mr. Tan or Sir, call me
    Max (Im still young)

3
about myself
  • Consultation hours Tuesday 2 - 4 in my office
    COM1 01-09
  • Or you can make an appointment with me via email
  • You may download tutorials, answers and this
    slides at http//www.comp.nus.edu.sg/tanhuiyi/

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
  • Prereqs A pass in CS1101
  • However, that is highly misleading, the module
    requires a very strong understanding of CS1101.
  • Lectures will assume you can code anything that
    has been taught in CS1101
  • All modules taught in the earlier topics will be
    used in the later topics, and it is assumed that
    you know the earlier topics

6
about cs1102
ADTs
LinkedLists
Queues
Stacks
Hashing
Trees
Graphs
A rough view of how the syllabus is related
Sorting
Complexity
7
about cs1102
  • This is not an easy module
  • Try to catch up, if you lag behind in one topic,
    you will not be able to understand the next
  • Lets look at some past student feedback

8
student feedback
  • Lecturer has put in effort so that we can spend
    reasonable time on lab exercises and also take
    care of other modules. However, the module on the
    whole is still difficult.
  • i personally dislike programming. So this module
    is just too difficult for me.
  • as one of the most tough moudule in SOC, the
    structure of the course is not well
    designed.especally the weekly lab assignment,
    from which hardly anything can be learned. some
    important concepts are not well clarified in
    lectures.
  • This module is known as one of the most diffcult
    modules that offered in SOC. It absolutely
    requires a lot of hard wording.
  • Well it's a nice module. Tough and challenging.
  • This module is too heavy. The cirriculum is
    amazing and the weekly lab assignments are too
    difficult. It takes students so much time to do
    them weekly tha syudents neglect other subjects.
  • More time should be taken to teach this. Too much
    content in too little time.

9
about cs1102
  • Not trying to scare you off
  • Help is at hand!
  • Your two lecturers, Dr Tan and Prof Ling
  • Your three tutors, myself, Hugo, Zhangli

10
CA
  • Labs 20
  • tutorial participation 5
  • Midterm test 20 (closed book)
  • Practical exam 15
  • Exam 40  (closed book)

11
CA
  • tutorial participation 5
  • Ask/Answer questions/chat with me/present
    solution/prove your existence to gain the 5
    marks.
  • At the end of the semester, if I can remember
    your name, you get 5 P

12
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!
  • Make sure you are up to date with all materials,
    if you need some clarifications, look for me

13
how to do well for cs1102
  • If you get lost in the earlier lectures, please
    work to understand and look for me for help.
  • The later portions of the course are heavily
    dependent on the earlier parts.

14
tutorial format
  • First 15 minutes Brief overview by me on some
    difficult lecture materials
  • Next 40 minutes Tutorial question presentations
    you talk. I listen.
  • You will split into 4 groups (for the entire
    semester) and present your solutions as a
    grp/individual (to your preference)
  • Remaining time further questions (usually no
    remaining time)

15
tutorial format
  • Today
  • Group 1 Question 1 and 5
  • Group 2 Question 2 and 6
  • Group 3 Question 3 and 4
  • Group 4 Question 7
  • Next week, Group 1 Q2, Group 2 Q3, Group 3
    Q4, Group 4 Q1 (add 1 to your question number
    every week)

16
CS1102 Tut 1 Java Revisit
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • COM1-01-09 Tel65164364

17
15 minute recap
  • List of List of String
  • ArrayList lt ArrayList ltStringgt gt bigBox new
    ArrayList lt ArrayList ltStringgt gt()
  • How do I access, use, or read the above
    complicated-as-hell statement?
  • ArrayList lt ArrayList ltStringgt gt bigBox
  • Means that this is an ArrayList object, with the
    identifier bigBox
  • Means that the type stored in the ArrayList
    object is a ArrayList ltStringgt

18
15 minute recap
  • Meaning whatever we put into bigBox must be of
    type ArrayList ltStringgt
  • E.g.
  • ArrayList ltStringgt smallBox new
    ArrayListltStringgt()
  • bigBox.add(smallBox) is valid
  • String candy mms
  • bigBox.add(candy) is NOT valid

19
15 minute recap
  • List of List of String
  • Imagine a Box as a List

candy
SmallBox
BigBox lt SmallBoxltcandygt gt ArrayList lt
ArrayListltStringgt gt
BigBox
20
15 minute recap
  • Can you access the candy items from the BigBox ?
  • No! You have to take out the SmallBox first
    before you can access the candy!

candy
SmallBox
SmallBox s bigBox.get(0) Candy c s.get(0)
BigBox
21
q1
  • Let the initial values of x and y be 1 and 2 for
    questions a to e below. What is the value of z
    after the following operations?
  • z x y
  • z x y
  • z x y
  • z x --y
  • z x y

22
q1
  • xmeans add to the value of x BEFORE running
    the operation x appears in
  • xmeans add to the value of x AFTER running the
    operation x appears in
  • Basically y x 1can be rewritten asx
    x1y x 1

23
q1
  • x 1, y 2
  • z x y (3)
  • z x y (4)
  • z x y (4)
  • z x --y (3)
  • z x y (5)

24
q2
  • Methods to mutate and access the private
    variables of a class are called mutators and
    accessors respectively. List the possible
    mutators and accessors of a Triangle class. You
    may assume your own definition of the data fields
    in the class.

25
q2
  • What can you do to a triangle ?
  • Mutators
  • Set the lengths of the sides setLength(int side,
    int length)
  • Set the angles setAngle(int side, int angle)
  • Accessors
  • Get the angles getAngle(int side)
  • Get the lengths getLength(int side)

26
q3 q4
  • What is information hiding and encapsulation? How
    does Java support these concepts?
  • How is code reuse supported in Java?

27
q3 q4
  • Information hiding makes implementation details,
    including components of an object, inaccessible
  • Implemented in Java by private and protected
    keywords
  • Encapsulation is the grouping of data and the
    operations that apply to them to form an
    aggregate while hiding the implementation of the
    aggregate
  • Implemented in Java by classes
  • Code reuse is supported by Java through the use
    of class inheritance (the extends keyword)

28
q5
  • 1 class Person
  • 2
  • 3 public static final int NO_SSN -1
  • 4
  • 5 private int SSN 0
  • 6 String name null
  • 7
  • 8
  • 9 class TestPerson
  • 10
  • 11 private Person p new Person()
  • 12
  • 13 public static void main(String args)
  • 14
  • 15 Person q new Person()
  • 16
  • 17 System.out.println(p)
  • 18 System.out.println(q)
  • 19
  • Line 17 is illegal, even though line 18 is legal.
    Explain why.

29
q5
  • p is declared in the TestPerson class context
  • p does not have a static keyword
  • You cannot access a non-static object/variable in
    a static scope (in this case the main method)
  • BUT you can access a static object in a
    non-static scope!
  • You need to understand that when the static
    keyword is used, regardless of how many instances
    of the object exists, there is only ONE common
    object corresponding to that static variable

30
q5
  • 1 class Person
  • 2
  • 3 public static final int NO_SSN -1
  • 4
  • 5 private int SSN 0
  • 6 String name null
  • 7
  • 8
  • 9 class TestPerson
  • 10
  • 11 private Person p new Person()
  • 12
  • 13 public static void main(String args)
  • 14
  • 15 Person q new Person()
  • 16
  • 17 System.out.println(p)
  • 18 System.out.println(q)
  • 19
  • Which of lines 20 to 24 are legal and which are
    not? Explain why.

31
q5
  • Line 20 and 23 are legal as NO_SSN is a public
    static field which can be accessed via an object
    reference or a class name
  • Line 22 is legal because by default, name is
    visible
  • Line 21 and 24 are illegal because SSN is
    private. Also, Person.SSN is illegal as SSN is
    nonstatic.)

32
q6
  • 1 public class Base
  • 2
  • 3 public int bPublic
  • 4 protected int bProtext
  • 5 private int bPrivate
  • 6 // Public methods follow
  • 7
  • 8
  • 9 public class Derived extends Base
  • 10
  • 11 public int dPublic
  • 12 private int dPrivate
  • 13 // Public methods follow
  • 14
  • 15
  • 16 public class Tester
  • 17
  • 18 public static void main(String args)
  • 19
  • Which accesses are illegal?

33
q6
  • 1 public class Base
  • 2
  • 3 public int bPublic
  • 4 protected int bProtect
  • 5 private int bPrivate
  • 6 // Public methods follow
  • 7
  • 8
  • 9 public class Derived extends Base
  • 10
  • 11 public int dPublic
  • 12 private int dPrivate
  • 13 // Public methods follow
  • 14
  • 15
  • 16 public class Tester
  • 17
  • 18 public static void main(String args)
  • 19
  • Which accesses are illegal?
  • You can only access b.bProtect IF and ONLY IF
    they are in the same package

34
q6
  • 1 public class Base
  • 2
  • 3 public int bPublic
  • 4 protected int bProtect
  • 5 private int bPrivate
  • 6 // Public methods follow
  • 7 public static void main(String args)
  • 8
  • 9 Based b new Base()
  • 10 Derived d new Derived()
  • 11
  • 12 System.out.println(
  • 13 b.bPublic " " b.bProtect " "
  • 14 b.bPrivate " " d.dPublic " "
  • 15 d.dPrivate)
  • 16
  • 17
  • 18
  • 19 public class Derived extends Base

b. Make main a method in Base. Which accesses
are illegal?
35
q6
  • 1 public class Base
  • 2
  • 3 public int bPublic
  • 4 protected int bProtect
  • 5 private int bPrivate
  • 6 // Public methods follow
  • 7 public static void main(String args)
  • 8
  • 9 Based b new Base()
  • 10 Derived d new Derived()
  • 11
  • 12 System.out.println(
  • 13 b.bPublic " " b.bProtect " "
  • 14 b.bPrivate " " d.dPublic " "
  • 15 d.dPrivate)
  • 16
  • 17
  • 18
  • 19 public class Derived extends Base
  • b. Make main a method in Base. Which accesses
    are illegal?
  • Ans Only d.dPrivate is illegal
  • Why can you access b.bPrivate ?
  • Recall in lecture notes -gt
  • Private members can be accessed by methods of the
    class.
  • Now you can definitely access b.bProtect

36
q6
  • 1 public class Base
  • 2
  • 3 public int bPublic
  • 4 protected int bProtect
  • 5 private int bPrivate
  • 6 // Public methods follow
  • 7
  • 8
  • 9 public class Derived extends Base
  • 10
  • 11 public int dPublic
  • 12 private int dPrivate
  • 13 // Public methods follow
  • 14 public static void main(String args)
  • 15
  • 16 Based b new Base()
  • 17 Derived d new Derived()
  • 18
  • 19 System.out.println(

c. Make main a method in Derived. Which
accesses are illegal?
37
q6
  • 1 public class Base
  • 2
  • 3 public int bPublic
  • 4 protected int bProtect
  • 5 private int bPrivate
  • 6 // Public methods follow
  • 7
  • 8
  • 9 public class Derived extends Base
  • 10
  • 11 public int dPublic
  • 12 private int dPrivate
  • 13 // Public methods follow
  • 14 public static void main(String args)
  • 15
  • 16 Based b new Base()
  • 17 Derived d new Derived()
  • 18
  • 19 System.out.println(
  • Make main a method in Derived. Which accesses are
    illegal?
  • Only b.bPrivate is illegal, all others are legal
    as it is not in the same class

38
q6
  • d. How do these answers change if protected is
    removed from protected int bProtect?

39
q6
  • d. How do these answers change if protected is
    removed from protected int bProtect?
  • Ans If the classes are in the same package, then
    it is visible in all cases stated above
    OTHERWISE, first case NO, second and third case
    YES
  • By default, if you do not specify a accessibility
    modifier, it will be package-private i.e. same
    accessibility as protected EXCLUDING subclass

40
q6
  • e. Write a three parameter constructor for Base.
    Then write a five parameter constructor for
    Derived.

41
q6
  • e. Write a three parameter constructor for Base.
    Then write a five parameter constructor for
    Derived.
  • Ans Base(int pub, int pri, int pro)
  • bPublic pub bPrivate pri bProtext pro
  • Derived(int bpub, int bpri, int bpro, int dpub,
    int dpri)
  • super(bpub, bpri, bpro)
  • dPublic dpub dPrivate dpri

42
q6
  • f. The class Derived consists of 5 integers.
    Which are accessible to the class Derived?

43
q6
  • f. The class Derived consists of 5 integers.
    Which are accessible to the class Derived?
  • Ans all except bPrivate as Derived is a subclass
    of Base

44
q6
  • g. A method in the class Derived is passed a Base
    object. Which of the Base object members can the
    Derived class access?

45
q6
  • g. A method in the class Derived is passed a Base
    object. Which of the Base object members can the
    Derived class access?
  • Ans all except bPrivate as Derived is a subclass
    of Base
Write a Comment
User Comments (0)
About PowerShow.com