5. EXAMPLE PROBLEM LINEAR EQUATION
5.1 Requirements
A linear equation with one unknown can be expressed as follows:
bx + c = 0
where b and c represent numeric quantities. Produce a Java
program which will solve equations of this form (i.e. find a value for
x - the root of the equation). Assume b and c are
input as integers and that the output is to be presented in the form of a real
number. Note that to find a value for x we must rearrange the above
equation:
|
Figure 5: Linear equation expressions
|
x = -c/b
Consequently b must not have a value of 0 (otherwise a "divide by
zero error will occur").
5.3.1 LinearEquation class
Field Summary |
private int |
bValue
An instance variable to store "b" value of an instance of the
class LinearEquation. |
private int |
cValue
An instance variable to store "c" value of an instance of the class
LinearEquation. |
private double |
xValue
An instance variable to store "x" value of an instance of the class
LinearEquation. |
Constructor Summary |
LinearEquation(int b_intInput, int c_intInput)
Constructs an instance of the class
LinearEquation given b and c values as input. |
Method Summary |
public boolean |
resolveLinearEquation()
Method to calculate x_value.
Returns true if a result can be calculated and false in the event that
b_value is equal to zero (and consequently a divide by zero error would
occur if the calculation was attempted). |
public double |
getXvalue()
Method to access the private instance field
x_value.. |
A Nassi-Shneiderman design for the above is presented in Figure 7.
Figure 7: Nassi-Shneiderman chart for LinearEquation
class
5.3.2 LinearEquationApp Class
Field Summary |
private static Scanner |
input
A class instance to facilitate input from the input stream. |
Method Summary |
public static void |
main(String[] args)
Main method for resolution of linear equation problem.
Allows input of two integers (as specified in requirements statement) which
must be cast to doubles when the constructor is called. Once an instance of
the class LinearEquation has been created the main
method invokes the ResolveLinearEquation method
to carry out the necessary calculation. If ResolveLinearEquation
returns true output the result for x, and otherwise output an error message. |
A Nassi-Shneiderman chart for the above is given in Figure 7.
Figure 7: Nassi-Shneiderman chart for LinearEquationApp
class
// LINEAR EQUATION CLASS
// Frans Coenen
// Tuesday 22 March 1999
// The University of Liverpool, UK
class LinearEquation {
// ------------------- FIELDS ------------------------
private int b_value, c_value;
private double x_value;
// ---------------- CONSTRUCTOR ------------------------
public LinearEquation(int b_intInput, int c_intInput) {
b_value = b_intInput;
c_value = c_intInput;
}
// ------------------ METHODS ------------------------
/* Resolve linear equation, returns true if successful and
false otherwise. */
public boolean resolveLinearEquation() {
// Test for divide by 0 error, if found return false
// otherise do calculation
if (b_value != 0) {
// Determine value for x and return true
x_value = ((double) -c_value)/(double) b_value;
return(true);
}
else return(false);
}
/* Get x_value */
public double getXvalue() {
return(x_value);
}
}
|
Table 2: Source code for linear equation class
Notes on the above:
5.4.2 LinearEquationApp class
// LINEAR EQUATION APPLICATION
// Frans Coenen
// Tuesday 22 March 1999
// Revised: Wednesday 5 July 2005
// The University of Liverpool, UK
import java.util.*;
class LinearEquationApp {
// ------------------- FIELDS ------------------------
// Create Scanner class instance
private static Scanner input = new Scanner(System.in);
// ------------------ METHODS ------------------------
/** Main method */
public static void main(String[] args) {
LinearEquation newEquation;
int newBvalue, newCvalue;
// Input
System.out.println("Input an integer values for b and c: ");
newBvalue = input.nextInt();
newCvalue = input.nextInt();
// Create linear equation instance
newEquation = new LinearEquation(newBvalue,newCvalue);
// Calculation. If method returns true output result,
// otherwiase produce error message.
if (newEquation.resolveLinearEquation()) {
System.out.print("Result is: ");
System.out.println(newEquation.getXvalue());
}
else
System.out.println("ERROR 1: Divide by zero error");
}
}
|
Table 3: Source code for linear equation application class
Note the way that the selection statement is encoded. The condition part of
the statement is evaluated first and then one or other of the action parts is
invoked according to the result. It is considered bad practice (and also
pointless) to test the result of a Boolean method against itself, i.e. we should
not write the condition part as (newEquation.resolveLinearEquation() ==
true).
5.4. Testing
5.4.1. Black box testing
Arithmetic testing: A suitable set of arithmetic test cases is given in the table to the right.
5.4.2. White box testing
Path testing: The activity diagram given in Figure 8 identifies
two paths through the system; one
where b_value is 0 and the other where b_value is
non-zero.
We should devise test cases to exercise both paths.
|
TEST CASE | EXPECTED RESULT |
---|
b_value | c_value | x_value |
---|
-4 | -2 | -0.5 |
-4 | 0 | 0.0 |
-4 | 2 | 0.5 |
0 | -2 | Error |
0 | 0 | Error |
0 | 2 | Error |
4 | -2 | 0.5 |
4 | 0 | 0.0 |
4 | 2 | -0.5 |
|
|
Two appropriate test cases are given in the
Table to the right. Note that both are duplicates
duplicate of a black box test case, thus we do nor need to run either.
5.4.3. Data validation testing
This should also be carried out
|
TEST CASE | EXPECTED RESULT |
---|
b_value | c_value | x_value |
---|
0 | 2 | Error |
4 | 2 | -0.5 |
|
|
|