0

This is my main class:

import java.util.Scanner;

public class calc {
public static void main(String[] args){
    Scanner variablea = new Scanner(System.in);
    Scanner variableb = new Scanner(System.in);
    Scanner variablec = new Scanner(System.in);
    int a1, b1, c1;
    System.out.println("enter your 'A' variable");
    a1 = variablea.nextInt();
    System.out.println("enter your 'B' variable");
    b1 = variableb.nextInt();
    System.out.println("enter your 'C' variable");
    c1 = variablec.nextInt();

    algorithm algorithmObject = new algorithm();
    algorithmObject.algorithm(a1, b1, c1);

}

}

and this is the second one

      public class algorithm{
public void algorithm(int a, int b, int c){
    double x1;
    double square = Math.sqrt(b*b - 4*a*c);
    double numerator = b*-1 + square;
    double finalanswer = numerator/2*a;

    System.out.println(finalanswer);
}

}

Eclipse doesn't give me any errors, but after it asks for my 3 variables and I enter them, it just gives NaN. Any idea what I have done wrong?

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
user2350474
  • 3
  • 1
  • 3
  • 1
    NaN is Not-A-Number - if that helps – Scott Selby May 04 '13 at 18:47
  • You can change `b*-1` to `-b`, which looks a little cleaner – nullptr May 04 '13 at 18:49
  • Side note: I am not a Java expert, but it is really necessary to use three scanners? – Martin R May 04 '13 at 18:55
  • 1
    Another note: Watch out for order of operations: `double finalanswer = numerator/(2*a);` – Anna Brenden May 04 '13 at 18:59
  • You forgot to ask a question. This makes it awfully hard to answer. What were you expecting? – David Schwartz May 04 '13 at 18:59
  • Martin R, what should I have done? when I try doing Scanner variablea, variableb, variablec = new Scanner(System.in); I just get errors on lines 12 and 14 saying the scanners aren't initialized – user2350474 May 04 '13 at 19:37
  • @user2350474: Create just one scanner: `Scanner sc = new Scanner(System.in);` and use it for all input: `a1 = sc.nextInt(); ... ; b1 = sc.nextInt(); ...`. But as I said, I am not a Java expert1 – Martin R May 04 '13 at 19:48
  • possible duplicate of [Why Quadratic equation's root result is NaN ? (Java)](http://stackoverflow.com/questions/7461803/why-quadratic-equations-root-result-is-nan-java) – Praveen May 06 '13 at 08:49

3 Answers3

2

There are issues with the code but the culprit is most likely this line:

double square = Math.sqrt(b*b - 4*a*c);

If b*b - 4*a*c is negative (there is no solution to the equation) then square is NaN and every computation involving it will be NaN as well. You can check it here.


You could improve your calculator by first checking if b*b - 4*a*c < 0 and if it is so then you could write to the console that there is no real solution (and of course stop the computations there).


I would change public void algorithm(int a, int b, int c) to

public void algorithm(double a, double b, double c)

Integer arithmetic can surprise you when you least expect it and I see no reason why a, b and c should be constrained to be int-s.


OK, hope this helped.

Ali
  • 56,466
  • 29
  • 168
  • 265
  • I think I got it. i made a separate variable for whats inside the sqrt called root. i made that absolute value and then squared that. double x1; double root = Math.abs(b*b - 4*a*c); double square = Math.sqrt(root); – user2350474 May 04 '13 at 19:50
  • 1
    No. It `b*b - 4*a*c` is negative, then there is no real solution to your equation. See http://en.wikipedia.org/wiki/Quadratic_equation#Discriminant – Ali May 04 '13 at 19:53
1

There are several special cases that you need to watch out for. You don't seem to watch for any of them:

y = a*x^2 + b*x + c
  1. If the quadratic coefficient a is zero, there's only one root because the equation is linear: y = b*x + c.
  2. If the linear coefficient b is zero, there are two roots: [x1, x2] = +/-sqrt(c)
  3. If the constant coefficient c is zero, one of the roots is zero and the other is -b/a.

If the discriminant is negative, you have two complex conjugate roots.

The interesting thing is that all these situations have meaning for the solutions of physics problems like damped harmonic motion and L-C-R circuits. You should learn something about those, too.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

As this looks a bit a homework assignment, I'll only give a hint:

NaN is what math functions return when the result can not be accurately represented as a number for some reason, either technical or mathematical.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • that's what I put as a comment - I wan't too sure if I should count that as an answer or not – Scott Selby May 04 '13 at 18:49
  • @ScottSelby True - I didn't see your comment until I had already posted this. The answer should be enough to help the poster out, though, while still having him learn something. At least, that's the idea. – Wander Nauta May 04 '13 at 18:52
  • I am not even in school. I am just a teen using thenewboston's java tutorials and I was just playing around with what I've learned – user2350474 May 04 '13 at 19:34