Question

In: Computer Science

Solve Quadratic Equation IN JAVA (static methods, parameters, arguments, Math functions) Write a program that will...

Solve Quadratic Equation IN JAVA (static methods, parameters, arguments, Math functions)

Write a program that will print the real-valued solutions of a quadratic equation

ax^2+bx+c=0

For example:

Enter coefficient a:  1
Enter coefficient b:  10
Enter coefficient c:  2
The solutions are -0.2041684766872809, and -9.79583152331272

For another example:

Enter coefficient a:  1
Enter coefficient b:  2
Enter coefficient c:  3
The equation does not have real solution

The program should use the following methods.

  1. getCoeff(), which takes a String message as a parameter. It will print the message and returns the user's input (a double value)
  2. hasRealSolution(), which takes the three double parameters, a, b and c, and returns true if

    b^2−4ac>=0

  3. solution1() and solution2(), both take the three coefficients, a, b, and c, and returns the two solutions according to

    x = −b ± (√(b^2 − 4ac)) / (2a)

  4. main(), which prompts the user for the three coefficients, and call the other methods to find the solutions.

Notice that other than getCoeff() and the main() methods, methods should not interact with the user.

Solutions

Expert Solution

import java.util.Scanner;

public class Main {

public static double getCoeff(String msg) {

Scanner sc = new Scanner(System.in);

System.out.println(msg);

return sc.nextDouble();

}

public static boolean hasRealSolution(double a, double b, double c) {

return (b * b - 4 * a * c) >= 0;

}

public static double solution1(double a, double b, double c) {

return ((-1 * b) + (Math.sqrt(b * b - 4 * a * c))) / (2 *a);

}

public static double solution2(double a, double b, double c) {

return ((-1 * b) - (Math.sqrt(b * b - 4 * a * c))) / (2 * a);

}

public static void main(String[] args) {

double a = getCoeff("Enter coefficient a: ");

double b = getCoeff("Enter coefficient b: ");

double c = getCoeff("Enter coefficient c: ");

if (hasRealSolution(a, b, c)) {

System.out.println("The solutions are " + solution1(a, b, c) + ", and " + solution2(a, b, c));

} else {

System.out.println("The equation does not have real solution");

}

}

}


Related Solutions

Write a program to input the coefficients of a quadratic equation and solve roots for all...
Write a program to input the coefficients of a quadratic equation and solve roots for all cases (including complex roots). VBA. x=(b ^2) - (4ac) I have it for 2 complex and 2 real and repeated. Is that all cases?
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
write the program named Lab06.java that contains the following four static methods: • public static double...
write the program named Lab06.java that contains the following four static methods: • public static double max(double[] data) that returns the maximum value in the array data • public static double min(double[] data) that returns the minimum value in the array data • public static double sum(double[] data) that sums all items in the array and return the result • public static double ave(double[] data) that call the sum() method and then return the average. Once you have completed the...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
This program will focus on utilizing functions, parameters, Python's math and random modules, and if-statements! To...
This program will focus on utilizing functions, parameters, Python's math and random modules, and if-statements! To Start: You should fork the provided REPL, run the program and observe the output, and then take a look through the code. You may not 100% understand the code, but taking a quick look through what's there will be helpful in the future. You are going to systematically replace the TODO comments with code so that the program will work as intended. TODO 0...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called "DoTheMath" that accepts as input three floating-point numbers x, y, and z (define them as double) and outputs several calculations: x to the power of y x to the power of (y to the power of z) The absolute value of x The square root of (x*y to the power of z). Sample Run: Enter the values for x, y, z: -3.7 -3 5...
(Java) Create a program using 3 methods. The methods must be public and static. Ask the...
(Java) Create a program using 3 methods. The methods must be public and static. Ask the user for 3 numbers, then call the methods from main to print max, min, and average. The first method max (int x, int y, int z) returns the maximum value of 3 integer values. The second method min (int X, int y, int z) returns the minimum value of 3 integer values. And the third average (int x, int y, int z) returns the...
Write a C++ program that solves a quadratic equation to find its roots. The roots of...
Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic equation ax2 + bx + c = 0 (where a is not zero) are given by the formula (–b ± sqrt(b2 – 4ac)) / 2a *Use #include for the use of sqrt. The value of the discriminant (b2 – 4ac) determines the nature of roots. If the value of the discriminant is zero, then the equation has a single real root. If...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula ( you must do it both ways). Show all steps for each method and put your answer in simplest radical form possible. 2) Which part of the Quadratic Formula can help you to find the Nature of the roots for the Quadratic Equation. Explain how you can find the nature of the roots and provide one Example for each possible case with solution.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT