# recursion

• 01-18-2008, 05:17 PM
kdeighan
recursion
I have to complete the program below which is intended to print
the value of the largest element in an array of doubles. This is what I have so far w/ the test class but i'm stuck with tester method (2 parameters) & tester method(1parameter)

/*
* A class to test the ArrayTools class
*/
public class ArrayToolsTest
{
public static void main(String[] args)
{
double[] myArray = { ... }; // Fill this array with any doubles
ArrayTools tester = new ArrayTools(myArray);
System.out.println("The largest value in the array is " + tester.findLargest());
}
}

/*
* This class contains methods which would be helpful to use with an array.
*/
public class ArrayTools
{
public ArrayTools(double[] myArray)
{
doubs = myArray;
}

public boolean tester()
{
}

private boolean tester(//PARAMETERS?)
{
}

double[] doubs = new double[100];
double maximum;
}
• 01-18-2008, 08:20 PM
CaptainMorgan
You titled the thread "recursion", but didn't say whether you wanted to use it and I do not see your attempt at it. You also call a method findLargest, so I don't understand why you need the tester methods. The following compiles and runs - but I'm leaving the findLargest logic up to you - if you get stuck post how you've attempted it. Please use code tags.

Code:

public class ArrayTools {
double[] doubs = new double[100];
double maximum;

public ArrayTools(double[] myArray) {
this.doubs = myArray;
}

public boolean tester() {
return true;
}

private boolean tester(double i, int j) {
return true;
}

public double findLargest() {
// for all values k in doubs,
//  iterate over doubs[k],
//  compare next value to values already compared
return maximum;
}
}

Code:

public class ArrayToolsTest{
public static void main(String[] args) {
double[] myArray = { 1.2, 5.7, 4.3, 17.24, 12.34 }; // Fill this array with any doubles
ArrayTools tester = new ArrayTools(myArray);
System.out.println("The largest value in the array is " + tester.findLargest());
}
}

• 01-25-2008, 04:53 AM
kdeighan
okay i got the base case down, but i don't think i called the recursive method correctly at all.

Code:

public double findLargest(myArray)
{

if( k == 0)
{
maximum = doubs[k];
return maximum;
}
else return (myArray.findLargest(k-1)
}

• 01-25-2008, 10:48 PM
portis2003
you don't need to use recursion to find the largest double in an array of doubles. try adding this method in.

Code:

public double findLargest(double[] myArray)    //so this method is taking your array
{                                                  //as a parameter
int maximum = myArray[0];
for(int i = 0; i < myArray.length; i++)
{
if(myArray[i] > maximum)
{
maximum = myArray[i];
}
}
return maximum;
}