By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,965 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,965 IT Pros & Developers. It's quick & easy.

Java Methods ( for experts - :-) )

P: n/a
KL
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;
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"KL" <kh**********@hotmail.com> wrote in message
news:6W****************@news20.bellglobal.com...
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;
}
}


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;
}
Jul 17 '05 #2

P: n/a
KL
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
Jul 17 '05 #3

P: n/a
"KL" <kh**********@hotmail.com> wrote in message
news:xn****************@news20.bellglobal.com...
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


No, there is no difference between the two. There must be something else.
Jul 17 '05 #4

P: n/a
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" <kh**********@hotmail.com> wrote in message news:<6W****************@news20.bellglobal.com>...
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;
}
}

Jul 17 '05 #5

P: n/a
no*****************@scovetta.com (Michael Scovetta) wrote in message news:<8c**************************@posting.google. com>...
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";


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- ><>
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.