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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

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

  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, 06:58 AM
  2. Replies: 3
    Last Post: 03-21-2007, 04:28 PM
  3. Replies: 2
    Last Post: 05-23-2006, 02:22 PM
  4. Replies: 0
    Last Post: 05-04-2002, 04:59 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center