4 Replies - 1366 Views - Last Post: 14 January 2010 - 09:19 AM Rate Topic: -----

#1 sherlyn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-January 10

Overloading Methods

Posted 14 January 2010 - 06:33 AM

everyone can help me..?
I'm very confuse..

hmm.. How write two overload methods that return the average of an array. for example with the following headers:
public static int average (int [] array);
public static double average (double [] array);

Thank you so much......

This post has been edited by KYA: 14 January 2010 - 09:04 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Overloading Methods

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Overloading Methods

Posted 14 January 2010 - 06:41 AM

You simply write two functions with the same names but different return types and/or argument lists. The prototypes you have presented have that quality so those are the prototypes of overloaded functions. You will have to write the body of each function to be different so that the first handles an array of integers and the second handles an array of doubles.
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Overloading Methods

Posted 14 January 2010 - 06:43 AM

you already are. different parameters will automatically override the method.

Edit: misread question

This post has been edited by SwiftStriker00: 14 January 2010 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Overloading Methods

Posted 14 January 2010 - 09:04 AM

Title renamed to be more descriptive of the problem.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: Overloading Methods

Posted 14 January 2010 - 09:19 AM

View PostSwiftStriker00, on 14 Jan, 2010 - 09:43 AM, said:

you already are. different parameters will automatically override the method.

Edit: misread question


I think you might mean overloading methods, not overriding methods. :)

@OP: You simply have to define the bodies of the functions. However, overloading methods is basically using different methods with the same name and a different number, type or order of arguments/parameters. Java does not look at return-type when considering overloaded methods.

Also, just a suggestion, I would change the return-type of your average(int[] array) to a double instead of an int because if the sum < array.length, it will return 0 (hint- you should probably also store the sum variable as a double).

I've attached the link to the Sun tutorial on Defining Methods: http://java.sun.com/...OO/methods.html

Hope this helps clear up overloading methods. Good luck! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1