1

Question: Write a class ArrayCalc which implements the following method

String arrayCalc(int[] array) : returns a string in the following format: “high: , low: , average: ”. Assume an array with any elements will have no null elements. Handle a null array by returning an empty string.

Consider the following code block:

ArrayCalc a = new ArrayCalc();
System.out.println(a.arrayCalc(new int[]{1, 2, 3})); //prints "high: 3, low: 1, average: 2"
System.out.println(a.arrayCalc(new int[]{0})); //prints "high: 0, low: 0, average: 0"
System.out.println(a.arrayCalc(null)); //prints ""

What I have:

public class ArrayCalc {
    String arrayCalc(int[] array) {
        int sum = 0;
        int average = 0;
        int i = 0;

        int min = Integer.MIN_VALUE;
        int max = Integer.MAX_VALUE;

        for (i = 0; i < array.length; i++) {
            sum += array[i];

            if (array[i] < min) {
                min = array[i];
            } else if (array[i] > max) {
                max = array[i];
            } else if (array == null) {
                return "";
            }
        }
        average = sum/array.length;

        return "";

    }
    public static void main(String[] args) {
        ArrayCalc a = new ArrayCalc();
        System.out.println(a.arrayCalc(new int[]{1, 2, 3}));
        System.out.println(a.arrayCalc(new int[]{0}));
        System.out.println(a.arrayCalc(null));
    }
}

What it prints out:

Exception in thread "main" java.lang.NullPointerException

    at ArrayCalc.arrayCalc(ArrayCalc.java:13)
    at ArrayCalc.main(ArrayCalc.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1
Connor V
  • 231
  • 3
  • 15
  • 3
    You should do the null check as first task of your method before the array is being processed. – Flown Sep 22 '15 at 05:04
  • Also, you should specify if arrayCalc is `public`, `private` or `protected`. I recommend `public`. You cannot use `private` or `protected` in this case. Also, there are two bugs: you should fix the declaration of `min` and `max` into: `int max = Integer.MIN_VALUE` and `int min = Integer.MAX_VALUE`. Can you reason why? Finally, you only return empty strings. Hence you can't expect to see output. – TuanDT Sep 22 '15 at 05:08
  • And, method always returns only one value. So you should print `max`, `min` and `average` in `arrayCalc()` itself. – SatyaTNV Sep 22 '15 at 05:10
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jason Sep 22 '15 at 05:17
  • 2
    When you pass null for method variable `array`, `array.length` will cause `NullPointerException`. – Naman Gala Sep 22 '15 at 05:29

3 Answers3

2

I would use String.format(String, Object...), Math.max(int, int) and Math.min(int, int). I would also prefer a for-each loop to iterate the values in the array. Something like

String arrayCalc(int[] array) {
    if (array == null || array.length == 0) {
        return "";
    }
    int sum = 0;
    int high = Integer.MIN_VALUE; // <-- start low.
    int low = Integer.MAX_VALUE; // <-- start high.
    for (int v : array) {
        high = Math.max(v, high);
        low = Math.min(v, low);
        sum += v;
    }
    return String.format("high: %d, low: %d, average: %d", high, low,
            sum / array.length);
}

which I ran with your main method. I got (the requested)

high: 3, low: 1, average: 2
high: 0, low: 0, average: 0
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You cannot check the length of NULL.

In you code-->

System.out.println(a.arrayCalc(null));

You are passing null value to an array. which is causing to exception in for loop (array.length nothing but NULL.length you are checking)

for (i = 0; i < array.length; i++)

add some boundary conditions for validation part.. rest code looks file

Vishal
  • 549
  • 1
  • 4
  • 21
0

Put null and empty check before you loop on array and print message if array is found null or empty.

Mr. Noddy
  • 1,530
  • 2
  • 16
  • 45