Answer the following questions by typing answers right in a word document. For any code, make sure to
convert the code into COURIER NEW font and submit the exam by the due date in Blackboard.
1. What is the principle of proximity with regards to variable declaration and use? Why is it a
good idea to be following this principle with regards to variables? (10 points)
2. What is the difference between variable span and the live time of a variable? Illustrate the
difference using an example. What are good practices with regards to the length of the variable
span and live time. (15 points)
3. The following control statement helps determine the season based on the value of the
temperature. Identify any issues with this control statement, fix them and see if there is any way
to simplify it. (Type your code in Courier New font)
if(temperature >= 90)
print(“It must be summer!”);
else if (temperature < 90 && temperature >= 60)
print(“It must be spring”);
else if (temperature < 60 && temperature >= 40)
print(“It must be early spring”);
else if (temperature < 40 && temperature >= 20)
print(“It must be mild winter”);
else if (temperature < 20 && temperature >= 0)
print(“It is a very cold winter”);
else if (temperature < 0) print(“You must be living in North Dakota”); else if (temperature >= 130 && temperature < -100) print(“You must be not be living on earth”); (20 points) 4. The following static method implements the binary search algorithm to search for a key in an array public static int binarySearch( int [] arr, int key ) { int start = 0; int end = arr.length – 1; int middle; while ( end >= start )
{
middle = ( start + end )/ 2; // element in middle of array
if ( arr[middle] == key )
{
return middle; // key found at middle
}
else if ( arr[middle] > key )
{
end = middle – 1; // search left side of array
}
else
{
start = middle + 1; // search right side of array
}
}
return -1;
}
Create a flowchart for the code and label each of the nodes using
numbers or letters. Next determine complexity of the code using the
McCabes Cyclomatic number and identify the various paths of code
execution using the labels of the nodes (like A B D E G H).
(For this question, you are welcome to hand raw the flowchart and take
a picture and embed the picture in the word document)
(20 points)
5. Use a table driven approach to simplify the logic of the following control structure of the
getTax() method. This method calculates the tax amount based on the status and income.
public double getTax()
{
double tax = 0;
if (status == SINGLE)
{
if (income <= SINGLE_CUTOFF1) tax = RATE1 * income; else if (income <= SINGLE_CUTOFF2) tax = SINGLE_BASE2 + RATE2 * (income – SINGLE_CUTOFF1); else tax = SINGLE_BASE3 + RATE3 * (income – SINGLE_CUTOFF2); } else { if (income <= MARRIED_CUTOFF1) tax = RATE1 * income; else if (income <= MARRIED_CUTOFF2) tax = MARRIED_BASE2 + RATE2 * (income – MARRIED_CUTOFF1); else tax = MARRIED_BASE3 + RATE3 * (income – MARRIED_CUTOFF2); } return tax; } Here are the declarations of the constants and instance variables used in the above method. public static final int SINGLE = 1; public static final int MARRIED = 2; private static final double RATE1 = 0.15; private static final double RATE2 = 0.28; private static final double RATE3 = 0.31; private static final double SINGLE_CUTOFF1 = 21450; private static final double SINGLE_CUTOFF2 = 51900; private static final double SINGLE_BASE2 = 3217.50; private static final double SINGLE_BASE3 = 11743.50; private static final double MARRIED_CUTOFF1 = 35800; private static final double MARRIED_CUTOFF2 = 86500; private static final double MARRIED_BASE2 = 5370; private static final double MARRIED_BASE3 = 19566; private double income; private int status; (20 points)
Leave a Reply