Question

In: Computer Science

1. Using the following code identify the fault. 2. Using the following code indentify a test...

1. Using the following code identify the fault.

2. Using the following code indentify a test case that results in an error, but not a failure.

public static int lastZero (int[] x) {

//Effects: if x == null throw NullPointerException

//else return the index of the last 0 in x.

//Return -1 if 0 does not occur in x

for (int i = 0; i < x.length; i++)

{

if (x[i] == 0)

{

return i;

}

}

return -1;

}

//test: x = [0, 1, 0]

// Expected = 2

Solutions

Expert Solution

Here is code:

public static int lastZero (int[] x) {
//Effects: if x == null throw NullPointerException
//else return the index of the last 0 in x.
//Return -1 if 0 does not occur in x
if(x == null)
throw new NullPointerException("The x is null");
else
{
//loops from last index to 0th index
for (int i = x.length - 1; i >= 0; i--)
{
if (x[i] == 0)
{
return i;
}
}
return -1;
}
}

Sample test case to run:

public class testCase {
public static void main(String []args){
int[] a = {0,1,0};
System.out.println(lastZero(a));
}
public static int lastZero (int[] x) {
//Effects: if x == null throw NullPointerException
//else return the index of the last 0 in x.
//Return -1 if 0 does not occur in x
if(x == null)
throw new NullPointerException("The x is null");
else
{
//loops from last index to 0th index
for (int i = x.length - 1; i >= 0; i--)
{
if (x[i] == 0)
{
return i;
}
}
return -1;
}
}
}

Output:

2


Related Solutions

For the following C++ code, identify the 10 errors and rewrite the code using the space...
For the following C++ code, identify the 10 errors and rewrite the code using the space provided below. Do not write a new program just add, remove or change the characters/words that make the code incorrect. #include "iostream> using namespace std: class Line { public: Line( double L ) [ setLength(L); } void setLength( double L ) { length = L } double getLength( void ) { return length; } private= double length; }; int main() { double len; cout...
In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis, 4. test...
In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis, 4. test statistic, 5. p-value, and 6. conclusion. Researchers examined the effect of phosphate supplementation on bone formation in six healthy adult dogs. For each dog, bone formation was measured twice: once after 12 weeks of phosphate supplementation and once after a 12-week control period. The results in percent bone growth are shown below. Do the data provide evidence that phosphate supplementation significantly stimulates bone formation?...
Using the values that you found in numbers 1 and 2, perform the following hypothesis test....
Using the values that you found in numbers 1 and 2, perform the following hypothesis test. At a 10% significance level, test the claim that the women’s mean completion time is greater than ___________ (the men’s mean completion time). Ho: ___µ ≤ 27.914________   Ha: __µ > 27.914 (Claim)___ Label the claim. (6pts) What type of test will you use? (Z-Test, T-test, or 1-ProZTest) ______ (4 pts) Where is the rejection region? (Left-tailed, Right-tailed, Two-tailed test) (4 pts) Find the critical...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t b[]={b0, b1, b2}; // They are the last three digits of your A# uint8_t* c=b; uint8_t* d=&a;
Essay: Using the following problem, 1) apply the Pillow Method to view the issue. 2) Identify...
Essay: Using the following problem, 1) apply the Pillow Method to view the issue. 2) Identify each position and explain each position clearly in relation to the scenario. 3) Explain how an expanded view of the situation might affect the communication in this relationship. Problem:You and friend have talked about moving to another city together after college graduation. While planning where to live during your last year, you discover that your friend is insisting on living in the expensive parts...
Trace this code: 1) #include<iostream> using namespace std;    class Test {     int value; public:     Test(int...
Trace this code: 1) #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } =================================================================== 2) #include <iostream> using namespace std; int main() {                 int i, j;                 for (i = 1; i <= 3; i++)                 {                                 //print * equal to row number                                 for (j = 1; j <= i; j++)                                 {                                                 cout...
Edit question In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis,...
Edit question In the following research, identify 1. test type, 2. null hypothesis, 3. alternate hypothesis, 4. test statistic, 5. p-value, and 6. conclusion. A researcher took a sample of the grade point averages for students in his class. For 25 students, the standard deviation of grade points was 0.65, and the mean was 2.89. In the previous semester his class had an average of 2.95. Is there evidence that his current class is significantly different from his previous class?...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible. Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When...
Calculate the test statistic, x^(2), for a chi-squared test for association using the following table. The...
Calculate the test statistic, x^(2), for a chi-squared test for association using the following table. The following data represents the observed values and expected values of the eating habits of runners and swimmers for a random sample of adult athletes. Prefer to eat before workout. Prefer to eat after workout. Total Runner 68 121 189 Swimmer 73 128 201 Total 141 249 390
Test the following hypotheses by using the χ 2 goodness of fit test. H 0: p...
Test the following hypotheses by using the χ 2 goodness of fit test. H 0: p A = 0.2, p B = 0.4, and p C = 0.4 Ha: The population proportions are not p A = 0.2 , p B = 0.4 , and p C = 0.4 A sample of size 200 yielded 60 in category A, 120 in category B, and 20 in category C. Use  = .01 and test to see whether the proportions are as stated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT