 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jstorta Guest
|
Posted: Sun Jul 17, 2005 2:01 am Post subject: Problem pasing shorts to methods |
|
|
I am just starting with Java and I am writing a program to do some
mortgage calculations.
I have a method, setAllValues, that accepts a couple short values, in
addition to some floats. I use this method inside the constructors for
the class.
It works fine in my default constructor, but not when I use an
overloaded constructor to pass different values. It looks like it is
getting confused and thinks I am passing ints instead of shorts. If I
change all of the shorts to ints it works fine.
I can't figure out why it is not working with shorts.
When I try to compile it, I get these messages.
----------------------------------------------------------
mort.java:4: cannot find symbol
symbol : constructor Mortgage(float,float,float,int,int)
location: class Mortgage
Mortgage secondMortgage = new Mortgage( 200000f, 7.75f, (7.75f /
12), 30, 360 );
^
mort.java:21: setAllValues(float,float,float,short,short) in Mortgage
cannot be applied to (float,float,float,int,int)
setAllValues( 150000f, 6.75f, (6.75f / 12), 30, 360 );
^
2 errors
Here is the code
--------------------------------------------------------
public class mort {
public static void main( String [] args ) {
Mortgage firstMortgage = new Mortgage();
Mortgage secondMortgage = new Mortgage( 200000f, 7.75f, (7.75f /
12), 30, 360 );
System.out.println( "First Mortgagen" );
firstMortgage.printAllValues();
System.out.println( "Second Mortgagen" );
secondMortgage.printAllValues();
}
}
class Mortgage {
float principal;
float annualRate, monthlyRate;
short termYears, termMonths;
Mortgage() {
setAllValues( 150000f, 6.75f, (6.75f / 12), 30, 360 );
}
Mortgage( float newPrincipal, float newAnnualRate, float
newMonthlyRate,
short newTermYears, short newTermMonths ) {
setAllValues( newPrincipal, newAnnualRate, newMonthlyRate,
newTermYears, newTermMonths );
}
void setAllValues( float newPrincipal, float newAnnualRate, float
newMonthlyRate,
short newTermYears, short newTermMonths ) {
principal = newPrincipal;
annualRate = newAnnualRate;
monthlyRate = newMonthlyRate;
termYears = newTermYears;
termMonths = newTermMonths;
}
void printAllValues() {
System.out.println( "Principal:t" + principal );
System.out.println( "Interest Rate:t" + annualRate + " (" +
monthlyRate + "/month)" );
System.out.println( "Term:t" + termYears + " (" + termMonths +
"mos)" );
}
}
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Sun Jul 17, 2005 4:09 am Post subject: Re: Problem pasing shorts to methods |
|
|
jstorta wrote:
| Quote: | I am just starting with Java and I am writing a program to do some
mortgage calculations.
I have a method, setAllValues, that accepts a couple short values, in
addition to some floats. I use this method inside the constructors for
the class.
It works fine in my default constructor, but not when I use an
overloaded constructor to pass different values. It looks like it is
getting confused and thinks I am passing ints instead of shorts. If I
change all of the shorts to ints it works fine.
I can't figure out why it is not working with shorts.
When I try to compile it, I get these messages.
----------------------------------------------------------
mort.java:4: cannot find symbol
symbol : constructor Mortgage(float,float,float,int,int)
location: class Mortgage
Mortgage secondMortgage = new Mortgage( 200000f, 7.75f, (7.75f /
12), 30, 360 );
^
mort.java:21: setAllValues(float,float,float,short,short) in Mortgage
cannot be applied to (float,float,float,int,int)
setAllValues( 150000f, 6.75f, (6.75f / 12), 30, 360 );
^
2 errors
|
30 and 360 are ints, not shorts. There are some contexts, the assignment
conversions, in which int constant expressions that fit in the short
range are automatically converted to short.
The relevant conversion for your parameters is method invocation conversion:
"Method invocation conversions specifically do not include the implicit
narrowing of integer constants which is part of assignment conversion
(§5.2). The designers of the Java programming language felt that
including these implicit narrowing conversions would add additional
complexity to the overloaded method matching resolution process
(§15.12.2)."
[http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#12687]
Use an explicit cast to short: (short)30 and (short)360.
Patricia
|
|
| Back to top |
|
 |
John B. Matthews Guest
|
Posted: Sun Jul 17, 2005 5:27 am Post subject: Re: Problem pasing shorts to methods |
|
|
In article <1121565600.417011.305760 (AT) g43g2000cwa (DOT) googlegroups.com>,
"jstorta" <john (AT) storta (DOT) net> wrote:
| Quote: | I am just starting with Java and I am writing a program to do some
mortgage calculations.
I have a method, setAllValues, that accepts a couple short values, in
addition to some floats. I use this method inside the constructors for
the class.
It works fine in my default constructor, but not when I use an
overloaded constructor to pass different values. It looks like it is
getting confused and thinks I am passing ints instead of shorts. If I
change all of the shorts to ints it works fine.
I can't figure out why it is not working with shorts.
|
The underlying problem is that there's no tag to specify a short
integer, as there is for float or long. The JLS, sec. 3.10.1*, says, "An
integer literal is of type long if it is suffixed with an ASCII letter L
or l (ell); otherwise it is of type int." One way to deal with this is
to use a casting conversion:
Mortgage secondMortgage = new Mortgage(
200000f, 7.75f, (7.75f / 12), (short) 30, (short) 360 );
*<http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html
#19247>
| Quote: | When I try to compile it, I get these messages.
----------------------------------------------------------
mort.java:4: cannot find symbol
symbol : constructor Mortgage(float,float,float,int,int)
location: class Mortgage
Mortgage secondMortgage = new Mortgage( 200000f, 7.75f, (7.75f /
12), 30, 360 );
^
mort.java:21: setAllValues(float,float,float,short,short) in Mortgage
cannot be applied to (float,float,float,int,int)
setAllValues( 150000f, 6.75f, (6.75f / 12), 30, 360 );
^
2 errors
[...] |
I'm not sure it's working in either place. The first error message is
saying that there's no constructor named Mortgage with the signature you
want. Mortgage itself hasn't been compiled yet. The compiler is
inferring the name and signature of the required constructor from your
instantiation, and it can't find a matching class named Mortgage.
The second error message says: the method setAllValues requires three
floats and two shorts; you've called it with three floats and two ints.
It may be easier to see what's going on if you put the static method
main at the end of class Mortgage and don't use class mort.
Hope this helps.
--
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Sun Jul 17, 2005 1:16 pm Post subject: Re: Problem pasing shorts to methods |
|
|
John B. Matthews wrote:
| Quote: | In article <1121565600.417011.305760 (AT) g43g2000cwa (DOT) googlegroups.com>,
"jstorta" <john (AT) storta (DOT) net> wrote:
I am just starting with Java and I am writing a program to do some
mortgage calculations.
I have a method, setAllValues, that accepts a couple short values, in
addition to some floats. I use this method inside the constructors for
the class.
It works fine in my default constructor, but not when I use an
overloaded constructor to pass different values. It looks like it is
getting confused and thinks I am passing ints instead of shorts. If I
change all of the shorts to ints it works fine.
I can't figure out why it is not working with shorts.
The underlying problem is that there's no tag to specify a short
integer, as there is for float or long. The JLS, sec. 3.10.1*, says, "An
integer literal is of type long if it is suffixed with an ASCII letter L
or l (ell); otherwise it is of type int." One way to deal with this is
to use a casting conversion:
Mortgage secondMortgage = new Mortgage(
200000f, 7.75f, (7.75f / 12), (short) 30, (short) 360 );
*<http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html
#19247
.... |
The lack of a tag is not really a problem because the result of taking a
string such as "30", converting it to int, and then casting that int to
short is exactly the same as the result of a direct conversion to short.
You don't lose anything by writing "(short)30".
Patricia
|
|
| Back to top |
|
 |
John B. Matthews Guest
|
Posted: Sun Jul 17, 2005 5:05 pm Post subject: Re: Problem pasing shorts to methods |
|
|
In article <uSsCe.9107$8f7.321 (AT) newsread1 (DOT) news.pas.earthlink.net>,
Patricia Shanahan <pats (AT) acm (DOT) org> wrote:
| Quote: | John B. Matthews wrote:
In article <1121565600.417011.305760 (AT) g43g2000cwa (DOT) googlegroups.com>,
"jstorta" <john (AT) storta (DOT) net> wrote:
I am just starting with Java and I am writing a program to do some
mortgage calculations.
I have a method, setAllValues, that accepts a couple short values, in
addition to some floats. I use this method inside the constructors for
the class.
It works fine in my default constructor, but not when I use an
overloaded constructor to pass different values. It looks like it is
getting confused and thinks I am passing ints instead of shorts. If I
change all of the shorts to ints it works fine.
I can't figure out why it is not working with shorts.
The underlying problem is that there's no tag to specify a short
integer, as there is for float or long. The JLS, sec. 3.10.1*, says, "An
integer literal is of type long if it is suffixed with an ASCII letter L
or l (ell); otherwise it is of type int." One way to deal with this is
to use a casting conversion:
Mortgage secondMortgage = new Mortgage(
200000f, 7.75f, (7.75f / 12), (short) 30, (short) 360 );
*<http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html
#19247
...
The lack of a tag is not really a problem because the result of taking a
string such as "30", converting it to int, and then casting that int to
short is exactly the same as the result of a direct conversion to short.
You don't lose anything by writing "(short)30".
Patricia
|
Quite right. I did not mean to suggest adding new tags:-) I only meant
that, absent another way to signify a literal short, an explicit cast
was required.
Your point (in another article) about implicit narrowing conversions in
method invocations is particularly cogent (JLS §5.2).
Thanks!
--
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
|
|
| Back to top |
|
 |
George Cherry Guest
|
Posted: Mon Jul 18, 2005 7:11 pm Post subject: Re: Problem pasing shorts to methods |
|
|
jstorta wrote:
| Quote: | I am just starting with Java and I am writing a program to do some
mortgage calculations.
I have a method, setAllValues, that accepts a couple short values, in
addition to some floats. I use this method inside the constructors for
the class.
It works fine in my default constructor, but not when I use an
overloaded constructor to pass different values. It looks like it is
getting confused and thinks I am passing ints instead of shorts. If I
change all of the shorts to ints it works fine.
I can't figure out why it is not working with shorts.
When I try to compile it, I get these messages.
----------------------------------------------------------
mort.java:4: cannot find symbol
symbol : constructor Mortgage(float,float,float,int,int)
location: class Mortgage
Mortgage secondMortgage = new Mortgage( 200000f, 7.75f, (7.75f /
12), 30, 360 );
^
mort.java:21: setAllValues(float,float,float,short,short) in Mortgage
cannot be applied to (float,float,float,int,int)
setAllValues( 150000f, 6.75f, (6.75f / 12), 30, 360 );
^
2 errors
|
Why are you using shorts. Why are you not using ints?
GWC
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|