This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes  Reverse Root Problem. My solution. Need check Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Reverse Root Problem. My solution. Need check" Watch " Reverse Root Problem. My solution. Need check" New topic
Author

Reverse Root Problem. My solution. Need check

Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
Condition:

Input
The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.
Output
For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.


Sample:

input:
1427 0

876652098643267843
5276538

output:
2297.0716
936297014.1164
0.0000
37.7757


My code below:


Judgement result:
Accepted

So, my first goal was successfully completed - I found solution for this exercise.
but, currently, I think about other point: code quality.
I would like reach high code quality level.

What do you think, this code is ok? Because, I am not sure, that my solution with processing InputStream is really cool.
Could you please suggest any improvements if it is need.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
No.
You should not have so much code in the main method; its ideal length is one statement.
Why are you using a Long?
Why are you putting Longs into a List?
Why have you got a byte[]?
Why are you using that dreadful method, System.in .read()?
Where did you get 262144 from? It says not more than 256kB. It does not say exactly 256kB.
Why are you using DecimalFormat rather than the % tags?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Who “accepted” that solution?
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
I uploaded code on http://acm.timus.ru/. On this server code was successfully executed and passed through couple tests. All happened automatically.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
Campbell Ritchie wrote:You should not have so much code in the main method; its ideal length is one statement.

Stating this in absolute terms is wrong, IMO - this is hardly an accepted best practice in the industry.

Why are you using that dreadful method, System.in .read()?

In what is clearly a learning exercise, I think there's nothing wrong with using it if the focus of the lesson lies elsewhere.

Why are you using DecimalFormat rather than the % tags?

Choosing one over the other is a matter of preference in my book, not an issue of code quality.


Ping & DNS - my free Android networking tools app
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
Why are you using a Long?

Because: digit between 0 ≤ A ≤ 10X18, but Integer.MAX_VALUE=2147483647
Why are you putting Longs into a List?

Because I dont know how much digits came from input string. In this case where can I save any number od digits? In array? But How?
Why have you got a byte[]?

Sorry?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You have given a good reason for the List, but not for the byte[]. If you don’t know what it is for, why don’t you get rid of it?
It said 1018 in the original post, which does not require a long, nor does 10×18. Do you mean 10 to the 18th power? That does require a long.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I ought to learn to read. You haven’t got a byte[]. What a stupid mistake of mine. Sorry about that
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
Yep, It is 10 x18. I don't know how insert superscript in the post message.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I didn’t know how to do that myself, but have done some research.

You can try ¹ (¹), (or ², ³) but I couldn’t seem to get it to work for 8. Try ² (or 1, 3) for ² and ⁰ for ⁰ (or &#x2074 for ⁴ etc). A little more information here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
¹ ² ³ = ¹ ² ³
[added 28th March 2014]In this post, Sudhir Srinivasan points out that \u207f is ⁿ
 
GeeCON Prague 2014
 
subject: Reverse Root Problem. My solution. Need check