DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2007
    Posts
    6

    Having a problem with arrays as parmater in JAVA!!!

    Here's the problem and the solution that I could do. It has some errors. Can anyone please correct this program and make it work. Any help is greatly appreciated.


    Problem : Write a method called Spread() which accepts an array of integers as a parameter and returns the spread, i.e., the difference between the largest and the smallest elements of the array. Write a main() method to test your method.


    import java.util.*;
    public class MaxMin
    {
    private int[] list;
    public static int Spread(int [] list);
    {
    int max, min, dif;
    max=list[0];
    min=list[0];
    for(int j = 0; j < list.length; j++)
    {
    if (max < list[j])
    max=list[j];
    if (min > list[j])
    min=list[j];
    }
    dif = (max - min);
    return (dif);
    }
    public static void main(String[] args)
    {
    int a;
    int arraySize;
    Scanner input=new Scanner(System.in);
    System.out.print("How many numbers do you wish to enter: ");
    arraySize = input.nextInt();
    System.out.println();
    int[] list = new int[arraySize];
    System.out.print("\nEnter your numbers: ");
    for(int i = 0; i < arraySize; i++)
    {
    list[i] = input.nextInt();
    }
    MaxMin object=new MaxMin();
    System.out.print("\nThe difference between the maximum and minimum is: "+ object.Spread(list));
    }
    }

  2. #2
    Join Date
    Dec 2005
    Location
    Arrr
    Posts
    32
    You have a semicolon at the end of the method header for Spread().:

    public static int Spread(int [] list);

    remove the semicolon, and all should be well.

  3. #3
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Try This one:

    Code:
    /**
     * Write a description of class Spread here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.*;
    public class MaxMin
    {
    private int[] list;
    public static int Spread(int [] list)
    {
    int max, min, dif;
    max=list[0];
    min=list[0];
    for(int j = 0; j < list.length; j++)
    {
    if (max < list[j])
    max=list[j];
    if (min > list[j])
    min=list[j];
    }
    dif = (max - min);
    return (dif);
    }
    public static void main(String[] args)
    {
    int a;
    int arraySize;
    Scanner input=new Scanner(System.in);
    System.out.print("How many numbers do you wish to enter: ");
    arraySize = input.nextInt();
    System.out.println(); 
    int[] list = new int[arraySize]; 
    System.out.print("\nEnter your numbers: ");
    for(int i = 0; i < arraySize; i++) 
    {
    list[i] = input.nextInt();
    }
    MaxMin object=new MaxMin();
    System.out.print("\nThe difference between the maximum and minimum is: "+ object.Spread(list));
    }
    }

  4. #4
    Join Date
    Mar 2007
    Posts
    6
    Thankyou very much....Now its working perfectly......

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 05:58 AM
  2. Replies: 3
    Last Post: 03-21-2007, 03:28 PM
  3. Replies: 2
    Last Post: 05-23-2006, 01:22 PM
  4. Replies: 0
    Last Post: 05-04-2002, 03:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links