Transcript Week4

CS1101: Programming Methodology
http://www.comp.nus.edu.sg/~cs1101/
Week 4: OOP Basics (cont’d) and
Selection Statements
 Last week:
 Chapter 3: Numerical Data
 Chapter 4: Defining Your Own Classes – Part 1
 This week:
 Take-home lab #2
 Chapter 4: Defining Your Own Classes – Part 1 (cont’d)
 Chapter 5: Selection Statements
 Next week:
 Chapter 5: Selection Statements (cont’d)
 Chapter 6: Repetition Statements
© CS1101 (AY2009-2010 Semester 1)
Week4 - 2
It Pays to Explore!

Have you been exploring on your own?




Don’t forget to ask questions in class/IVLE forums!
We don’t like such questions 




Writing simple programs (lots of them) on your own
Reading up the API specification pages
Can you tell me what’s the output of this program?
What is wrong with my program?
My friend told me this is the way to do it. Is he correct?
We like such questions 


I’ve tried this program. The result is different from what I
expected, which is … I was surprised! But why didn’t it give
my expected results?
I surfed the Internet and found this. I tried it out but I don’t
quite understand how it works. Can you explain it?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 3
Class and Instance Data Members

Recall in Chapter 1 slides 11-14, we introduced the
class and instance data values.



Class data values are common to all instances (objects) of
that that class
Instance data values are owned by individual instances
(objects), hence they may be different for different objects
We like such questions 


I’ve tried this program. The result is different from what I
expected, which is … I was surprised! But why didn’t it give
my expected results?
I surfed the Internet and found this. I tried it out but I don’t
quite understand how it works. Can you explain it?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 4
Defining Your Own Classes




Any questions on Chapter 4?
Have you done the Quick Check questions?
Name the basic components usually present
in a simple class
Now, think about this class Ball

What data members would you like to include?




Let’s have a few instance data members, and one class data
member
What methods would you have to include?
Now, write Ball.java and an application program
TestBall.java to test it out.
Keep the programs as we will refer to them some other time.
© CS1101 (AY2009-2010 Semester 1)
Week4 - 5
Take-home lab assignment #2


It has been released. Deadline is next
Monday, 7 September 2009, 23:59hr.
Any questions?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 6
Chapter 5 Selection Statements

We learned in week 1 about control structures




Sequence
Selection
Repetition
The syntax for selection statements (‘if’,
‘switch’) is very simple. More importantly:


How to use them elegantly (eg: Thomas Wu’s
slide 13)
How to write them neatly (eg: Thomas Wu’s slides
14 – 15)
© CS1101 (AY2009-2010 Semester 1)
Week4 - 7
Conditional operator ? :


(This is not in the book.)
Syntax:
condition ? expression1 : expression2

Examples:
int x = a>b ? a : b;
System.out.print("John met " + num);
System.out.println(num>1 ? " people" : " person");

You may add parenthesis to make it more
readable:

Example:
double f = (a*b > c) ? (2 * d) : (e – f);
© CS1101 (AY2009-2010 Semester 1)
Week4 - 8
Strange behaviour


Download Strange.java and compile it.
You get this compilation error



“variable answer might not have been initialized”
Why do you think the compiler issues this
error?
How do you correct the program?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 9
Exercise #1 (1/2)
 Given this code (FindMaxV1.java) to find the
maximum among 3 integer values in variables num1,
num2 and num3:
// To find the maximum among 3 integer
// values in variables num1, num2, num3.
int max = 0;
if (num1 > max)
max = num1;
else if (num2 > max)
max = num2;
else if (num3 > max)
max = num3;
 What’s wrong with this code?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 10
Exercise #1 (2/2)
 Another version (FindMaxV2.java):
// To find the maximum among 3 integer
// values in variables num1, num2, num3.
int max = 0;
if (num1 > num2 && num1 > num3)
max = num1;
if (num2 > num1 && num2 > num3)
max = num2;
if (num3 > num1 && num3 > num2)
max = num3;
 What’s wrong with this code?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 11
Exercise #2



Given this code IfAndSwitch.java
Correct the errors in the program
Change the ‘if’ statement into a ‘switch’ statement.
© CS1101 (AY2009-2010 Semester 1)
Week4 - 12
Exercise #3 (Prep for Exercise #4)

