This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "function" Watch "function" New topic
Author

function

Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
Hi


Can any body give the idea for the following function ...
round(<variablename>,x)

Where x = zeros to be rounded off to
Notes
a.Divide the variable by 10^x
b.Round up the value so arrived at
c.Multiply the value by 10^x again.

If round(21,1)
The function should perform the following activities:
Step a Output: 2.1
Step b Output: 2
Step c Output (Final output) = 2 * 10 = 20

If round(27,1)
The function should perform the following activities:
Stepa Output: 2.7
Step b Output: 3
Step c Output (Final output) = 3 * 10 = 30


If round(19876,3)
The function should perform the following activities:
Stepa Output: 198.76
Step b Output: 20
Step c Output (Final output) = 20 * 1000 = 20000


Can any body give me the good way of handling the above function ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Uhm...its hard to go into more detail than your assignment already gives you. People here don't tend to write code for you, rather suggest ways you can write it yourself. But since the question gives you pretty much all the detail you would probaly need, I can't see what else to add. What specifically is confusing you?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11249
    
  16

start small.

write a function that lets you pass in two variables, and print them out.

once that works, the next thing you need to to is compute 10^x. write that code and print out your result to make sure IT works.

at each step, TEST TEST TEST. i might even suggest leaving in all the print statements until you are done. the ones your just using for debug, you can do something like

System.out.println("DEBUG: blah blah blah");

then, when your all finished and are SURE everything works, you can quickly find the print statements with the word DEBUG and remove them.

Note that there are better ways of doing this, but this is pretty simple, and will get you pretty far...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
Also look at java.lang.Math.pow() and round().
 
wood burning stoves
 
subject: function