CSE116 / CSE504 Introduction to Computer Science II Dr. Carl Alphonce

advertisement
CSE116 / CSE504
Introduction to Computer Science II
Dr. Carl Alphonce
343 Davis Hall
alphonce@buffalo.edu
Office hours:
Thursday 12:00 PM – 2:00 PM
Friday 8:30 AM – 10:30 AM
OR request appointment via e-mail
PROFESSIONALISM
Turn off and put away electronics:
cell phones
pagers
laptops
tablets
etc.
© Dr. Carl Alphonce
ANNOUNCEMENTS
Recitations
No recitations this week.
Clicker questions
Not for credit this week.
They will count starting next week.
Sign up for Piazza
piazza.com/buffalo/spring2016/cse116
Practice problems
Posted on website over weekend.
ROADMAP
Today
Test-Driven Development (TDD)
Writing tests
Coming up
Writing tests (continued)
Running Junit
Writing implementation code
a void parameterless method
Writing a test
@Test annotation
defines test data, expected (correct) answer,
computes actual answer
compares expected and actual using assertTrue
© Dr. Carl Alphonce
Sample test method
@Test public void sampleTestMethod() {
ArrayList<String> list = new ArrayList<String>();
list.add("Fred");
list.add("Wilma");
int expected = 2;
int actual = list.size();
assertTrue("I expected the size to be " +
expected + " but it was " + actual,
actual == expected);
}
© Dr. Carl Alphonce
Test Driven Development (TDD)
write tests before code
think of test cases before writing code
make sure tests are a reflection of WHAT code should do
test must NOT be a reflection of HOW code solves the
problem
Test Driven Development (TDD)
tests are executable specifications
Basic structure of test
For a given computation:
determine the expected value
the value which should be produced if the software
is working correctly
determine the actual value
the value which the software is actually computing
compare the two:
if they agree, the test passes
if they disagree, the test fails
group exercise
Come up with test cases to verify the
functionality of a method that accepts one
String as argument (call it ‘input’) and which
returns another String which is the reverse of
‘input’.
We will assume that uppercase and
lowercase letters are distinct.
channel 1
WhichofthefollowingisNOTapassingtestcase
(input-outputpair)forreversingaString
A.
B.
C.
D.
E.
F.
“abc”&“cba”
“”&“”
“aaa”&“aaa”
“Dad”&“Dad”
“Dad”&“daD”
“13#$%”&“%$#31”
WhichofthefollowingisNOTapassingtestcase
(input-outputpair)forreversingaString
A.
B.
C.
D.
E.
F.
“abc”&“cba”
“”&“”
“aaa”&“aaa”
“Dad”&“Dad”
“Dad”&“daD”
“13#$%”&“%$#31”
WhichofthefollowingisNOTapassingtestcase
(input-outputpair)forreversingaString
A.
B.
C.
D.
E.
F.
“abc”&“cba”
“”&“”
“aaa”&“aaa”
“Dad”&“Dad”
“Dad”&“daD”
“13#$%”&“%$#31”
Explanation
WhichofthefollowingisNOTacorrecttest
case(input-outputpair)forreversingaString
Capitalization matters: ‘D’ and ‘d’ are
different characters.
The reverse of “Dad” is therefore “daD”, for
the same reason that the reverse of “@$*” is
“*$@”.
exercise
Define the method to reverse a String.
Work with your neighbors to solve this
problem.
HINTS
String objects are immutable
charAt(int) à char
length() à int
Download