the two solutions. If the quantity under the square root sign is negative, the e
ID: 3544782 • Letter: T
Question
the two solutions. If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect. You may assume that the value for a is nonzero. Allow the user to enter the number of equations he would like to solve.
Sample Run:
How man equations you have? 2
Enter coefficients for the quadratic equation ax2 + bx + c = 0:
a? 1
b? -5
c? 6
The first solution is 3.0
The second solution is 2.0
Enter coefficients for the quadratic equation ax2 + bx + c = 0:
a? 5
b? 1
c? 6
The equation has no real solution
5. Write a program that uses GUI to implement question 4. All your data should be taken using an input dialog and all result should be done using a message dialog. Notice that all data entered in an input dialog will be stored as a type string, you need to convert it into a integer using Integer.parseI
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class quadretic {
public static void main(String args[]) {
try {
int noEqn;
int a, b, c;
double d, solution1 = 0.0, solution2 = 0.0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the number of equations");
noEqn = Integer.parseInt(br.readLine());
for (int i = 0; i < noEqn; i++) {
System.out.println("Enter coefficients for the quadratic equation ax2 + bx + c = 0:");
System.out.println("a?");
a = Integer.parseInt(br.readLine());
System.out.println("b?");
b = Integer.parseInt(br.readLine());
System.out.println("c?");
c = Integer.parseInt(br.readLine());
d = (b * b) - 4 * a * c;
if (d < 0) {
System.out.println("The equation has no real solution");
} else {
solution1 = (((-1) * b) + java.lang.Math.sqrt(d)) / (2 * a);
solution2 = (((-1) * b) - java.lang.Math.sqrt(d)) / (2 * a);
System.out.println(solution1);
System.out.println(solution2);
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.