intro to java week 12 (slides courtesy of charatan & kans, chapter 8)
DESCRIPTION
Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8). Software Quality. Learning objectives. By end of this lecture you should be able to:. document your code so that it is easy to maintain; distinguish between compile-time errors and run-time errors; - PowerPoint PPT PresentationTRANSCRIPT
1
Intro to Java
Week 12(Slides courtesy of Charatan & Kans, chapter 8)
2
Software Quality
Learning objectives
By end of this lecture you should be able to:
document your code so that it is easy to maintain; distinguish between compile-time errors and run-time
errors; test a program using the strategies of unit testing and
integration testing; generate test data using the strategies of black box
testing, white box testing and stress testing; document your test results professionally using a test
log; explain the meaning of a Java program throwing an
exception; format your output to improve the usability of your
programs.
3
Measuring quality
There are many desirable features of a piece of software.
We will concentrate on the following:
maintainability;reliability;robustness;usability.
4
Maintainability
The requirements of an application often change over time.
Maintaining a system refers to the need to update an existing system to reflect these changing requirements.
Code should be designed in such a way as to make these changes easier rather than harder.
5
Documentation
For a software system to be easy to maintain, the software design also needs to be clearly documented.
When developing object-oriented programs, this design documentation should include:
complete class diagrams;clear method definitions (parameter &return types, plus pseudocode when
appropriate).
6
Reliability
When attempting to run a program two kinds of errors could occur:
compile time errors; run-time errors.
7
Compiler errors
8
Testing
Testing can never show the absence of errors, only the presence of them.
The aim therefore of any testing strategy is to uncover these errors.
Two areas of testing:
validation (making sure you are building the
right product); verification (making sure you are building the
product right).
9
Verification
A Java application typically consists of many classes working together.
Testing for such errors will start with a process of
unit testing (testing individual classes)
followed by
integration testing (testing classes that make up an application together).
10
Unit testing
All applications require a class with a main method before they can be run.
Two possibilities
add a main method into the class you aretesting;
writing a separate class especially to contain the
main method. This new class then acts as the
driver for the original class.
11
Dummy classes
Testing the StudentList class requires the Student class to be available.
You can develop your own dummy class in place of the missing class.
// a dummy student class class Student {
// no code in class }
12
Adding dummy methods to dummy classes
public double findStudentAverage (int i){ return student[i-1].calculateAverageMark( );}
class Student // ammended dummy class{ // additional dummy method public double calculateAverageMark() { return 50.5; } }
13
Integration testing
When the individual classes in a program have been tested they can be integrated and tested together.
If compiler errors occur during integration then check the following:
14
All methods called should have an implementation in the receiving class
Student
Student (…)
getName( )
setName(…)
StudentList
x = student[i-].calculateAverageMark( )?
15
Names of method calls should match exactly names of methods in receiving class
Student
double calculateAverageMark( )
StudentList
x = student[i-1].CalculateAverageMark()
?
16
Parameter list of method call should match exactly parameter list of methods in the receiving class
StudentList
x = student[i-1].calculateAverageMark(2)
?
Student
double calculateAverageMark( )
17
The expected type of values returned from the method calls should match the return types of these methods in the receiving class.
Student
double calculateAverageMark( )
StudentList
int x; x = student[i-1].calculateAverageMark();
?
18
Black box testing
component to test
? inputs expected outputs
19
Testing the method getGrade
Student Grades
marks 70 and above grade 'A'
marks in the 60's grade 'B'
marks in the 50's grade 'C'
marks in the 40's grade 'D'
marks in the 30's grade 'E'
marks below 30 grade 'F'
20
Test data produced
grade 'A'
grade 'B'
grade 'C'
grade 'D'
grade 'E'
grade 'F'
"mark too low"
"mark too
high"
79, 64, 55, 46, 33, 25, -40, 120
21
Testing the boundaries
If the code still fails to produce the correct result often the error lies on the boundaries of such equivalent groups.
In this case the following boundary values should all be tested as well as the sample from each equivalent group identified earlier:
-1, 0, 1, 29, 30, 31,39, 40, 41,49, 50, 51,59, 60, 61, 69, 70, 71, 99, 100, 101
22
White box testing
component to test
inputs based on code
expected outputs
topMark = 70;
// more code here
while(!valid)
{
if (mark > topMark)
grade = 'A';
//more code here
else
valid = false;
}
23
The test log
TEST LOG
Purpose: To test the STUDENTLIST class
Run Number: 1 Date: 17th March 2000Action Expected Output Pass/ Fail Reason for failureAdd student ("Ade") message "student
entered"
set mark to 69 no message expected get grade 'B' Add student ("Madhu") message "student
entered"
set mark to 70 no message expected get grade 'A' Displays 'B' instead of 'A'.
Due to error in if statementother tests
24
Robustness
A program is said to crash when it terminates unexpectedly.
A robust program is one that doesn't crash even if it receives unexpected input values.
Generally, whenever a value is received to be processed, it should be checked before processing continues; if an unexpected value could cause the program to crash.
25
A program will crash if an illegal array index is used
PushToLimitSalesStaff
for (int i = 1; i<=3; i++){ System.out.println ("enter sales for employee "+ i); value = EasyIn.getInt(); cars4U.setFigure(i, value); }
SalesStaff
public void setFigure(int numberIn, int valueIn){ staff[numberIn-1] = valueIn; }
'i' set to 3 last time round the loop
an attempt will be made to access index 2
26
Dealing with the problem
public boolean setFigure(int numberIn, int valueIn){ if (numberIn <= staff.length) { staff[numberIn-1] = valueIn; return true; // method succesful } else { return false; // method unsuccessful }}
27
Adapting the driver program
for (int i = 1; i<=3; i++){ System.out.println ("enter sales for employee "+ i); value = EasyIn.getInt(); boolean ok = cars4U.setFigure(i, value); if (!ok) // unable to set figure succesfully { System.out.println ("ERROR:last figure not entered "); } }
28
Usability
The usability of a program refers to the ease with which users of your application can interact with your program.
A user-manual can help make your programs easier to follow.
29
Adding a "HELP" option
*** REACTOR SYSTEM ***
[1] Get current temperature[2] Increase temperature[3] HELP[4] Quit
enter choice [1,2,3,4]: _
30
Escape sequences
Special formatting characters, known as escape sequences, can be added into strings.
Some useful escape sequences
\t add a tab space
\n add a new line
\" add a double quote
\' add a single quote
\\ add a backslash
31
The DecimalFormat class
The DecimalFormat class can be used to format the display of decimal numbers.
This class resides in the java.text package
import java.text.*;
Once you have access to this class you can create DecimalFormat objects in your program.
32
Creating DecimalFormat objects
The DecimalFormat constructor has one parameter, the format string.
DecimalFormat df = new DecimalFormat( "000,000.000");
33
Using a DecimalFormat object
Use the format method of the DecimalFormat class to format a given number:
DecimalFormat df =new DecimalFormat( "000,000.000");
double someNumber = 12345.6789;
System.out.println("before\t" + someNumber);System.out.println("after\t"+ df.format(someNumber));
before 12345.6789after 012,345.679
34
Optional digits
Replacing a zero in a format string with a hash (#) would mean that the digit was optional, not compulsory.
DecimalFormat df = new DecimalFormat( "#00,000.000");
double someNumber = 12345.6789;
System.out.println("before\t" + someNumber);
System.out.println("after\t" + df.format(someNumber));
before 12345.6789after 12,345.679
35
Graphical user interfaces