-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_25.java
More file actions
56 lines (45 loc) · 1.97 KB
/
Copy pathExercise07_25.java
File metadata and controls
56 lines (45 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
/****************************************************************************
* This test program takes in A B and C and passes it to the method
* solveQuadratic. It also passes an empty array of roots. The method returns
* the number of roots and edits the root array to include the two roots (if
* there are no roots NaN is used)
****************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_25 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter A B and C sperated by spaces: ");
double[] eqn = {input.nextDouble(), input.nextDouble(), input.nextDouble()};
double[] roots = new double[2];
int nOfRoots = solveQuadratic(eqn, roots);
//determine how to display the information based on the number of roots
switch(nOfRoots){
case 2: System.out.print("There are two roots: " + roots[0] + " and " + roots[1]);
break;
case 1: if(Double.isNaN(roots[0]))
System.out.print("There is one root: " + roots[1]);
else
System.out.println("There is one root: " + roots[0]);
break;
case 0:
System.out.println("There are no roots");
}
}
public static int solveQuadratic(double[] eqn, double[] roots){
//positive root
roots[0] = (-eqn[1] + Math.sqrt(Math.pow(eqn[1], 2) - (4 * eqn[0] * eqn[2]))) / (2 * eqn[0]);
//negitive root
roots[1] = (-eqn[1] - Math.sqrt(Math.pow(eqn[1], 2) - (4 * eqn[0] * eqn[2]))) / (2 * eqn[0]);
//determine which value is a number
int nOfRoots = 2;
if(Double.isNaN(roots[0]))
nOfRoots--;
if(Double.isNaN(roots[1]))
nOfRoots--;
return nOfRoots;
}
}