?
Solved

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

Posted on 2012-04-07
9
Medium Priority
?
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 39

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 39

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 28

Accepted Solution

by:
dpearson earned 2000 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 39

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

765 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