Results 1 to 3 of 3
  1. #1
    ilop12 is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default Hopefully in the right place.

    I think its in the right place. If not, im sorry.
    does the code below A) work at all, B) answer the question, and C) make sense?

    Write a program to demonstrate the overloading method that would have a class with many functions of same name (i.e. adding two values of different data types).



    class a
    {
    void Add(int a, double b)
    {
    System.out.println(a+b);

    }
    }
    class overload
    {
    public static void main(String args[])
    {
    Over obj= new Over();
    obj.Add(10,2.5);
    }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    No, I believe they are trying to mean overloading as in other languages such as C++ or PHP where a method may have optional arguments, or perform the same operation but with different kinds of inputs.

    Your example just declares one class with one method and then a second class to create an instance of it to make use of this one method.

    Consider,
    Java Code:
    public class FancyString {
    
      private String content = new String();
    
      public FancyString() {
      // empty
      }
    
      public FancyString(String in) {
        this.content = in;
      }
    
       public void add(int i) {
        content += i;
       }
    
        public void add(String s) {
          content += s;
        }
    
        public void add(boolean b) {
          content += b;
        }
      public String toString() {
        return content;
      }
    }
    where we have the same method, add() that takes on a single parameter, but each variant uses a different data type.

    Alternatively,

    Java Code:
    public class B {
    
      /**
      * Catenates two strings, no delimiter between them,  and no enforcement of max length.
      */
      public String appendString(String input1, String input2) {
        return appendString(input1, null, input2);
      }
    
      /**
      * Catenates two strings, using a delimiter, but no restriction on the maximum length.
      */
      public String appendString(String input1, String input2, String delimiter) {
        return appendString(input1, input2, delimiter, null);
      }
     
      /** 
      *  catenates two strings, using the delimiter between each input, and enforcing the string is not longer than maxLength
      */
      public String appendString(String input1, String input2, String delimiter, Integer maxlength) {
        String result = input1;
        if (delimiter != null) {
          result += delimiter;
        }
        result += input2;
    
        if (maxLength != null) {
          int len = maxLenth.intValue();
            if (result.length() > len) {
              result = result.substring(0,len);
            }
        }
        return result;
      }
    }
    creates an 'overloaded' method with different amount of arguments, *flagrant sigh*, because Java [for some reason], does not support default value on method arguments.
    In this example above, I just invoke the other variant of the method with more arguments supplying what in this case suitable defaults would be, but in practice each different 'overloaded' method could certainly do something completely different internally, perhaps even unsensibly so, since overloading for method signatures only means the same method name with different number or different data types for parameters.

  3. #3
    ilop12 is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    I really appreciate that response and forgive me if i am rude, but i did not understand a single word you said. I am really, really new. I think the are just asking me to use a program to "overload"( they didnt really explain this well) to convert and add two different types of numbers such as an int and double. And i cant use strings since that is in another section way ahead of this. Isnt this a great course?
    Last edited by ilop12; 06-03-2010 at 02:39 AM.

Similar Threads

  1. Where to place the jar files..?
    By Stephen Douglas in forum New To Java
    Replies: 26
    Last Post: 03-26-2010, 04:50 PM
  2. Edit In place for JSF
    By akira_mz86@yahoo.com in forum New To Java
    Replies: 2
    Last Post: 02-24-2010, 10:35 AM
  3. Where to place jar files
    By rummy in forum New To Java
    Replies: 3
    Last Post: 02-11-2010, 01:08 AM
  4. how to place the controls >
    By makpandian in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 06:17 AM
  5. how to place a divider of the splitpane?
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •