Question

In: Computer Science

For java. It's your turn to write a test suite! Let's start out simple. Create a...

For java.

It's your turn to write a test suite! Let's start out simple. Create a public class TestArraySum that provides a single void class method named test. test accepts a single parameter: an instance of ArraySum.

Each ArraySum provides a method sum that accepts an int[] and returns the sum of the values as an int, or 0 if the array is null. However, some ArraySum implementations are broken! Your job is to identify all of them correctly.

To do this you should use assert to test various inputs. Here's an example:

assert sum.sum(null) == 0;

Your function does not need to return a value. Instead, if the code is correct no assertion should fail, and if it is incorrect one should.

As you design test inputs, here are two conflicting objectives to keep in mind:

  • Less is more. The fewer inputs you need to identify all broken cases, the more readable your test suite will be.
  • Think defensively. At the same time, you want to anticipate all of the different mistakes that a programmer might make. You've probably made many of these yourself! Examples include forgetting to check for null, off-by-one errors, not handling special cases properly, etc.

Solutions

Expert Solution

The program can be written as

class ArraySum
{
   int sum(int[] arr)
   {
  
       int sum = 0;
if (arr.length ==0)
return 0;
      
       for (int i = 0; i < arr.length; i++)
           sum+=arr[i];
      
       return sum;
   }
}
class TestArraySum
{
   void test( ArraySum a)
  
   {
       int arr[] = {};
int x =a.sum(arr);
System.out.println(" x= "+x);
      
   }
}


public class Test
{   
   public static void main(String args[])
   {
ArraySum a= new ArraySum();
TestArraySum t1= new TestArraySum();
       t1.test(a);
      
  
   }
}

That will print 0.


Related Solutions

WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
Turn Think of your favorite place to shop. Write a program that will create a form...
Turn Think of your favorite place to shop. Write a program that will create a form for your favorite store using the PHP Application The form should include fields for contact information and at least three(3) items to order from. It should calculate the order and display a subtotal before tax and a total after tax. This is your final project, so add background color, pictures, and anything else that you thin
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0. I have this so far. What code should I put? divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (number1.getText().length() != 0 && number2.getText().length() != 0) { double n1= Double.parseDouble(number1.getText().toString()); double n2= Double.parseDouble(number2.getText().toString()); double res= n1 / n2; result.setText(String.valueOf(res)); } else { Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show(); } } });
Write two simple logical statements in English. Negate your second simple statement. Then write out a...
Write two simple logical statements in English. Negate your second simple statement. Then write out a compound statement using your first simple statement and the negated statement with the following connectives. In each case evaluate the truth value of your compound statement. (a) Logical AND (b) Logical OR (c) Logical XOR (d) Logical implication
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
Prove the following statement: Suppose it's your turn and the Nim sum of the number of...
Prove the following statement: Suppose it's your turn and the Nim sum of the number of coins in the heaps is equal to 0. Then whatever you do, the Nim sum of the number of coins after your move will not be equal to 0.
Test Your Understanding Create a JAVA package by name ClassEx2 and in the main() method of...
Test Your Understanding Create a JAVA package by name ClassEx2 and in the main() method of the class, assign your name to a String object and your father name to a different String object and do the following; Concatenate your name and your father name. Get the length of your full name. Find the index of any character in your full name. Replace the occurrence of any lower case character in your full name to capital character. Compare your first...
Need this in java .Create a simple login screen in java That lets people sign in...
Need this in java .Create a simple login screen in java That lets people sign in to the emergency room. It needs to ask for name, social,and DOB. Once all the information is received it then display the info back to them if any info is is missing have the program prompt the user to re enter their information.
ow, it's time to turn in your semester project. Remember your project should include the following:...
ow, it's time to turn in your semester project. Remember your project should include the following: A Mental Health Concern - and the Patient or Community - for your project. A brief history of the patient including diagnoses and medications - or a brief description of the community issue. Any substance abuse, addiction or violence issues surrounding this mental health problem. Describe the attempted interventions that have been made for your patient or community, and identify what has been successful...
Now, it's time to turn in your semester project. Remember your project should include the following:...
Now, it's time to turn in your semester project. Remember your project should include the following: A Mental Health Concern - and the Patient or Community - for your project. A brief history of the patient including diagnoses and medications - or a brief description of the community issue. Any substance abuse, addiction or violence issues surrounding this mental health problem. Describe the attempted interventions that have been made for your patient or community, and identify what has been successful...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT