help with really easy thing I just cant get....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: help with really easy thing I just cant get....

  1. #1
    Join Date
    Mar 2005
    Posts
    6

    help with really easy thing I just cant get....

    Write a class named IntFinder that has a single static method named largest that takes an array of int and returns the largest int in the array. You may assume all integers in the array are positive.

    You may use BlueJ for this problem, and copy and paste your IntFinder class from BlueJ into the Answer section of this problem when you are finished.

    Here is a class that you may cut and paste into BlueJ to use to test your IntFinder class.

    public class IntFinderDriver
    {
    public static int runIntFinder()
    {
    int[] ia = new int[5];
    ia[0] = 15;
    ia[1] = 7;
    ia[2] = 99;
    ia[3] = 55;
    ia[4] = 19;
    return IntFinder.largest(ia);
    }
    }

    If you execute the runIntFinder() method, it should return 99.



    I have this so far... I need the body....

    public class IntFinder

    {

    public static void largest(int[ ] )

    {

    if ia[ ] = = true

    return int;

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Wel well....

    Code:
    public class IntFinder {
      public static void largest(int[ ]a) {
        int max=Integer.MIN_VALUE;
        for (int i=0; i<a.length; i++) {
          max=(max < a[i]) ? a[i] : max;
        }
        return max;
      }
    }
    The code you posted... hmmmm, you'd better get started reading java..

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    of course the declaration of largest() should be public static int largest(int[]) ...

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    definitely

    Quote Originally Posted by nspils
    of course the declaration of largest() should be public static int largest(int[]) ...
    without a trace of doubt
    eschew obfuscation

  5. #5
    Join Date
    May 2005
    Posts
    25
    public class IntFinder {

    public static int largest(int[] a) {
    int largest = Integer.MIN_VALUE;
    for ( int i = 0 ; i < a.length ; i++ ) {
    if ( a[i] > largest )
    largest = a[i];
    }
    return largest;

    }


    }

    then for your main()

    public class IntFinderDriver
    {
    public static int runIntFinder()
    {
    int[] ia = new int[5];
    ia[0] = 15;
    ia[1] = 78858;
    ia[2] = 99;
    ia[3] = 55;
    ia[4] = 19;
    return IntFinder.largest(ia);
    }
    public static void main(String[] args){
    System.out.print(runIntFinder());


    }

    } // end class


    it will return 99 or w/e the largest int in the array is. o and dont use the ternary op, its ugly and makes code harder to read.

    make sure you understand this before you hand it in. when you learn a programming lang its very important to know the basics like the back of your hand, they are the foundation that holds the building up.
    Last edited by ZeroFear; 05-03-2005 at 08:58 PM.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Since code style is an issue:

    Static methods should be used with the complete definition so
    runIntFinder should be invoked like:

    IntFinder.runIntFinder();

    As for statics, they should only be used when the analysis/design requires
    it, a habit of using statics is a bad habit.

    Code:
    public class IntFinder {
    
      public int largest(int[] a) {
      int largest = Integer.MIN_VALUE;
      for ( int i = 0 ; i < a.length ; i++ ) {
        if ( a[i] > largest )
        largest = a[i];
      }
       return largest;
      }
    }
    public class IntFinderDriver {
      public int runIntFinder() {
        int[] ia = new int[5];
        ia[0] = 15;
        ia[1] = 78858;
        ia[2] = 99;
        ia[3] = 55;
        ia[4] = 19;
        IntFinder inf=new IntFinder();
        return inf.largest(ia);
      }
      public static void main(String[] args){
        IntFinderDriver ifd=new IntFinderDriver();
        System.out.print(ifd.runIntFinder());
      }
    } // end class
    eschew obfuscation

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