Title: CS110 Lecture 10 Mar 9, 2005
1CS110- Lecture 10Mar 9, 2005
- Agenda
- Practice Session (classes and loops)
- JUnit Testing
- Practice Session (testing)
2Practice Session (Extra Credit)
- Design and implement a class called Circle that
contains instance data that represents the
circles radius. Define the Circle constructor to
accept and initialize the radius, and include
getter and setter methods for the radius. Include
method area that calculates and return the area
and another method perimeter that calculates and
return the perimeter.
3Practice Session (Extra Credit)
- Design and implement a class called Person that
contains instance data that represents the
persons name and age. Define the Person
constructor to accept and initialize the name and
age, and include getter and setter methods for
the name and age. - Create a driver class called PersonClient whose
main method instantiates and update several
Person objects.
4Practice Session (Extra Credit)
- Design and implement a class called Book that
contains instance data that represents the books
title, author, publisher, edition and isbn
number. Define the Book constructor to accept and
initialize the instance data, and include getter
and setter methods for all instance data. - Create a driver class called BookShelf whose main
method instantiates and update several Book
objects.
5Practice Session (Page 277)
- 5.17 Write a for loop to print the odd numbers
from 1 to 99. - 5.18 Write a for loop to print the multiples of
3 from 300 down to 3. - 5.19 Write a code fragment that reads 10 integer
values from the user and prints the highest value
entered. - 5.20 Write a code fragment that determines and
prints the number of times the character a
appears in a String object called name. - 5.21 Write a code fragment that prints the
characters stored in a String object called str
backward.
6Practice Session (Page 277)
- 5.22 Write a code fragment that prints every
other character in a String object called word
starting from the first character. - 5.23 Write a method called powersOfTwo that
prints the first 10 powers of 2 (starting with
2). The method takes no parameter and doesnt
return anything.
7Practice Session
Class Name
Point
x double y double
Data
getX() double getY() double setX (double x1)
void setY (double y1) void
Methods
8Testing
- Unit Testing To test each unit, or component,
independently, before the units are integrated
into the whole system. - Integration and system testing To integrate all
the components of a system and to test the system
as a whole.
9Unit Testing
- Unit testing is important because
- For the whole system to function properly, each
of its components, or units, must function
properly individually. - Units are much smaller than the whole system, it
is much easier to find the errors in each unit
individually. - Good unit testing will reduce the effort and cost
required for integration significantly.
10Unit Testing
- In OOP the unit for unit testing is often a
single class. - Old style of testing Carry out tests in main
method. - New style of testing Write separate test classes
for testing purposes only.
11JUnit Testing
- When dealing with large set of test cases it is
usually more convenient to use a unit testing
tool. - JUnit is a flexible, easy to use, and open source
unit testing tool for java programs.
12Template of a typical JUnit test program
public class TestX extends TestCase // X is the
class // to be tested private X x
null protected void setUp() throws Exception
x new X() protected void
tearDown() throws Exception x null
//All test methods are public/protected not
private public void testCase1()
//test and compare results public void
testCase2() //test and compare results
setUp is called before every testCase is executed
tearDown is called after every testCase is
executed
Name of test methods starts with test
13Test class for Die.java
public class TestDie extends TestCase //Die is
the class //to be tested private
Die die null protected void setUp() throws
Exception die new Die() protected
void tearDown() throws Exception die
null public void testGetFaceValue()
//test and compare results int
expectedReturn 1 int actualReturn
die.getFaceValue() assertEquals(expectedRetu
rn,actualReturn)
14JUnit features
- JUnit features include
- Assertions for testing expected results
- Test fixtures (setUp and tearDown) for sharing
common test data. - Graphical and textual test runners