Tutorial 1 C++ Programming

Download Report

Transcript Tutorial 1 C++ Programming

Welcome to CS1102
sem1 08/09 
Notes:
All of my PowerPoint slides will be uploaded to my website after my last tutorial class every week.
http://www.comp.nus.edu.sg/~stevenha/myteaching/
You do not need to write down much. Your attention is more important!
This is not the official solution. The official solution will be uploaded by Dr Tan to IVLE workbin weekly.
1
About Me
• Name: Steven Halim
– Indonesian Chinese
• 5th year (err…) PhD candidate @ SoC, NUS
• Former hobby: Programming/Studying Data Structures or Algorithms
– Now this hobby is turned into: Profession
• Teaching Assistant for CS1102, sem1 08/09, class T3, T4, T5, and T6
• Very important website  side by side with CS1102 IVLE:
– http://www.comp.nus.edu.sg/~stevenha/myteaching/
• For extra consultation outside this classroom:
– Timing: preferably Thursday, 1-2pm (before T4) or 5-6pm (after T6)
– Venue: TA Office: COM1-2-46 (just around the corner, call me from outside)
– If you feel that you need help, try to get help as early as possible!
2
About You
• No time for full introduction…
• Let’s just do some quick survey 
– Nationality:
• Indonesian/Singaporean/Malaysian/Chinese/Vietnamese/Philippines/Indian/
Bangladeshi/Pakistani?
• Others?
– Faculty:
• COM/CPE/Science?
• Others?
– Origin:
• Poly/JC/High school in your home country?
• Others?
3
About This Tutorial (1)
• Why you should attend this tutorial?
– Tutorial attendance and participation: 5% (PE: 20%15%)
– Please sign the attendance list.
• Tutorial Attendance
– Minus certain % per absence…
• Tutorial Participation
– For next tutorials, you will BID to answer at least one question.
– We have 9-10 tutorials left, 3-4 questions each, ~ 27-40 questions to grab 
– You can be kiasu for today’s tutorial too, 5 questions to grab 
– Email me ([email protected]), say e.g. “Steven, I want to do T2, Q1”.
– I will confirm your bid based on First-Come-First-Serve basis.
– At the end of the day, everyone gets at least one chance.
– If you have done one presentation, do not bid again until everyone gets a chance.
– Prepare your answer in electronic format (ppt, word), use this PC to present!
– No participation % if you do not try at all!
4
About This Tutorial (2)
• What are we going to do @ each tutorial?
– Review Past Lectures (10-15 minutes)
– Discuss Tutorial Questions (15-35 minutes)
• Student presentations, according to the assignments
– >> Target: finish in 50 minutes… <<
– Individual Q & A (the last 10 minutes)
• If you are too shy to ask during the session, use this time window
• Best: post question in IVLE for others to see! I will occasionally reply there
5
About Java and CS1102
• Java
– Hopefully you are familiar with it by now (revise your CS1101 again + lec 1)…
• CS1102
–
–
–
–
Java is not the only thing in CS1102!
CS1102 is about “Data Structures” and “Algorithms”
Not an easy module 
No guarantee that you will understand the materials after my class
– But let’s try our best …
– Also, read this story later
• http://www.comp.nus.edu.sg/~rtan/articles/RavenEve/
6
Tutorial 1
Java OO Programming
7
Very Quick Review
• Problem Solving and Software Engineering
– These stuffs will going to be revisited in CS2103!
• Java Revision
– Too many too revise… >.<
• 127 slides++ in Lecture 1
– However, make sure that you are familiar with all these:
• Using Java, I/O (keyboard/monitor, file), Class, Class Inheritance, Method
Overriding, Abstract Classes, Polymorphism Exception, Assertion
– We will test some concepts during this tutorial.
– I will address your individual query (if any), after class…
Q1 –Pseudocode: definition+usage
• http://en.wikipedia.org/wiki/Pseudocode
Q2 – Spiral Output (1)
• n is a perfect square if n = i*i; i = sqrt(n); i < n; i  odd integer!
•
– This problem is not well defined for n = 22, 42, 62, etc…
25 24 23 22 21
10 09 08 07 20
11 02 01 06 19
12 03 04 05 18
13 14 15 16 17
• Try to solve this algorithm using “pseudo code”
– Actual Java code is fine, but perhaps quite complex
• “Easiest” solution (other suggestions are welcomed):
– Use 2-D array of integers of size i*i, where i2 = n, initialize the values with all 0.
– Start from center: coordinate (i/2, i/2), insert number 1 with direction up.
– If my left side is 0 (not filled), turn left, fill the next number
else if my left side is not 0 (filled), go forward, fill the next number
– Print the updated 2-D array
Q2 – Spiral Output (2)
• To make the result looks like this (different direction):
•
21 22 23 24 25
20 07 08 09 10
19 06 01 02 11
18 05 04 03 12
17 16 15 14 13
• You just need to change the algorithm to “check your right side”…
• The actual Java implementation may require recursion
– You will learn this in Lecture 5
– My source code is uploaded to my website for those who wants to try
Q3 – Sentence
• Sentence is a string with words
separated by punctuation marks and
ended with a full stop.
– http://en.wikipedia.org/wiki/Punctuation
• Your task: design these classes
– Sentence
• Describe a sentence using “private instance attribute”
• Have an “instance method” for outside world to see the private instance attribute above
• Have “toString()” method, one word per line
– NotSentenceException
• User defined exception class
– TestSentence
• Catching IOException (for BufferedReader only)
• Check if it is a sentence, if not, throw “NotSentenceException”
• If ok, create Sentence object and print it
Q4 – Swapping (1)
• class Swapper {
static void swap(int x, int y) {
int temp = y;
Does not work as expected.
y = x;
Why?
Parameter passing in Java:
x = temp;
Pass by value
}
public static void main(String[] args) {
int a = 1, b = 2;
System.out.println(“a=“ + a + “, b = “ + b);
swap(a, b);
System.out.println(“a=“ + a + “, b = “ + b);
}
}
Q4 – Swapping (2)
• class Swapper {
static void swap(Integer x, Integer y) {
Integer temp = y;
Also does not work as expected.
y = x;
Why?
We are swapping references only! The
x = temp;
actual content is unchanged. Moreover,
}
Integer class is immutable in Java.
public static void main(String[] args) {
int a = 1, b = 2;
System.out.println(“a=“ + a + “, b = “ + b);
swap(a, b);
System.out.println(“a=“ + a + “, b = “ + b);
}
}
Note: swapping is
important in Sorting!
(Lecture 6)
Q4 – Swapping (3)
• So what should you to do to make a and b swap values?
– Dirty but easiest answer:
• Just do the swapping inline, e. g.
• public static void main(String[] args) {
int a = 1, b = 2;
System.out.println(“a=“ + a + “, b = “ + b);
int temp = a;
a = b;
b = temp;
System.out.println(“a=“ + a + “, b = “ + b);
}
Q5 – Base and Derived (1)
•Private are private!
public class Base { // must be saved in Base.java
•Public are public!
public int bPublic;
•Only “protected” needs
protected int bProtect;
special considerations!
private int bPrivate;
}
public class Derived extends Base { // must be saved in Derived.java
public int dPublic;
private int dPrivate;
}
public class Tester { // must be saved in Tester.java
public static void main(String[] args) {
Base b = new Base();
Derived d = new Derived();
System.out.println(b.bPublic + “ “ + b.bProtect + “ “ + b.bPrivate + “ “ +
d.dPublic + “ “ + d.dPrivate);
}
}
That’s All for Today
• See you next week for an even more exciting topics:
– Abstract Data Types (ADT) 
– Using Formula 1 as background story…