Testing Example 1

 

Writing a program and getting it to compile does not mean that we have written a program that works correctly. In fact it's extermely rare for anyone to write a program that works correctly the first time. So how do we know if our program is correct? We test it: we run the program with some sample data and check whether the results are correct. If the results are not correct then we need to modify the program.

What data should we use? How many times do we need to do this? It depends on the program, but we always want to run one test for each of the different conditions that might occur in the program.

Formal testing methods require the creation of a test plan, which describes each input to be tested and the expected output for that input. It is a critical step to test all conditions in a program even if a written test plan is not required. Insufficient testing frequently results in a program that doesn't work.

Two main types of testing are "black-box testing" and "white-box testing". The "box" in question is the program code. In black-box testing, we ignore the program code. Instead we look at the specs (like this document or the lab assignment) and test all possibilities it describes. In white-box testing we look at the code and make sure to test all paths in the program. We make sure to have test data that goes through all ifs and all elses, we make sure to run through each loop several times.

Sample Description

Tuition for students at MCC is based on the number of credits taken. It also depends on whether one is a Middlesex County resident or not. In this part of the lab you will write a program to calculate tuition. The rules for tuition charges are:

If you live in Middlesex County, the cost of attending MCC is $103.00 tuition per credit plus $ 34.50 fees per credit.
If you live outside of Middlesex County, the cost of attending MCC is $206.00 tuition per credit plus $ 69.00 fees per credit.

Your program will have the following input:

Your program will print the following:

Black-Box Test Data

Choose test data for both options: M and O.

It's helpful to use numbers that make your calculations easy. That's why I chose 10 credits.

Sample Program

		Scanner keyboard = new Scanner(System.in);

		double inTuition = 103.00;
		double inFees = 34.50;
		double outTuition = 206.00;
		double outFees = 69.00;
		String name;
		String residency;
		char resLetter;
		int credits;
		double tuition;
		double fees;
		double totalCost;

		// Input for name.
		System.out.println("What is your first and last name?");
		name = keyboard.nextLine();

		// Input for residency.
		System.out.println("What county do you live in? Enter M for Middlesex. Enter O for out of county.");
		residency = keyboard.next();
		resLetter = residency.charAt(0);

		// Input for credits.
		System.out.println("How many credits are you taking this semester?");
		credits = keyboard.nextInt();

		// Output displays for name, credits, residency, tuition, fees, and total.
		System.out.println("Name: " + name);
		System.out.println("Number of Credits: " + credits);

		// calculate tuition, fees, and total
		if(resLetter == 'M') {
			tuition = credits * inTuition;
			fees = credits * inFees;
			System.out.println("Residency Status: In County");
		}
		else {
			tuition = credits * outTuition;
			fees = credits * outFees;
			System.out.println("Residency Status: Out County");
		}
		totalCost = tuition + fees;
		System.out.println("Tuition: $" + tuition);
		System.out.println("Fees: $" + fees);
		System.out.println("Total Payment Due: $" + totalCost);

White-Box Test Data

Create data that will go through both parts of the if.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013