|
1 | 1 | package fundamentals;
|
2 | 2 |
|
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/** |
| 6 | + * Create a program that solves quadratic equations (ax2 + bx + c = 0) using Bhaskara's formula. |
| 7 | + * Use as an example a = 1, b = 12 and c = -13. Find the delta |
| 8 | + * |
| 9 | + * Author: Jefferson Silva |
| 10 | + * Date : 07/02/2024 |
| 11 | + * Last modified: 07/02/2024 |
| 12 | + * |
| 13 | + * Modifications: |
| 14 | + * |
| 15 | + * - v1.1.0 : Created program |
| 16 | + * |
| 17 | + */ |
| 18 | + |
3 | 19 | public class Algebra {
|
| 20 | + |
| 21 | + private static int getDelta(int a, int b, int c) { |
| 22 | + return (b * b) - (4 * a * c); |
| 23 | + } |
| 24 | + |
| 25 | + private static double getX1(int a, int b, int delta) { |
| 26 | + return (-b + Math.sqrt(delta)) / (2 * a); |
| 27 | + } |
| 28 | + |
| 29 | + private static double getX2(int a, int b, int delta) { |
| 30 | + return (-b - Math.sqrt(delta)) / (2 * a); |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] args) { |
| 34 | + |
| 35 | + Scanner input = new Scanner(System.in); |
| 36 | + |
| 37 | + System.out.println("Equation: ax² + bx + c = 0 \n"); |
| 38 | + |
| 39 | + System.out.print("* Enter the value of a: "); |
| 40 | + int a = input.nextInt(); |
| 41 | + |
| 42 | + System.out.print("\n* Enter the value of b: "); |
| 43 | + int b = input.nextInt(); |
| 44 | + |
| 45 | + System.out.print("\n* Enter the value of c: "); |
| 46 | + int c = input.nextInt(); |
| 47 | + |
| 48 | + int delta = getDelta(a, b, c); |
| 49 | + |
| 50 | + System.out.printf("\nYour equation is: %dx² + %dx + %d = 0", a, b, c); |
| 51 | + |
| 52 | + System.out.println("\nThe delta is: " + delta); |
| 53 | + |
| 54 | + double x1 = getX1(a, b, delta); |
| 55 | + |
| 56 | + System.out.printf("\nThe x1 of the equation is: %.2f", x1); |
| 57 | + |
| 58 | + double x2 = getX2(a, b, delta); |
4 | 59 |
|
| 60 | + System.out.printf("\nThe x2 of the equation is: %.2f" ,x2); |
| 61 | + |
| 62 | + input.close(); |
| 63 | + |
| 64 | + } |
5 | 65 | }
|
0 commit comments