Solved

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

Posted on 2012-04-07
9
562 Views
Last Modified: 2012-04-07
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.
0
Comment
Question by:EMB01
9 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 37819202
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
 
LVL 14

Author Comment

by:EMB01
ID: 37819251
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
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 37819267
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
 
LVL 14

Author Comment

by:EMB01
ID: 37819282
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 37819339
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
 
LVL 14

Author Comment

by:EMB01
ID: 37819359
>>  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
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 37819751
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
 
LVL 14

Author Closing Comment

by:EMB01
ID: 37819843
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
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 37819897
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now