Java Methods ( for experts - :-) )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KL

    Java Methods ( for experts - :-) )

    considering my prof asked the question below, I wrote the code (also shown
    below)..... however it is wrong......so I any help as to where I'm going
    wrong will be appreciated

    Question Asked:
    =============== ==

    Write the definition of a class Telephone. The class has no constructors,
    one instance variable of type String called number, and two static
    variables. One is of type int called quantity; the other is of type double
    called total. Besides that, the class has one static method makeFullNumber.
    The method accepts two arguments, a String containing a telephone number and
    an int containing an area code. The method concatenates the two arguments in
    the following manner: First comes the area code, then a dash, then the
    telephone number. The method returns the resultant string.

    (cannot initiate your own identifier for code and number --- because it
    returns errors)



    Answer Given:
    =============== ==
    class Telephone {
    String number;
    static int quantity;
    static double total;
    static String makeFullNumber( ){
    return "416-"+number;
    }
    }


  • Ryan Stewart

    #2
    Re: Java Methods ( for experts - :-) )

    "KL" <khoorooslary@h otmail.com> wrote in message
    news:6W6Tb.30$9 U5.13999@news20 .bellglobal.com ...[color=blue]
    > considering my prof asked the question below, I wrote the code (also shown
    > below)..... however it is wrong......so I any help as to where I'm going
    > wrong will be appreciated
    >
    > Question Asked:
    > =============== ==
    >
    > Write the definition of a class Telephone. The class has no constructors,
    > one instance variable of type String called number, and two static
    > variables. One is of type int called quantity; the other is of type double
    > called total. Besides that, the class has one static method[/color]
    makeFullNumber.[color=blue]
    > The method accepts two arguments, a String containing a telephone number[/color]
    and[color=blue]
    > an int containing an area code. The method concatenates the two arguments[/color]
    in[color=blue]
    > the following manner: First comes the area code, then a dash, then the
    > telephone number. The method returns the resultant string.
    >
    > (cannot initiate your own identifier for code and number --- because it
    > returns errors)
    >
    >
    >
    > Answer Given:
    > =============== ==
    > class Telephone {
    > String number;
    > static int quantity;
    > static double total;
    > static String makeFullNumber( ){
    > return "416-"+number;
    > }
    > }[/color]

    Hardly expert material. After reading the problem:
    1) You can't make a class with no constructors. You can make one with none
    declared, and I suppose that's what he's asking for.
    2) What are the number, quantity, and total variables for?
    3) What does the last bit in parentheses mean?

    As for your solution, the method doesn't do at all what he asked. It's
    supposed to take two parameters. It takes none. How about:
    static String makeFullNumber( String number, int areaCode) {
    return areaCode + "-" + number;
    }


    Comment

    • KL

      #3
      Re: Java Methods ( for experts - :-) )

      heh

      Ryan. . as a newb i didn't think doing it as

      makeFullNumber (String x, int y) { ...}
      would be different than
      makeFullNumber (int y, String x) { ... }

      but the correcting program that kept giving me errors did differentiate

      ok...... now i'm fine

      thanks for the reply ( i know it wasn't expert material.... that's why i had
      :-) beside it)

      cheers


      Comment

      • Ryan Stewart

        #4
        Re: Java Methods ( for experts - :-) )

        "KL" <khoorooslary@h otmail.com> wrote in message
        news:xn7Tb.52$9 U5.19739@news20 .bellglobal.com ...[color=blue]
        > heh
        >
        > Ryan. . as a newb i didn't think doing it as
        >
        > makeFullNumber (String x, int y) { ...}
        > would be different than
        > makeFullNumber (int y, String x) { ... }
        >
        > but the correcting program that kept giving me errors did differentiate[/color]

        No, there is no difference between the two. There must be something else.


        Comment

        • Michael Scovetta

          #5
          Re: Java Methods ( for experts - :-) )

          KL,
          It's important to get past the 'newbie' stage as fast as possible--
          it's easy to develop bad habits. Take a look at the following code as
          an example of what to-do. One note that might have been giving you
          problems: Don't say:
          int i = 5;
          String s = i + "foo";
          because it will say ooh, I have an integer, and I need to add
          something to it. Since (+) is both an integer operator and a string
          concat operator, it might generate an error or warning. Either say:
          String s = "" + i + "foo";
          or
          String s = String.valueOf( i) + "foo";

          ---

          /**
          * Telephone class
          */
          class Telephone {
          /** Number, not used */
          private String number = null;
          /** Quantity, not used */
          private static int quantity = 0;
          /** Total, not used */
          private static double total = 0.0d;

          /**
          * Creates a full telephone number from the telephone
          * number and area code.
          * @param telephoneNumber telephone number
          * @param areaCode area code
          * return [area code]-[telephone number]
          */
          public static void makeFullNumber( String telephoneNumber ,
          int areaCode ) {
          return String.valueOf( areaCode) + "-" + telephoneNumber );
          }
          }


          "KL" <khoorooslary@h otmail.com> wrote in message news:<6W6Tb.30$ 9U5.13999@news2 0.bellglobal.co m>...[color=blue]
          > considering my prof asked the question below, I wrote the code (also shown
          > below)..... however it is wrong......so I any help as to where I'm going
          > wrong will be appreciated
          >
          > Question Asked:
          > =============== ==
          >
          > Write the definition of a class Telephone. The class has no constructors,
          > one instance variable of type String called number, and two static
          > variables. One is of type int called quantity; the other is of type double
          > called total. Besides that, the class has one static method makeFullNumber.
          > The method accepts two arguments, a String containing a telephone number and
          > an int containing an area code. The method concatenates the two arguments in
          > the following manner: First comes the area code, then a dash, then the
          > telephone number. The method returns the resultant string.
          >
          > (cannot initiate your own identifier for code and number --- because it
          > returns errors)
          >
          >
          >
          > Answer Given:
          > =============== ==
          > class Telephone {
          > String number;
          > static int quantity;
          > static double total;
          > static String makeFullNumber( ){
          > return "416-"+number;
          > }
          > }[/color]

          Comment

          • FISH

            #6
            Re: Java Methods ( for experts - :-) )

            nospam_googlegr [email protected] om (Michael Scovetta) wrote in message news:<8cbbc1c4. 0402011859.2284 [email protected] ogle.com>...[color=blue]
            > KL,
            > It's important to get past the 'newbie' stage as fast as possible--
            > it's easy to develop bad habits. Take a look at the following code as
            > an example of what to-do. One note that might have been giving you
            > problems: Don't say:
            > int i = 5;
            > String s = i + "foo";
            > because it will say ooh, I have an integer, and I need to add
            > something to it. Since (+) is both an integer operator and a string
            > concat operator, it might generate an error or warning. Either say:
            > String s = "" + i + "foo";
            > or
            > String s = String.valueOf( i) + "foo";[/color]

            If the 'it' you are refering to is a compiler, then either i+"" or ""+i
            are valid, and will result in a String. ""+i+"Foo" has the same result
            as i+"Foo" therefore - although the former will have an extra
            unnecessary step (which some compilers may optimise away!)

            (If the 'it' is some kind of code verifying tool, then you should post
            a bug report with the author if it doesn't handle i+"Foo" .)


            -FISH- ><>

            Comment

            Working...