Jump to content

Why is this returning (easy java)

Go to solution Solved by Bene,

The order of parameters are wrong for the function handleradicand call

 

https://ideone.com/UvmqJC

 

I created a program for the quadratic formula in java with my current knowledge, but its returning NaN after I plug in the coefficients and it does the calculation.  I think I found the, "ROOT"(pun), of the issue.  in the method, rootradicand i have a Math.sqrt, when i remove that it does not return nan, but it obviously is incorrect, source code below, thanks for the help in advance.
 
 


I created a program for the quadratic formula in java with my current knowledge, but its returning NaN after I plug in the coefficients and it does the calculation.  I think I found the, "ROOT"(pun), of the issue.  in the method, rootradicand i have a Math.sqrt, when i remove that it does not return nan, but it obviously is incorrect, source code below, thanks for the help in advance.

 

 
/** * Write a description of class quadraticformula here. *  * @author (Gabe Spound)  * @[member=versions] (8/21/15) */import java.util.Scanner;import java.util.*;public class quadraticformula {    public double handleradicand(double b, double a, double c){ //Step one, calculate the radicand(the part inside the square root)        double radicand = b * b - (4 * a * c); // b squared minus 4ac        return radicand; //return the radicand    }    public double rootradicand(double rad, double e){ //find the square root of the radical        double radical = Math.sqrt(rad); // square root of b squared minus 4ac        return radical; //return the radical    }    public double divideBby2a(double b, double a) { //finding  -b over 2a and the square root over 2a and storing them in seperate variables.        double zerominusb = 0 - b; //storing -b as a variable because I wasn't sure if I could just type -b in a calculation        double bover2a = zerominusb / (2 * a); //-b over 2a        return bover2a;    }    public double divideRADby2a(double rad, double a){        double radover2a = rad / (2 * a); // the radical over 2a        return radover2a;    }    public double findroot1(double BO2A, double RO2A){ //find the roots of the equation        double first_X = BO2A + RO2A; // -b over 2a plus the radical over 2a        return first_X; //return the first root    }    public double findroot2(double BO2A, double RO2A){        double second_X = BO2A - RO2A;        return second_X;    }    public void Main(String[] args){ //execute the previously defined methods to calculate the quadratic equation.        System.out.println("What is the A value?"); //Asking the user for the A value in their quadratic equation.        Scanner Ascanner = new Scanner(System.in); //Created a new scanner variable for the A value.        double a = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the A value.        System.out.println("What is the B value?"); //Asking the user for the B value in their quadratic equation.        Scanner Bscanner = new Scanner(System.in); //Created a new scanner variable for the B value.        double b = Bscanner.nextInt(); //Storing the next integer of that scanner variable for an Integer for the B value.        System.out.println("What is the C value?"); //Asking the user for the C value in their quadratic equation.        Scanner Cscanner = new Scanner(System.in); //Created a new scanenr variable for the C value.        double c = Cscanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the C value.        handleradicand(a, b, c); //Finding the radicand        double tem1 = handleradicand(a,b,c);        rootradicand(tem1, .5); //Finding the radical        double tem2 = rootradicand(tem1, .5);        divideBby2a (b,a);        divideRADby2a (tem2, a);        double tem3 = divideBby2a (b,a);        double tem4 = divideRADby2a (tem2, a);        findroot1 (tem3, tem4);        double firstX = findroot1 (tem3, tem4);        findroot2 (tem3, tem4);        double secondX = findroot2 (tem3, tem4);        System.out.println("Your roots are X = " + firstX + "and X = " + secondX); //displaying the roots    }}
Edited by colonel_mortis
Code tags

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to comment
https://linustechtips.com/topic/437990-why-is-this-returning-easy-java/
Share on other sites

Link to post
Share on other sites

Post this is the software section next time.

@colonel_mortis please move to the coding section.

sorry, thought this would be the right place

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to post
Share on other sites

The order of parameters are wrong for the function handleradicand call

 

https://ideone.com/UvmqJC

 

I created a program for the quadratic formula in java with my current knowledge, but its returning NaN after I plug in the coefficients and it does the calculation.  I think I found the, "ROOT"(pun), of the issue.  in the method, rootradicand i have a Math.sqrt, when i remove that it does not return nan, but it obviously is incorrect, source code below, thanks for the help in advance.
 
 


Link to post
Share on other sites

Math.sqrt(rad) returns NaN when rad is negative. You'll should to test for negative numbers before taking the square root.

 

@littlepigboy5 follow your topics to get notifications

 

The order of parameters are wrong for the function handleradicand call

 

https://ideone.com/UvmqJC

keep in mind, im new to java, can you explain a bit more please

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to post
Share on other sites

keep in mind, im new to java, can you explain a bit more please

Let me see if I can help you out. Like the user above said, your function parameters were ordered wrongly for handleradicand. You had:

public double handleradicand(double b, double a, double c) 

where you should have (notice how the first 2 parameters' orderings have swapped):

public double handleradicand(double a, double b, double c)

With what you had previously, the line

double tem1 = handleradicand(a,b,c);

calculates a (sometimes) negative number which is stored in a variable tem1. This variable tem1 is passed into the method rootradicand by the line

double tem2 = rootradicand(tem1, .5);

This is where it your compiler throws an error. Rootradicand has a line that calculates the square root of tem1 - if you try to calculate a square root of a negative number (i.e. tem1), then it throws the error NaN which is exactly what you're getting. Try this in a compiler - open your command prompt and type javac, then type "import Java.lang.Math;*" followed by "Math.sqrt(-5)" and see what you get.

 

 

Hope this helps.

 

Another note: I'd recommend you keep your methods static. You don't need to create any object to call these methods, and consequently it doesn't make sense for your methods to be non-static. Your methods do something that doesn't depend on individual characteristics of a class (i.e. you only have one a, b and c term per class, and not one per object).

 

There's no harm in doing so (right now), but it does no harm to get into good programming practice. If I've confused you, then theres a good chance you haven't gone over objects yet, so just discard the above paragraph.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×