-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.java
More file actions
131 lines (126 loc) · 5.93 KB
/
code.java
File metadata and controls
131 lines (126 loc) · 5.93 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.Scanner;
import java.lang.Math;
public class Derivative {
public static void main(String[] args) {
double product;
String result;
Scanner keyboard = new Scanner(System.in);
int selection = 1;
System.out.print("Hello! Welcome to Lucas's math solver! Your options for tools are as follows\n");
while (selection != 0) {
System.out.print("\t1. Derivative Finder\n\t2. Polynomial Factorer\n\t3. Circle Solver\n\t4. SSS Triangle Angle Finder\n\t0. End Program\n");
selection = keyboard.nextInt();
if (selection == 1) {
System.out.print("Enter your coefficient of x:\n");
double coefficient = keyboard.nextDouble();
System.out.print("Enter the power of x as an integer:\n");
int prepower = keyboard.nextInt();
product = coefficient * prepower;
int power = prepower - 1;
if (prepower == 0) {
System.out.print("The answer is 0\n");
continue;
}
if (power != 0)
System.out.print("The answer is " + product + "x^" + power + "\n");
else
System.out.print("The answer is " + product + "\n");
}
if (selection == 2) {
char again;
int a, b, c, canfactor;
System.out.print("Enter the coefficient of x^2:");
a = keyboard.nextInt();
System.out.print("Enter the coefficient of x: ");
b = keyboard.nextInt();
System.out.print("Enter the constant: ");
c = keyboard.nextInt();
//0 is true
canfactor = 0;
double discriminant, temp, u, v;
discriminant = (b * b) + (-4 * a * c);
temp = (Math.sqrt(discriminant));
u = (b + temp) / (2 * a);
v = (b - temp) / (2 * a);
if (discriminant < 0)
canfactor = 1;
else {
if (temp * temp != discriminant)
canfactor = 1;
else {
if (((-b + temp) % 2) != 0 || ((-b - temp) % 2) != 0)
canfactor = 1;
}
}
if (canfactor == 0 && a == 1) {
temp = (int) (temp);
//these if statements just format the output so that the positive numbers get a + sign in front of them. The negatives already have a - sign, the they are in else statements.
if (b >= 0)
System.out.print("x^2 + " + b + "x");
else
System.out.print("x^2 " + b + "x");
if (c >= 0)
System.out.print(" + " + c + " factored is\n");
else
System.out.print(" " + c + " factored is\n");
System.out.print("(x ");
if (u >= 0)
System.out.print("+ " + u);
else
System.out.print(u);
System.out.print(") (x ");
if (v >= 0)
System.out.print("+ " + v);
else
System.out.print(v);
System.out.print(")\n\n");
//This simple code turns the values of x into their positives then prints them out.
if (u > 0)
u -= (2*u);
else if (u < 0)
u += (-2*u);
if (v > 0)
v -= (2*v);
else if (v < 0)
v += (-2*v);
System.out.print("So, x is " + u + " and " + v + ".\n");
}
else if (canfactor == 0 && a != 1) {
System.out.println("X is " + -u + " and " + -v);
}
else
System.out.println("This is not factorable");
}
if (selection == 3) {
System.out.print("This circle calculator uses pi rounded to 5 decimal places.\n");
System.out.print("Please enter your circle's radius (P.S, if you only have the diameter, just divide that by 2):\n");
double radius = keyboard.nextDouble();
double area = 3.14159 * radius * radius;
double circumference = 2 * 3.14159 * radius;
System.out.print("The area of your circle is " + area + " units squared\n");
System.out.print("And the circumference is " + circumference + " units\n");
}
if (selection == 4) {
System.out.print("Hello, please enter the sides of your triangle, click enter when done!\n");
System.out.print("Side 1: ");
double a = keyboard.nextDouble();
System.out.print("Side 2: ");
double b = keyboard.nextDouble();
System.out.print("Side 3: ");
double c = keyboard.nextDouble();
//this one finds angle a
double anglea = ((b*b) + (c*c) - (a*a))/(2 * b * c);
anglea = Math.acos(anglea);
System.out.println("The angle opposite from side 1, " + a + ", is " + Math.toDegrees(anglea) + " degrees");
//this one finds angle b
double angleb = ((c*c) + (a*a) - (b*b))/(2 * c * a);
angleb = Math.acos(angleb);
System.out.println("The angle opposite from side 2, " + b + ", is " + Math.toDegrees(angleb) + " degrees");
//this one finds angle c
double anglec = ((a*a) + (b*b) - (c*c))/(2 * a * b);
anglec = Math.acos(anglec);
System.out.println("The angle opposite from side 3, " + c + ", is " + Math.toDegrees(anglec) + " degrees");
}
}
}
}