Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2012, 02:00 AM   PM User | #1
tjgibbs92
New to the CF scene

 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tjgibbs92 is an unknown quantity at this point
Coding Help with Arrays

Code:
public class ArrayMethods
{
  private double[] a;
  
  public static double sumOfArrays(double[] a)
  {
    double max = a[0];
    
    for(int i =1; i<a.length; i++)
    {
      if(a[i] > max)
        max = a[i];
    }
    
    double min = a[1];
    for(int i =1; i<a.length; i++)
    {
      if(a[i] < min)
        min = a[i];
    }
    
    double difference = max - min;
    return difference;
  }
  
  public static void main(String[] args)
  {
    double [] b = {43.9,12.3,99.6,48.2,2.1,65.5};
    double diff = differenceOfArrays(b);
    System.out.println(diff);
  }
}
When I run the code, at the line where I run the method I wrote, double diff = differenceOfArrays(b);, it says it cannot find the symbol. Before I wrote it, I didn't have the method as static and when I didn't have static there it said that it couldn't be dereferenced. So I made it static. Could anyone help with this? It would be greatly appreciated.
tjgibbs92 is offline   Reply With Quote
Old 05-02-2012, 04:21 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,644
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You have no method called differenceOfArrays. All you have here is sumOfArrays, although given what it does I'd suspect it should have its signature changed to differenceOfArrays.
An easier solution btw would be to first sort the array from Arrays.sort, then pull both the first and last elements in the array, and calculate the difference. Otherwise, you can perform both the max and the min within the same loop by evaluating both min and max to the current iteration to see if it should be bumped.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
array

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:35 PM.


Advertisement
Log in to turn off these ads.