So I'm suppose to create some code that will perform some methods in recursion.

Here's the prompt:
Assignment #9 will be the construction of a program that reads in a sequence of numbers (not necessary integers) from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers.

Then compute the minimum number, the sum of positive numbers, the sum of numbers at odd indexes (1,3,5,...) of the array, and count the number of negative numbers, using recursion. Thus you will create recursive methods findMin, computePositiveSum, computeSumAtOdd, and countNegative in Assignment9 class and they will be called by a main method.

Specifically, the following recursive methods must be implemented (These methods should not contain any loop):

public static double findMin(double[] numbers, int startIndex, int endIndex)

public static double computePositiveSum(double[] numbers, int startIndex, int endIndex)

public static double computeSumAtOdd(double[] numbers, int startIndex, int endIndex)

public static int countNegative(double[] numbers, int startIndex, int endIndex)
If these methods are implemented using a loop, points will be deducted (from the test cases) even if your program passes test cases. DO NOT use any Static Variables either.

The program should output the results of those calculations to standard output. Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

The minimum number is 0.##
The sum of the positive numbers is 0.0#
The sum of the numbers at odd indexes is 0.000
The total count of negative numbers is 0
To format numbers as above, you should utilize the DecimalFormat class (java.text package).

Do not output a prompt to query for the numbers. The number 0 is included in the sequence of numbers and should be included in all of your calculations.


Here's my code, it's pretty much finished, but I don't know what's wrong. After I type in my values I get an error.

import java.io.*;
import java.text.*;

public class Assignment9
{

public static void main (String args[]) throws IOException
{
	int i = 0;
	double[] NumArray;
	NumArray = new double[100];

	// input stream reader reads in keyboard inputs, while buffered reader
	// reads in the line as a string
	InputStreamReader inRead = new InputStreamReader(System.in);
    BufferedReader buffRead = new BufferedReader(inRead);

    String line = buffRead.readLine();


    // if the string is equal to 0 and is false AND i is less than
    // 100, parse string into double.
    try{
    while (line.equals("0") == false && i<100)
    	{
    	i++;
    	line = buffRead.readLine();
    	NumArray[i]=Double.parseDouble(line);
    	}
       }
    catch(IOException e)
    {
    	System.out.println("Array index out of bound");
    }
    double min =  findMin(NumArray, 0, NumArray.length);
    double posSum = computePositiveSum(NumArray, 0, NumArray.length);
    double negSum = computeSumAtOdd(NumArray, 0, NumArray.length);
    int negCount = countNegative(NumArray, 0, NumArray.length);

    System.out.print ("The minimum number is " + min + ('\n'));
	System.out.print ("The sum of the positive numbers is " + posSum + ('\n'));
	System.out.print ("The sum of the numbers at odd indexes is " + negSum + ('\n'));
	System.out.print ("The total count of negative numbers is " +  negCount);


}
public static double findMin(double[] NumArray, int startIndex, int endIndex)
{
	if (startIndex == endIndex)//base case
	{
		return NumArray[startIndex]; // return value is there is only one entry
	}

	else if(findMin(NumArray, startIndex, endIndex - 1) < NumArray[endIndex]) // check through array for smallest value
	{
		return findMin(NumArray, startIndex, endIndex - 1); // return the method if it's less than array entry
	}

	else
	{
		return NumArray[endIndex]; // if not then just return the endIndex of the array
	}


}

public static double computePositiveSum(double[] NumArray, int startIndex, int endIndex)
{
	if (startIndex == endIndex)// base case
	{
		if (NumArray[startIndex] < 0) // checks the value in array entry if it's 0
		{
			return 0; // return 0 if true
		}

		else
			return NumArray[startIndex]; // if not, then return whatever value it is
		}

	else if (NumArray[endIndex] > 0)
	{
		return computePositiveSum(NumArray, startIndex, endIndex - 1) + NumArray[endIndex];
	}

	else
	{
		return computePositiveSum(NumArray, startIndex, endIndex - 1); // if the value doesn't apply to to any statements then return
	}

}


public static double computeSumAtOdd(double[] NumArray, int startIndex, int endIndex)
{
	if (startIndex == endIndex) // base case to check if there is only one entry
	 {
		if (startIndex %2 == 1) // if statement to check if the array index is an odd number
		{
			return NumArray[startIndex]; //return the only values that is in the array
		}

		else

			return 0;
	 }

	else
	{
		if (endIndex %2 == 1) // checks to see if the array entries are odd indexes
		{
			return computeSumAtOdd(NumArray, startIndex, endIndex - 1) + NumArray[endIndex]; // return those entries and method
		}

		else

			return computeSumAtOdd(NumArray, startIndex, endIndex - 1); // else just return the method and not the specific values

	   }

}


public static int countNegative(double[] NumArray, int startIndex, int endIndex)
{
	if (startIndex == endIndex) // base case to check if there's only one entry
	{
		if (NumArray[startIndex] < 0) //checks if the number is a negative
		{
			return 1; // if so then return 1
		}

		else

			return 0; // else return a 0 because there is no negative number
    }

	else{
		if (NumArray[startIndex] < 0) // check to see if the array entries are less than 0
		{
			return 1 + countNegative(NumArray, startIndex +1, endIndex); // for every negative number add 1 to the counter
		}

		else

			return countNegative(NumArray, startIndex +1, endIndex); // else return the method
		}

}

}

The error I get is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Assignment9.findMin(Assignment9.java:54)
at Assignment9.main(Assignment9.java:35)


Can anyone help me here? It's really confusing and I'm not sure what's wrong.

Recommended Answers

All 3 Replies

Line 35 in main says

double min =  findMin(NumArray, 0, NumArray.length);

Then in findMin , line 54 says

else if ... < NumArray[endIndex])

Remember in Java that array indices go from 0 to length-1. So you can't access NumArray[[B]endIndex[/B]]) . You are beyond the bounds of the array.

Thanks for the help man! I'm pretty sure students of the same class I'm in are looking at my coding right now. How can I close this thread?

You can mark it as solved, but that won't get rid of it. Once it's posted, it's here for the world to see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.