Write the following Java methods • boolean real-sols(double a, double b, double
ID: 3687430 • Letter: W
Question
Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, double b, double c): If the equation ax2 + bx + c has real solutions it return the smallest of the two. If it doesn’t have real solutions it should return the value 0 and an error message. • double solution2(double a, double b, double c): If the equation ax2 + bx + c has real solutions it return the biggest of the two. If it doesn’t have real solutions it should return the value 0 and an error message. • Test your methods within a class and check that they work properly. Use the method real-sols(double a, double b, double c) in the implementation of the other 2 methods.
Explanation / Answer
Quad.java
import java.util.Scanner;
public class Quad {
public static void main(String[] args) {
System.out.println ("Please enter a value for a");
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
System.out.println(a);
System.out.println ("Please enter a value for b");
double b = sc.nextDouble();
System.out.println(b);
System.out.println ("Please enter a value for c");
double c = sc.nextDouble();
System.out.println(c);
double e = solution1(a,b,c);
System.out.println ("smallest value = "+e);
double f= solution2(a,b,c);
System.out.println ("biggest value = "+f);
}
public static double solution1(double a, double b, double c)
{
boolean d = real_sols(a,b,c);
System.out.println (d);
double x = 0;
x = (-b + (Math.sqrt((b*b - ((4 * a * c))))))/ (2 * a);
double y = 0;
y = (-b - (Math.sqrt((b*b - ((4*a*c))))))/ (2 * a);
if (((b*b - ((4 * a * c))) < 0) == true) {
System.out.println ("There are no real roots");
}
if (((b*b - ((4 * a * c))) < 0) == false)
{}if(x>y){
return y; }
else
{ return x;}
}
public static double solution2(double a, double b, double c)
{
double x = 0;
x = (-b + (Math.sqrt((b*b - ((4 * a * c))))))/ (2 * a);
double y = 0;
y = (-b - (Math.sqrt((b*b - ((4*a*c))))))/ (2 * a);
boolean d = real_sols(a,b,c);
System.out.println (d);
if (((b*b - ((4 * a * c))) < 0) == true) {
System.out.println ("There are no real roots");
}
if (((b*b - ((4 * a * c))) < 0) == false)
{}
if(x>y){
return x;
}
else
{return y;}
}
public static boolean real_sols(double a, double b, double c)
{
if (((b*b - ((4 * a * c))) < 0) == true)
{
return false;
}
return true;
}
}
Output :
Please enter a value for a
2
2.0
Please enter a value for b
8
8.0
Please enter a value for c
2
2.0
true
smallest value = -3.732050807568877
true
biggest value = -0.2679491924311228
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.