This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
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)
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 ]
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.
Joined: Sep 15, 2004
Try something in a piece of code that does double precision math with an integer.