Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing java/05-staticmethods/Summer by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Write a program <code>Summer</code> whose API contains
two overloaded (same-name) public static methods
<ul>
<li>
<code>sum(double[] a)</code>,
which returns the sum of the elements of <code>a</code>
</li>
<li>
<code>sum(int[] a)</code>,
which returns the sum of the elements of <code>a</code>
</li>
</ul>
Public permissions remove
Engine
Template / Reference solution
 
\[
public static double sum(double[] a) {
   double result = 0;
   for (int i=0; i < a.length; i++)
      result += a[i]; 
   return result;
}
public static int sum(int[] a) {
   int result = 0;
   for (int i=0; i < a.length; i++)
      result += a[i]; 
   return result;
}
]\
// basic tests
public static void main(String[] args) {
   StdOut.println(sum(new int[]{1, 2, 6}));
   StdOut.println(sum(new double[]{Math.PI, Math.E, Math.log(2)}));
}
Java test suite
See manual
 
testMain();
test("sum", (Object)new int[]{-5, -4, -3, -2, -1});
test("sum", (Object)new double[]{1E10, 1E11, 1E12});
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.