Given a 6-digit integer, extract the individual digits
from the integer.






Sample run:
Enter a 6-digit number: 850425
Digits are: 8,5,0,4,2,5
You should use int variables for the 6 digits (digit1, digit2,
…, digit6)
The input (6-digit integer) should be read in as a single
integer.
Do not use ‘if’ statement.
Do not use String methods to solve this problem.
Download Extract6Digits.java and complete the
program.
© CS1101 (AY2009-2010 Semester 1)
Week4 - 13
Exercise #4 (1/2)

Algorithm for Matriculation check code

Matriculation number consists of 6 digits (ignore
the ‘U’ in front)


Step 1: Multiply the 2nd through 6th digits with their
corresponding weights 2, 6, 2, 4, 1 and sum them
up.


Eg: U097325
Eg: 92 + 76 + 32 + 24 + 51 = 18+42+6+8+5 = 79
Step 2: Divide step 1 result by 13 to obtain the
remainder.

Eg: 79 % 13 = 1
© CS1101 (AY2009-2010 Semester 1)
Week4 - 14
Exercise #4 (2/2)

Algorithm for Matriculation check code (cont’d)

Step 3: Subtract step 2 result from 13.


Step 4: Match step 3 result in this table for the
check code.
1
2
3
4
5
6
7
8
9
M
B
N
A
R
E
U
H
W



Eg: 13 – 1 = 12
10 11 12 13
J
X
L
Y
Eg: The check code corresponding to 12 is ‘L’.
Therefore, the check code for U097325 is ‘L’.
Write a program CheckMatric.java
© CS1101 (AY2009-2010 Semester 1)
Week4 - 15
Comparing Objects

Refer to Thomas Wu’s slides

What is the difference between == and .equals()?
© CS1101 (AY2009-2010 Semester 1)
Week4 - 16
Comparing Real Numbers



Real numbers, especially computed ones, are not
represented accurately in computers. (Why?)
Test out the program ExploreRealNumbers.java on
some data.
 What are the inputs where value2 is different
from value1?
To cater to such inaccuracy, we sometimes allow
some ‘tolerance’ in the difference.


Instead of using == to compare, we may conclude that 2
values are “equal” if they are close enough
See ExploreRealNumbersV2.java
© CS1101 (AY2009-2010 Semester 1)
Week4 - 17
Naming Boolean Variables


Good Boolean variable names make the code
easier to understand
Are these good names of Boolean variables?




boolean1
flag
isNotInvisible
Are these good names of Boolean variables?



isValid
isPrime
toContinue
© CS1101 (AY2009-2010 Semester 1)
Week4 - 18
Exercise #5

A year is a leap year if



Examples of leap year:


2004, 1980, 2000, 2400
Examples of non-leap year:


It is divisible by 4 but not by 100; or
It is divisible by 400
1997, 2001, 2006, 2100, 2200, 2300
Download LeapYear.java and correct it
© CS1101 (AY2009-2010 Semester 1)
Week4 - 19
Summary for Today

Creating you own class






Selection statements




‘if’ and ‘if-else’
Nested ‘if’
‘switch’, ‘break’
Comparing objects


Data members
Constructor
Accessor
Mutator
Others…
== versus .equals()
Comparing real numbers
© CS1101 (AY2009-2010 Semester 1)
Week4 - 20
Announcements/Things-to-do (1/2)

Take-home lab #2 deadline: 7 September 2009,
Monday, 23:59hr.

Complete the following



Do Quick Check questions in Chapter 5. Check out the
answers (on CS1101 website) yourself.
Exercise 4: CheckMatric.java
Exercise 5: LeapYear.java
© CS1101 (AY2009-2010 Semester 1)
Week4 - 21
Announcements/Things-to-do (2/2)

To prepare for next week’s lecture:


Read Chapter 6 and its PowerPoint file before you come for
lecture.
To prepare for this week’s discussion session:

Download “week4_discussion_qns.pdf” from module
website, “CA – Discussion”.

Do the questions before you attend your discussion session.
© CS1101 (AY2009-2010 Semester 1)
Week4 - 22
End of File
© CS1101 (AY2009-2010 Semester 1)
Week4 - 23