Link to home
Start Free TrialLog in
Avatar of EMB01
EMB01Flag for United States of America

asked on

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.
Avatar of ramrom
ramrom
Flag of United States of America image

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.
Avatar of EMB01

ASKER

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...
Avatar of Aaron Tomosky
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?
Avatar of EMB01

ASKER

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).
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.
Avatar of EMB01

ASKER

>>  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).
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EMB01

ASKER

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));
      
      }

}
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.