Results 1 to 4 of 4
Thread: recursion
- 01-18-2008, 04:17 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 3
- Rep Power
- 0
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, 07:20 PM #2
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.
Java 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; } }Java 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()); } }Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 01-25-2008, 03:53 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 3
- Rep Power
- 0
okay i got the base case down, but i don't think i called the recursive method correctly at all.
Java Code:public double findLargest(myArray) { if( k == 0) { maximum = doubs[k]; return maximum; } else return (myArray.findLargest(k-1) }
- 01-25-2008, 09:48 PM #4
Member
- Join Date
- Jan 2008
- Posts
- 1
- Rep Power
- 0
you don't need to use recursion to find the largest double in an array of doubles. try adding this method in.
Java 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; }Last edited by portis2003; 01-25-2008 at 09:51 PM.
Similar Threads
-
Help With Recursion
By andrew777 in forum New To JavaReplies: 1Last Post: 03-29-2008, 12:51 PM -
Recursion
By bozovilla in forum Advanced JavaReplies: 3Last Post: 01-07-2008, 04:53 PM -
recursion
By ravian in forum New To JavaReplies: 2Last Post: 12-03-2007, 05:15 PM -
Help with recursion
By scts102 in forum New To JavaReplies: 1Last Post: 11-19-2007, 10:07 PM -
Recursion in java
By lenny in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks