140 likes | 148 Views
CS110- Lecture 10 Mar 9, 2005. Agenda Practice Session (classes and loops) JUnit Testing Practice Session (testing). Practice Session (Extra Credit).
E N D
CS110- Lecture 10Mar 9, 2005 • Agenda • Practice Session (classes and loops) • JUnit Testing • Practice Session (testing) CS110-Spring 2005, Lecture 10
Practice Session (Extra Credit) • Design and implement a class called Circle that contains instance data that represents the circle’s 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. CS110-Spring 2005, Lecture 10
Practice Session (Extra Credit) • Design and implement a class called Person that contains instance data that represents the person’s 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 Personobjects. CS110-Spring 2005, Lecture 10
Practice Session (Extra Credit) • Design and implement a class called Bookthat contains instance data that represents the book’s 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 whosemain method instantiates and update several Bookobjects. CS110-Spring 2005, Lecture 10
Practice 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. CS110-Spring 2005, Lecture 10
Practice 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 doesn’t return anything. CS110-Spring 2005, Lecture 10
Practice Session Class Name Point x : double y : double Data getX() : double getY(): double setX (double x1) : void setY (double y1) : void Methods CS110-Spring 2005, Lecture 10
Testing • 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. CS110-Spring 2005, Lecture 10
Unit 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. CS110-Spring 2005, Lecture 10
Unit 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. CS110-Spring 2005, Lecture 10
JUnit 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. CS110-Spring 2005, Lecture 10
Template of a typical JUnit test program public classTestX 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 CS110-Spring 2005, Lecture 10
Test class for Die.java public classTestDie 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(expectedReturn,actualReturn); } } CS110-Spring 2005, Lecture 10
JUnit features • JUnit features include: • Assertions for testing expected results • Test fixtures (setUp and tearDown) for sharing common test data. • Graphical and textual test runners CS110-Spring 2005, Lecture 10