Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-18-2008, 06:17 PM
Member
 
Join Date: Jan 2008
Posts: 3
kdeighan is on a distinguished road
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;
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-18-2008, 09:20 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to 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()); } }
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-25-2008, 05:53 AM
Member
 
Join Date: Jan 2008
Posts: 3
kdeighan is on a distinguished road
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) }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-25-2008, 11:48 PM
Member
 
Join Date: Jan 2008
Posts: 1
portis2003 is on a distinguished road
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; }

Last edited by portis2003 : 01-25-2008 at 11:51 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Recursion andrew777 New To Java 1 03-29-2008 02:51 PM
Recursion bozovilla Advanced Java 3 01-07-2008 06:53 PM
recursion ravian New To Java 2 12-03-2007 07:15 PM
Help with recursion scts102 New To Java 1 11-20-2007 12:07 AM
Recursion in java lenny Advanced Java 1 08-07-2007 08:23 AM


All times are GMT +3. The time now is 01:37 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org