Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

diffSum example

Hi,

I wrote diffSum using recursion as below

public class DifferenceSum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int firstSensor[] = { 15, -4, 56, 10, -23 };
		int secondSensor[] = { 14, -9, 56, 14, -23 };
		System.out.println("value is-->" + diffSum(firstSensor, secondSensor, 5));
	}

	private static int diffSum(int[] sensorA, int[] sensorB, int size) {
		// TODO Auto-generated method stub

		int diff = 0;
		for (int i = 0; i < size; i++) {
			diff += Math.abs(sensorA[i] - sensorB[i]);
		}
		return diff;
	}

}

Open in new window


i wonder if i can make any modifications or improvements to this. please advise
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public class DifferenceSum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int firstSensor[] = { 15, -4, 56, 10, -23 };
		int secondSensor[] = { 14, -9, 56, 14, -23 };
		DifferenceSum ds=new DifferenceSum();
		System.out.println("value is-->" + ds.diffSum(firstSensor, secondSensor, 5));
	}

	private  int diffSum(int[] sensorA, int[] sensorB, int size) {
		// TODO Auto-generated method stub

		int diff = 0;
		for (int i = 0; i < size; i++) {
			diff += Math.abs(sensorA[i] - sensorB[i]);
		}
		return diff;
	}

}

Open in new window


i made last change. thinking about other changes on checks
Avatar of gudii9

ASKER

It may be deemed unnecessary in this narrow context (everything is self-contained currently) but it is usually good practice to check your inputs. Here you are providing two arrays and a size, using the size to control the loop iteration but indexing the arrays without any prior checks leaving yourself open to an ArrayIndexOutOfBoundsException. You might consider adding checks (perhaps even asserts) at the top of the method to ensure both arrays meet the size constraint before proceeding. It is usually appropriate to throw IllegalArgumentException in this case:
    assertTrue(sensorA,length == size, MessageFormat.format("sensorA.length size error: {0} != {1}", sensorA.length, size));
    assertTrue(sensorB,length == size, MessageFormat.format("sensorB.length size error: {0} != {1}", sensorB.length, size));

    private static void assertTrue(boolean cond, String message) throws IllegalArgumentException {
        if (cond == false) {
            throw new IllegalArgumentException(message);
        }
    }

Open in new window

checking this
Avatar of gudii9

ASKER

getting below error

Multiple markers at this line
      - Return type for the method is missing
      - Syntax error, insert "... VariableDeclaratorId" to complete
       FormalParameterList
      - Syntax error on token "==", delete this token
      - Syntax error, insert "... VariableDeclaratorId" to complete
       FormalParameter
      - Syntax error on token ".", @ expected after this token
      - Syntax error on token "(", { expected after this token
      - Syntax error on token(s), misplaced construct(s)
      - Syntax error, insert "SimpleName" to complete QualifiedName
      - Syntax error on token ")", { expected after this token

for below code

public class DifferenceSum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int firstSensor[] = { 15, -4, 56, 10, -23 };
		int secondSensor[] = { 14, -9, 56, 14, -23 };
		DifferenceSum ds=new DifferenceSum();
		System.out.println("value is-->" + ds.diffSum(firstSensor, secondSensor, 5));
	}
    assertTrue(sensorA,length == size, MessageFormat.format("sensorA.length size error: {0} != {1}", sensorA.length, size));
    assertTrue(sensorB,length == size, MessageFormat.format("sensorB.length size error: {0} != {1}", sensorB.length, size));
	private  int diffSum(int[] sensorA, int[] sensorB, int size) {
		// TODO Auto-generated method stub

		int diff = 0;
		for (int i = 0; i < size; i++) {
			diff += Math.abs(sensorA[i] - sensorB[i]);
		}
		return diff;
	}

}

Open in new window


please advise