Connor V
Connor V

Reputation: 241

Write a class ArrayCalc which returns the maximum, minimum and average in Java

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

Upvotes: 1

Views: 905

Answers (3)

Mr. Noddy
Mr. Noddy

Reputation: 1590

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

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201467

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

Upvotes: 2

Vishal
Vishal

Reputation: 559

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

Upvotes: 0

Related Questions