Reputation: 1
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);
}
}
Upvotes: 0
Views: 174
Reputation: 909
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 ....
Upvotes: 0
Reputation: 1523
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);
}
}
Upvotes: 0
Reputation: 795
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)
Upvotes: 1
Reputation: 53565
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]
Upvotes: 2
Reputation: 173
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.
Upvotes: 0
Reputation: 121
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);
}
}
Upvotes: 0