0

I'm a new java programmer and I'm trying to write a program that finds the roots of a quadratic equation by implementing the roots() method in this class.

I think I've figured out how to implement the equation, but the return statement says: Error-Type mismatch: cannot convert from double to Set

How would I fix this error?

Thank You!

 package warmup;

import java.util.Set;

 public class Quadratic {

/**
 * Find the integer roots of a quadratic equation, ax^2 + bx + c = 0.
 * @param a coefficient of x^2
 * @param b coefficient of x
 * @param c constant term.  Requires that a, b, and c are not ALL zero.
 * @return all integers x such that ax^2 + bx + c = 0.
 */
public static Set<Integer> roots(int a, int b, int c) {

    //my code so far
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
    return q;
}


/**
 * Main function of program.
 * @param args command-line arguments
 */
public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    Set<Integer> result = roots(1, -4, 3);
    System.out.println(result);
  }


}
5
  • The error is self-explanatory: you declare that the method returns Set<Integer> in the method's signature, but you return a double. Commented Nov 3, 2016 at 6:01
  • It is required to use Set? Commented Nov 3, 2016 at 6:01
  • I'm assuming you left out the part where you also calculate -b - ... and return both values at once. That really should be a Set<Double> though. Commented Nov 3, 2016 at 6:03
  • double q && return q != Set<Integer> Commented Nov 3, 2016 at 6:03
  • the -b should be divided by 2a as well... Commented Nov 3, 2016 at 6:05

6 Answers 6

2

The following code contains a few fixes to the code in the question, see the comments in the code for further explanation:

public static Set<Double> roots(int a, int b, int c) {
    Double x = Math.sqrt(Math.pow(b, 2) - 4 * a * c);
    Set<Double> result = new HashSet<>(); // return a set that contains the results
    result.add((-b + x)/ 2 * a); // -b should be divided by 2a as well
    result.add((-b - x)/ 2 * a); // -b should be divided by 2a as well
    return result;
}

public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    Set<Double> result = roots(1, -4, 3); // the returned type is Set<Double> not Set<Integer> 
    System.out.println(result);
}

OUTPUT

For the equation x^2 - 4x + 3 = 0, the possible solutions are:
[1.0, 3.0]
Sign up to request clarification or add additional context in comments.

5 Comments

An answer that gets at the problem instead of simply changing the return type to double! The input should probably be validated to ensure that a isn't zero (amongst other things). I'm not sure how Java handles the square root of negative numbers to be honest.
as stated, ther could be no result, one should check Math.pow(b, 2) >= 4 * a * c
@Grexis it will throw an exception as you would expect it to in such cases. The API contract between you and the user could be determined to "swallow" these exceptions and return an empty Set for example, but I like the "fail fast" approach and allow the user to decide what they want to do with the exception. Pay attention that most Java libraries have the same approach!
@Turo in that case you'll get a set that contains NaN
@alfasin No argument there. I prefer the fail fast approach myself. @Turo It would be faster to check if Double.isNaN(x) since Math.sqrt() returns NaN for negative numbers (TIL).
1

The return type of your method is Set<Integer>. Returning a Set means that you can return zero, one, or more elements. This is appropriate since a quadratic equation has multiple roots in the general case.

<Integer> means that the elements of the set you are returning must be Integers, which is strange since roots of a quadratic equation are often real numbers and are sometimes complex numbers. I'd guess what you really want is Set<Double>.

If you really do want to return a single element, you will need to do so by returning a set to which you've added the root in question. Calling Collections.singleton() is a convenient way to do this.

Worthwhile reading:

https://docs.oracle.com/javase/6/docs/api/java/util/Set.html

https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#singleton(T)

Comments

0
public class Quadratic  {

public static double roots(int a, int b, int c) {

    //my code so far
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
    return q;
}
public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    double result = roots(1, -4, 3);
    System.out.println(result);
  }

}

1 Comment

This returns only one result, but when you calculate root you might get more than one...
0

You're returning a double variable instead of a Set object in the roots function. Hence the type mismatch. Modify it as:

public static double roots(int a, int b, int c) {

double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
return q;
}

Make sure to obtain the result of this function as a double as well. Change this:

 Set<Integer> result = roots(1, -4, 3);

to:

double result = roots(1, -4, 3);

This should work.

1 Comment

This returns only one result, but when you calculate root you might get more than one...
0
    import java.util.Set;
   import java.util.HashSet;

     public class Quadratic {

/**
 * Find the integer roots of a quadratic equation, ax^2 + bx + c = 0.
 * @param a coefficient of x^2
 * @param b coefficient of x
 * @param c constant term.  Requires that a, b, and c are not ALL zero.
 * @return all integers x such that ax^2 + bx + c = 0.
 */
    public static Set<Double> roots(int a, int b, int c) {
    Set<Double> result=new HashSet<>();
    //my code so far
     double p = (-b - (Math.sqrt(Math.pow(b, 2)-4*a*c)))/2*a;  
    result.add(p);
    double q = (-b + (Math.sqrt(Math.pow(b, 2)-4*a*c)))/2*a;  
    result.add(q);
    return result;
}


/**
 * Main function of program.
 * @param args command-line arguments
 */
public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    Set<Double> result = roots(1, -4, 3);
    System.out.println(result);
  }


}

Comments

0

Why are you using Set<Integer> as the return type, instead you can use Double. Replace Set<Integer> with Double and your problem is solved.

Note:- You are trying to return double , but your method return type is Set ....

1 Comment

While this is true - it doesn't answer the question and should be posted as a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.