- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolytest.java
More file actions
Latest commit
78 lines (67 loc) · 2.38 KB
/
Copy pathPolytest.java
File metadata and controls
78 lines (67 loc) · 2.38 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
packagepoly;
importjava.io.File;
importjava.io.IOException;
importjava.util.Scanner;
publicclassPolytest {
staticScannersc1, sc2;
staticNodepoly1, poly2;
publicstaticfinalintADD = 1;
publicstaticfinalintMULTIPLY = 2;
publicstaticfinalintEVALUATE = 3;
publicstaticfinalintQUIT = 4;
publicstaticintgetChoice()
throwsIOException {
System.out.println();
System.out.println(ADD + ". ADD polynomial");
System.out.println(MULTIPLY + ". MULTIPLY polynomial");
System.out.println(EVALUATE + ". EVALUATE polynomial");
System.out.println(QUIT + ". QUIT");
System.out.print("\tEnter choice # => ");
return (Integer.parseInt(sc1.nextLine()));
}
publicstaticvoidadd()
throwsIOException {
System.out.print("Enter the file containing the polynomial to add => ");
sc2 = newScanner(newFile(sc1.nextLine()));
poly2 = Polynomial.read(sc2);
System.out.println("\n" + Polynomial.toString(poly2) + "\n");
System.out.println("Sum: " +
Polynomial.toString(Polynomial.add(poly1,poly2)) + "\n");
}
publicstaticvoidmultiply()
throwsIOException {
System.out.print("Enter the file containing the polynomial to multiply => ");
sc2 = newScanner(newFile(sc1.nextLine()));
poly2 = Polynomial.read(sc2);
System.out.println("\n" + Polynomial.toString(poly2) + "\n");
System.out.println("Product: " +
Polynomial.toString(Polynomial.multiply(poly1,poly2)) + "\n");
}
publicstaticvoidevaluate()
throwsIOException {
System.out.print("Enter the evaluation point x => ");
floatx = Float.parseFloat(sc1.nextLine());
System.out.println("Value at " + x + ": " + Polynomial.evaluate(poly1,x) + "\n");
}
publicstaticvoidmain(String[] args) throwsIOException {
sc1 = newScanner(System.in);
System.out.print("Enter the name of the polynomial file => ");
sc2 = newScanner(newFile(sc1.nextLine()));
poly1 = Polynomial.read(sc2);
System.out.println("\n" + Polynomial.toString(poly1) + "\n");
intchoice = getChoice();
while (choice != QUIT) {
if (choice < 1 || choice > QUIT) {
System.out.println("\tIncorrect choice " + choice);
} else {
switch (choice) {
caseADD: add(); break;
caseMULTIPLY: multiply(); break;
caseEVALUATE: evaluate(); break;
default: break;
}
}
choice = getChoice();
}
}
}