-
Notifications
You must be signed in to change notification settings - Fork 1
/
Polynomial.java
70 lines (58 loc) · 1.99 KB
/
Polynomial.java
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
import java.util.ArrayList;
import java.util.Scanner;
public class Polynomial {
private ArrayList<Values> poly;
public Polynomial(ArrayList<Values> poly) {
this.poly = poly;
}
public double evaulate(double x) {
double answer = 0;
for(Values v : poly) {
answer += v.getCoefficient()*Math.pow(x,v.getExponent());
}
return answer;
}
public Polynomial derivative() {
ArrayList<Values> newPoly = new ArrayList<Values>();
ArrayList<Values> currPoly = getValues();
for(Values v : currPoly) {
Values newV = new Values(v.getCoefficient()*v.getExponent(),
v.getExponent()-1);
newPoly.add(newV);
}
return new Polynomial(newPoly);
}
public ArrayList<Values> getValues() {
return poly;
}
/**
public static void main(String[] args) throws Exception {
ArrayList<Values> poly = new ArrayList<Values>();
System.out.println("Enter polynomials in the following format:");
System.out.println("Coefficient = <your input>");
System.out.println("Exponent = <your input>");
System.out.println("Done (y/n) = <y/n>");
boolean done = false;
Scanner scan = new Scanner(System.in);
while(!done) {
System.out.print("Coefficient = ");
double a = Double.parseDouble(scan.nextLine());
System.out.print("Exponent = ");
int n = Integer.parseInt(scan.nextLine());
System.out.print("Done (y/n) = ");
String s = scan.nextLine().trim().toLowerCase();
if(s.equals("y")) {
done = true;
}
poly.add(new Values(a,n));
}
for(int i = 0; i < poly.size(); i++) {
Values v = poly.get(i);
double a = v.getCoefficient();
int n = v.getExponent();
System.out.print(a + "x^" + n + " ");
}
System.out.println();
}
*/
}