Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

16
Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1

Transcript of Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

Page 1: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

1

Selection and Testing

Shirley MooreCS 1401 Spring 2013

February 21 and 26, 2013

Page 2: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

2

Agenda• Feb 21

– Announcements (5-10 min)– Concepts of if statements and Boolean expressions (20 min)– Checkpoint questions (30 min)– Test plans (15 min)– Wrapup (5 min)

• Feb 26– Announcements (5 min)– Quiz 3 (15 min)– Programming exercises 3.1, 3.8 (20 min)– Switch statement (10 min)– Testing video clips and discussion (20 min)– Research projects ( 5 min)– Wrapup, assignment for next time (5 min)

Page 3: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

3

Announcements

• Seminar today at 1:30: Eduard Dragut, CyberShare Viz Lab, Classroom Building 4th floor

• Exam 1 IPO Algorithm/Programming retest• Unit 2 Outcomes and Schedule• New due dates for Lab 3 and Lab 4

Page 4: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

4

Checking for Valid Program Input

• Why should you do it?• What is the logic?– Example: Program to input base radius and length

of a cylinder and compute the volume • How can we implement the logic?– If statement• condition(s) to test

– relational operators <, >, <=, >=, ==, !=– logical operators &&, ||, !

• actions to take depending on value of condition

Page 5: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

5

Boolean Data Type and Expressions

• Can have values true and false• Examples (can type in Dr Java Interactions)

3 < 5-3 < -5 ‘a’ < ‘b’(3 < 5) && (8 > 10)(3 < 5 ) || (8 > 10)double radius = -0.5;radius >= 0.0boolean checkRadius = (radius >= 0.0);checkRadius

Page 6: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

6

If Statement Syntax• if (boolean-expression) { statement(s) }• if (boolean-expression) { statements(s) // for the true case } else { statement(s) // for the false case }

• Can be nested• Class exercise: Use if-else to add input checking to cylinder problem

Page 7: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

7

CheckPoint Questions• p. 92, 3.8 if ( x > 2 ) { if (y > 2) { z = x + y; System.out.println(“z is “ + z); } } else { System.out.println(“x is “ + x); }• Test inputs

o x = 3, y = 2o x = 3, y = 4o x = 2, y = 2

Page 8: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

8

Checkpoint Questions (cont.)• p. 92, 3.9 if (x > 2) if (y > 2) { z = x + y; System.out.println(“z is “ + z); } else System.out.println(“x is “ + x);• Test inputs

o x = 2, y = 3o x = 3, y = 2o x = 3, y = 3

Page 9: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

9

Checkpoint Questions (cont.)• p. 92, 3.10 if (score >= 60.0) grade = ‘D’; else if (score >= 70.0) grade = ‘C’; else if (score >= 80.0) grade = ‘B’; else if (score >= 90.0) grade = ‘A’; else grade = ‘F’;• What do you think was the intent of the above code? Does it accomplish

this intent? If not, how can you fix it?

Page 10: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

10

Programming Exercises

• p. 121, 3.1 Write a program that inputs real values a, b, and c for the coefficients of a quadratic equation and outputs the result based on the discriminant. If the discriminant is positive, output two roots. If the discriminant is 0, output one root. If the discriminant is negative, output that the equation has no real roots.

Page 11: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

11

Programming Exercises (cont.)

• p. 123, exercise 3.8 Write a program that inputs three integers into variables num1, num2, num3 and outputs the integers sorted from least to greatest.

Page 12: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

12

Switch Statement• Syntax:switch (expression) { case constant-1: statements-1 break; case constant-2: statements-2 break; . . // (more cases) . case constant-N: statements-N break; default: // optional default case statements-(N+1)} // end of switch statement

• Discuss semantics

Page 13: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

13

In-class Exercise• The commission rate for a sale at a shop varies depending on

the sales class of the item, as given in the table below. Any other sales class besides those listed has zero commission. Write a program that inputs a price and sales class for an item and outputs the commission that the sales person will earn for selling that item. Use a switch statement to select the commission rate. Write a step-by-step algorithm before writing the ajava code.

Sales Class

Commission Rate

1 2 %

2 3.5%

2 5%

4 6%

Page 14: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

14

Testing Your Program

• Developing a test plan• Video clips– The Machine That Changed the World, Part 5

42:00-52:00– Tacoma Bridge disaster• http://science.howstuffworks.com/engineering/civil/

bridge10.htm

Page 15: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

15

Research mini-Projects

• History of computing• Using programming to teach math• Software reliability• More topics to come (feel free to suggest

topics in which you are interested!)

Page 16: Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26, 2013 1.

16

Assignment for Thursday, Feb 28

• Do Chapter 3 end-of-chapter programming exercises 3.19, 3.20, 3.22, 3.29. Follow steps discussed in class. Hand-write neatly or print out to turn in and/or discuss at beginning of class.

• Read Chapter 4 sections 4.1-4.7. Write down any questions you have. Write answers to CheckPoint questions.