Sei sulla pagina 1di 2

Questions for Chapter 9

1. Black box testing


Someone once said that a national lottery is a tax on people who are bad at maths. Suppose we want to test this with a
program that simulates a lottery game. It should ask the user for 6 numbers and how many times they wish to enter. If
they enter invalid numbers it should not accept them. Once they enter the numbers it will enter them each week for the
number of times specified. It will then return the total prize money and the total cost (1 pound per entry)

The prize money is assumed to be

all 6 match - 5000000 pounds


5 match + bonus - 100000 pounds
5 match but not bonus - 1000 pounds
4 match - 50 pounds
3 match - 10 pounds

The program works by randomly generating the winning numbers. This makes testing difficult. One way around this is
to test part of the program separately. We could make a test version of the program which always has the winning
numbers as 1,2,3,4,5,6,7.

Create in a table which lists the test cases, the test data, the expected output and the actual output for the lottery
simulation program.

2. White Box Testing.

Here is some java code that reads in daily data for rainfall, average windspeed and hours of sun for a number of days
and prints out summary statistics. Use a program flow graph to show the paths through the program and to calculate the
cyclomatic complexity. This should determine the number of independent paths. Now make up some data to test each
path.

import java.io.*;
import java.text.*;

public class Weather{

private static BufferedReader keyb = new BufferedReader(


new InputStreamReader(System.in));

public static void getWeatherStats(int NumberOfDays)


{ int i,j;
int wData[][] = new int[NumberOfDays][3]; //2-D array for the weather data
String wType[] = {"Rainfall", "WindSpeed", "Hours of Sun"}; //1-D array for type of data

//read in the data


for(i=0; i<NumberOfDays; i++)
{ for(j=0; j<3; j++)
{ System.out.print("Enter " + wType[j] + " for day " + i + ": ");
try{
wData[i][j] = Integer.parseInt(keyb.readLine());
}

catch (IOException e){


System.out.println("!! Error trying to read an int - zero returned !!");
wData[i][j] = 0;
}

}//for j
System.out.println();
}//for i

//menu
int choice=0;

while (choice == 0)
{ System.out.println("Stats for");
System.out.println("\t1. Rain");
System.out.println("\t2. Wind");
System.out.println("\t3. Sun\n");
System.out.println("\tChoice: ");

try{
choice = Integer.parseInt(keyb.readLine());
}

catch (IOException e){


System.out.println("!! Choose between 1 and 3 please !!");
choice = 0;
}

//reports
int total = 0;
switch (choice)
{ case 1: //average rain
for(i=0; i<NumberOfDays; i++)
{ total = total + wData[i][0];
}//for i
System.out.println("\nAverage Rain = " + total/NumberOfDays);
break;
case 2: //average wind
for(i=0; i<NumberOfDays; i++)
{ total = total + wData[i][1];
}//for i
System.out.println("\nAverage WindSpeed = " + total/NumberOfDays);
break;
case 3://average sun
for(i=0; i<NumberOfDays; i++)
{ total = total + wData[i][2];
}//for i
System.out.println("\nAverage Hours Sun = " + total/NumberOfDays);
break;
default: choice = 0;
}//switch
}//while
System.out.println();
}//main
}//Weather

Potrebbero piacerti anche