Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parse to double required? variable declarations

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found a site with some simple problems that I'm working through as a refresher/learning. I keep getting an error and think that:
a. my code is sloppy - do I need to declare variables elsewhere?
b. I'm having problems with the rad calculation as its calculating with int and double which is giving an error?
c. inData * inData seems silly to gets its square. Is there a Math function for square. Couldn't find one.

thanks for any help.

 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) It's a little cleaner to declare the variables only when they get some meaning. Say

String inData = stdin.readLine();;
int num = ...;
double rad = ...;

No reason to declare it with nonsense values.

b) Yes, you do need to parse that string. You can't multiply strings by strings.

c) Which would be sillier, typing x*x, or using static method of another class to do the same thing?
[ November 07, 2004: Message edited by: Nick George ]
 
Patrick Haley
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a. Not so much. I could read it.

I might have called the final value holder *area* instead of *rad* because that is what you calculate.

b. Actually, your calculation is being done with a String object (inData) and a double (Math.PI).

Remember, you read the buffer into a String, and then you parse the int with the Integer.parseInt method. Changing inData to num in the line of code above will fix the problem.

c. Math.pow(num, 2) will square it for you, but you would be splitting hairs on this.

Keep coding!
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,
I want to help you...
This is my answers for you..
a. Yes, you can declare variable anywhere before its is used.
b. I have updated your code ( and I fix some errors) like this..



c. You can use Math.sqrt(double a).

Correct me if I am wrong
hope this helps...

daniel
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a. my code is sloppy - do I need to declare variables elsewhere?
Many programmers declare local variables on or just before their first use. It makes it easier when you're reading someone else's code.
b. I'm having problems with the rad calculation as its calculating with int and double which is giving an error?
You just have two errors, both in one line - you're missing a ; and your expression should be num*num.
c. inData * inData seems silly to gets its square. Is there a Math function for square. Couldn't find one.
There is Math.pow(a,b) to compute a^b, but Math.pow( inData, 2 ) seems like overkill.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps, I am splitting hairs as Fisher says. However, I also think it's good for a programmer to understand the advantages and disadvantages associated with two different ways of accomplishing the same task. This will allow said programmer to make an informed decision about which way is "best" for the current situation.

With that said, I typically prefer "x * x" over "Math.pow(x, 2)" for several reasons:

1) less typing
2) faster execution (i.e. calling a method has a slight overhead associated with it)

HTH

Layne
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow that helped. I was doing a lot of those changes but forgot some small ones that made the whole thing complete. So I kept getting an error. So, I understand parsing num to double, but here's the stupid part that I forgot to include.

The assignment says,

Each of these programs calls for reading integer data from the keyboard. (Of course you will have to read in character data and convert it to an int using the wrapper class Integer.) Once you have read in an integer it is OK to do double precision arithmetic with it. Just be sure that your arithmetic expressions don't accidentally call for integer math when you don't want it.


So is it me or does the assignment as for extra steps that aren't necessary? You parse the String num to int as required then parse it again to double for the math? That just doesn't make sense. Why not just bring it in as a double especially since the radius doesn't necessarily have to be an integer in the real world.
[ November 08, 2004: Message edited by: Matt Fielder ]
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The wrapper class Double has a parseDouble(String s) method, so you have no real need to parse to the Integer class, but I think your assignment calls for that unnecessary step. This is probably so that you learn casting.
 
Patrick Haley
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something in a piece of code that does double precision math with an integer.



Then read about conversions and promotions.
Conversions and Promotions
Especially Numeric Promotion.


[ November 08, 2004: Message edited by: Patrick Haley ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic