-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolynomialRunner.java
72 lines (63 loc) · 2.08 KB
/
polynomialRunner.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
71
72
import java.util.*;
public class polynomialRunner {
static int degree;
static Scanner input = new Scanner(System.in);
static LinkedList<Integer> polyList = new LinkedList<Integer>();
static LinkedList<Integer> polyList2 = new LinkedList<Integer>();
static int xValue;
static int sum;
public static void main (String[]Args)
{
System.out.println("##### ENTER-POLY METHOD #####");
System.out.println();
System.out.print("Enter the degree of the polynomial ===>> ");
degree = input.nextInt();
System.out.println();
for (int i = degree; i >=0; i--)
{
System.out.print("Enter coefficient for X^" + i + " If no term exists, enter 0 ===>> ");
polyList.addFirst(input.nextInt());
}
System.out.println();
System.out.println();
System.out.println("##### ENTER-XVALUE METHOD #####");
System.out.println();
System.out.print("Enter X value of the polynomial ====>> ");
xValue = input.nextInt();
System.out.println();
System.out.println("##### DISPLAY-POLY METHOD #####");
System.out.println();
int thisIntDoesNothing;
for (int i = polyList.size(); i>0; i--)
{
polyList2.addFirst(polyList.get(i-1));
}
//polyList2 = polyList;
System.out.println(polyList2);
System.out.print("Y = " + polyList.removeFirst());
for (int i = 1; i<= degree; i++)
{
if (polyList.getFirst()==0)
polyList.removeFirst();
else if (i == 1)
System.out.print(" + " + polyList.removeFirst() + "X");
else if(polyList.getFirst()==1)
System.out.print(" + " + "X^" + i);
else
System.out.print(" + " + polyList.removeFirst() + "X^" + i);
}
//System.out.println("cat" + polyList2);
System.out.println();
System.out.println("##### COMPUTE-POLY METHOD #####");
System.out.println();
System.out.println();
sum = polyList2.removeFirst();
for (int i = 1; i<= degree; i++)
{
sum+= (polyList2.removeFirst() * (Math.pow(xValue, i)));
}
System.out.println("##### DISPLAY-VALUE METHOD #####");
System.out.println();
System.out.println("Y(" + xValue + ") = " + sum);
}
}