Expert Approach: Simple Square Root Program in Java with Data Validation

I am interested to see how an expert would approach a simple scenario such as this:

4. Write a Java program which finds the square root of a positive double number recursively using the following algorithm:

A) input the number, x

B) if the number entered for x is positive: continue.  If the number entered for x is negative or zero in value reject the number, display an error message and return to step A.

C) compute the first estimate of the square root of x by dividing x by 2.

D) compute the next estimate of the square root of x (call the next estimate ne) from the previous estimate of the square root of x (call the previous estimate pe) by using the formula:   ne = (pe + x / pe)/2.

E) continue to calculate new values for the estimate of the square root until the absolute value of (ne – pe) < 0.0001.

F) display the original number x and the estimated square root of x.

Thank you.
LVL 14
EMB01Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ramromconsultant Commented:
This sounds like homework. I think EE is not here to write your homework for you.

You have been given an algorithm. What steps have you taken to code it in Java?

Show us your work; tell us where you are stuck; then we can give you a hand.
0
EMB01Author Commented:
I would benefit more from seeing how an expert would write this code and studying it more than if I were to just take a random stab at it.  I think EE is a place for answering questions.  I'm an expert in PHP and if someone had a PHP "HOMEWORK" question, I wouldn't choose not to help them (as it appears you are doing).  I would help them just the same and I would help them learn PHP.  Strange that you would ignore my question simply because it's from a textbook...
0
Aaron TomoskySD-WAN SimplifiedCommented:
Php and java are very similar. I find it odd that you are an expert in php but can't make some skeleton code in java for this textbook question...How about you write it in php and we help you convert it to java?
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

EMB01Author Commented:
Well I don't really understand the question.  Here's how I would write it in PHP:

<?php

function sqrt($x)
{

      if ($x > 0)
      {
      
            $ne = 1;
            $pe = 1;
      
            while (($ne – $pe) < 0.0001)
            {
      
                  $pe = $x / 2;
                  $ne = ($pe + $x / $pe)/2;
                  echo $pe.'<br />';
                  echo $ne.'<br />';
            
            }
      
      }
      else
      {
            return 'x not positive';
      }

}

?>

Also, please stop being skeptical of me.  I am a PHP expert (look at some of my answered questions here on ee), but I don't have much time with school and work.  That's why I like to leverage experts exchange and I don't like to have to plead my case to get a simple question answered...

As for PHP and Java being similar...  They are somewhat syntactically similar, however, I hate java and I love PHP!  Maybe it would be better if I used eclipse (I just run from command line) but I always hate programming java, there's always something wrong with my code (precision error or something else).
0
Aaron TomoskySD-WAN SimplifiedCommented:
As an EE contributed I'm sure you can see the huge difference between helping someone Convert self written php to java, and answering a question out of a textbook.
0
EMB01Author Commented:
>>  As an EE contributed I'm sure you can see the huge difference between helping someone Convert self written php to java, and answering a question out of a textbook.

Not really, maybe I'm just weird (in a generous sort of way) in that if someone asked me to create a simple algorithm such as the one above in PHP, I wouldn't ask them questions as to the authenticity of their source for the question.  I would have just helped them...  So, I wrote it in PHP as you've requested by I've yet to get any java help...  Are you going to help me or not?  I could probably figure it out myself, but (as stated in the original question) I wanted to see how a JAVA EXPERT would do it (as this would be far more insightful).
0
dpearsonCommented:
It doesn't sound like homework to me.  Here's how I'd solve it (which seems a bit different from your PHP algorithm - I didn't test this code to see if it actually solves for square roots).

double sqrt(double x) {
   if (x <= 0)
     throw new IllegalArgumentException("x not positive");

     double ne = x/2 ;
     double pe = x ;
     while ( Math.abs(ne-pe) > 0.0001) {
	     pe = ne ;
             ne = (pe + x / pe) / 2;
      }
      return ne ;
   }

Open in new window


As you can see it's very similar to PHP.  At some point you start to realize that all languages are really the same - you just need to know how to map from your mental model to the one you happen to be writing in at the moment :)

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EMB01Author Commented:
Thanks.  I don't know why homework is evil here (to some).  Just help people when they need help.  Here's the final code:

public class sqrtApp
{

      public static double sqrt(double x) {
         if (x <= 0)
             throw new IllegalArgumentException("x not positive");

             double ne = x/2 ;
             double pe = x ;
             while ( Math.abs(ne-pe) > 0.0001) {
                   pe = ne ;
                         ne = (pe + x / pe) / 2;
              }
              return ne ;
      }
      
      public static void main(String[] args)
      {
      
            System.out.println(sqrt(8));
      
      }

}
0
Aaron TomoskySD-WAN SimplifiedCommented:
It's not evil, it's against the tos
Violating the guidelines for academic honesty or other unethical behavior; helping a student with a project is allowable, but not doing it for them.

School isn't about doing useful work, it's about learning.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.