Results 1 to 4 of 4

Thread: recursion

  1. #1
    kdeighan is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default 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;
    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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)

  3. #3
    kdeighan is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    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)
       }

  4. #4
    portis2003 is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    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

  1. Help With Recursion
    By andrew777 in forum New To Java
    Replies: 1
    Last Post: 03-29-2008, 12:51 PM
  2. Recursion
    By bozovilla in forum Advanced Java
    Replies: 3
    Last Post: 01-07-2008, 04:53 PM
  3. recursion
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 05:15 PM
  4. Help with recursion
    By scts102 in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 10:07 PM
  5. Recursion in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •