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...
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...
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.
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.
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT