Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am only supposed to code within Polynomial.java and not allowed to modify any headers. Just allowed to code inside add, multiply, addTerm, and evaluate.
I am only supposed to code within Polynomial.java and not allowed to modify any headers. Just allowed to code inside add, multiply, addTerm, and evaluate.
My code outputs polynomials with zero-coefficients.
like: 0.0 + 0.0x + 0.0x^3
How can I make it only output 0?
File Edit Selection View GO Run Terminal Help Polynomial.java - poly - Visual Studio Code EXPLORER L Otest.txt Term.java Node.java 1 V OPEN EDITORS 1 UNSAVED Polynomial.java Eptest2.txt short.txt ptest.txt Polytest.java Term.java Polynomialjava ptest.txt short.txt prest Polytest.java Polynomial.java 1 package poly; 2 3 import java.io.TOException; 4 import java.util.Scanner; 5 6 7 * This class implements evaluate, add and multiply for polynomials. > Otest.txt Node.java * uuthor * */ A public class Polynomial { V POLY Otest.txt Node.java Polynomial.java Polytestjava Eptest 1.1 prestopp.txt ptost2.txt short.txt Term.java 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 * Reads a polynomial from an input stream (file or keyboard). The storage format * of the polynomial is: *
* with the guarantee that degrees will be in descending order. For example: * 45 -23 21 32 33 ** which represents the polynomial: *** 4*x^5 - 2*x^3 + 2*x + 3 *34 35 30 03 > OUTLINE 37 > JAVA PROJECTS 29 A1 . * @param sc Scanner from which a polynomial is to be read Rthrows Torycention If there is an innut error in reading the polmomial Ln 130, Col 26 Tab Sice: 4 UTF-8 CRLF Java 0 0 11:49 AO DO API ODA 2021-03-05 O E 16 Otest.txt L Node.java * 43 44 File Edit Selection View GO Run Run Terminal Help Polynomial.java - poly - Visual Studio Code Polynomialjava EXPLORER . pest2.txt short.txt prest 1.1 Polytest.java Term.java V OPEN EDITORS 1 UNSAVED Polynomialjava Polynomial.java 1 37 * @param sc Scanner from which a polynomial is to be read Eptest2.txt 38 ** @throws 10Exception If there is any input error in reading the polynomial 30 short.txt * @return The polynomial linked list (front node) constructed from coefficients and ptest.txt degrees read from scanner 41 Polytest.java 42 public static Node read(scanner sc) > Term.java throws IOException { Otest.txt Node poly - null; Node.java 45 while (sc.hasNextLine()) { 16 Scanner soLine - now Scanner(sc.nextLine()); V POLY poly = new Node(schine.nextFloat(), scline.nextInt(), poly); Otest.txt A SCLine.close(); Node.java 49 } Polynomial.java 50 return poly; Polytestjava 51 Eptest 1.1 52 prestopp.txt 53 54 ptost2.txt * Returns the sum of two polynomials - DOES NOT change either of the input polynomials. 55 * The returned polynomial MUST have all new nodes. In other words, none of the nodes short.txt 56 * of the input polynomials can be in the result. Term.java 57 58 * @param polyi First input polynomial (front of polynomial linked list) 59 @param poly? Second input polynomial (front of polynomial linked list 60 * @return A new polynomial which is the sun of the input polynomials - the returned node 61 is the front of the result polynomial 62 * 63 public static Node add(Node polyi, Node poly2) { 64 Node result = null; 65 Node pl = polyi; 66 while (pi !- null) { 6/ if(pi.term.coeff |- ) { 68 result - addTerm(result, pl. term.coeff, pi.tern.degree); 69 pl - pl.next; 70 ) 71 else{ 72 03 > OUTLINE pl = pi.next; 73 ] > JAVA PROJECTS A1 . O E SP Ln 130, Col 26 Tab Sice: 4 UTF-8 CRLF lava 0 0 AO DO API 11:49 ODA FI 2021-03-05 L Node.java File Edit Selection View GO Run Terminal Help Polynomial.java - poly - Visual Studio Code EXPLORER Polynomialjava ptest.txt short.txt prest Polytest.java Otest.txt Term.java V OPEN EDITORS 1 UNSAVED Polynomialjava Polynomial.java 1 73 Eptest2.txt 74 } short.txt 75 Node p2 - poly2; 76 while (p2 |= null) { ptest.txt 77 if(p2.term.coeff != ){ Polytest.java 78 result = addTerm(result, p2.tern.coeff, p2. term.degree); > Term.java 79 p2 = p2.next; Otest.txt 88 1 Node.java 81 else{ 82 V POLY p2 - p2.next; 83 1 Otest.txt 84 } A Node.java 85 return result; Polynomial.java 86 } Polytestjava 87 Eptest 1.1 88 prestopp.txt 89 * Returns the product of two polynomials - DOES NOT change either of the input polynomials. 90 ptost2.txt * The returned polynomial MUST have all new nodes. In other words, none of the nodes 91 * of the input polynomials can be in the result. short.txt 92 Term.java 93 * @param polyi First input polynomial (front of polynomial linked list) 94 @param poly2 Second input polynomial (front of polynomial linked list) 95 @return A new polynomial which is the product of the input polynomials the returned node 96 36 is the front of the result polynomial 97 * 98 public static Node multiply(Node polyi, Node poly2) { 99 Node result - null; 180 Node start = polyn; 181 while(start.next != null) { 182 Node start2 - poly2; 103 while(start2 ! - null) { 184 if(start.term.coeff start2.term.coeff !- 0){ 105 result - addTerm(result, start.term.coeff * start2. term.coeff, 186 start.term.degree + start2.term.degree); 187 startz - startz.next; 108 } 03 > OUTLINE 189 clsel > JAVA PROJECTS Ln 130 A1 . o Et n SP OneDrive LICE . OneDrive ID AIDS VIIF LI AOODAP! 11:40 2021-03-05 Node.java File Edit Selection View GO Run Run Terminal Help - Polynomial.java - poly - Visual Studio Code EXPLORER L Polynomialjava Eptest2.txt short.txt prest 1.1 Polytest.java Term.java V OPEN EDITORS 1 UNSAVED Polynomialjava Polynomial.java 1 98 public static Node multiply(Node poly1, Mode poly?) { 99 Eptest2.txt Node result = null; 180 Node start - polyi; short.txt 101 while(start.next !- null) { ptest.txt 182 Node start2 - poly2; Polytest.java 103 while(start2 !- null) { > Term.java 184 if(start.term.coeff * start2.term.coeff 1-0) Otest.txt 185 result = addTerm(result, start.term.coeff * start?.term.coeff, 186 Node.java start.term.degree + start2. term.degree); 187 start2 = start2.next; V POLY 188 } Otest.txt 109 else{ A Node.java 110 startz - startz.next; Polynomial.java 111 } Polytestjava 112 } Eptest 1.1 113 start - start.next; 114 prestopp.txt } 115 return result; ptost2.txt 116 ) short.txt 117 private static Node addTerm( Node poly, float coeff, int degree) { Term.java 118 if(coeff == 0) { 119 return poly; 120 } 121 Node n = new Node (coeff, degree, null); 122 if (poly == null) { 123 return n; 124 } else { 125 if (poly. term.degree -- degree) { 126 poly.term.coeff - coeff; 127 return poly; 128 else if (poly.term.degree OUTLINE 134 while (p.next !- null) { > JAVA PROJECTS it euren lommerer A1 2 . O SP 133 Ln 130, Col 26 Tab Sice: 4 UTF-8 CRLF Java O &Q AO DO API 11:50 ODA FI 2021-03-05 X Term.java Node.java 137 File Edit Selection View GO Run Run Terminal Help Polynomial.java - poly - Visual Studio Code EXPLORER L Polynomialjava pest2.txt short.txt presti.ba Polytest.java V OPEN EDITORS 1 UNSAVED Polynomialjava puny Polynomial.java 1 131 return poly: Eptest2.txt 132 ) short.txt 133 Node p = poly; ptest.txt 134 while (p.next != null) { Polytest.java 135 if (degree -- p.term.degree) { 136 > Term.java p.term.coeff += coeff; return poly: Otest.txt 138 } else if (degree > p.next. term.degree) { Node.java 139 n. nex- p.nexl; V POLY 140 p.next = n; Otest.txt 141 return poly; A Node.java 142 } } Polynomial.java 143 P - p.next; 144 ] ] Polytestjava 145 if(p.term.degree == degree) { Eptest 1.1 146 p.term.coeff += coeff; prestopp.txt 147 return poly; ptost2.txt 148 } short.txt 149 p.next = n; Term.java 150 return poly: 151 } 152 ) 153 7:33 154 * Evaluates a polynomial at a given value. 155 156 * @param poly Polynomial (front of linked list) to be evaluated 157 @param x value at which evaluation is to be done * @return value of polynomial p at x 159 160 public static float evaluate(Node poly, float x) { 161 float result - B; 162 while(poly !- null) { 163 result += poly.tern.coerr * Math.pow(x, poly.term.degree), 164 poly - poly.next; 165 } 03 > OUTLINE 166 return result; > JAVA PROJECTS 167 } A1 . O Et n SP 158 OneDrive LICE . OneDrive Ln 130 ID AIDS VIIF LI AOODAP! 11:50 2021-03-05 L Term.java Node.java File Edit Selection View GO Run Run Terminal Help Polynomial.java - poly - Visual Studio Code EXPLORER Polynomial.java pest2.txt short.txt Polytest.java V OPEN EDITORS 1 UNSAVED Polynomialjava Polynomial.java 1 153 154 * Evaluates a polynomial at a given value. Eptest2.txt 155 short.txt 156 * @param poly Polynomial (front of linked list) to be evaluated ptest.txt 157 * @param x value at which evaluation is to be done Polytest.java 158 * @return value of polynomial p at x > Term.java 159 Otest.txt 160 public static float evaluate(Node poly, float x) { Node.java 161 flual resull - 0; 162 while(poly != null) { V POLY 163 result +- poly.term.coeft * Math.pow(x, poly.term.degree); Otest.txt 164 poly = poly.next; A Node.java 165 } Polynomial.java 166 return result; Polytestjava 167 } Eptest 1.1 168 169 prestopp.txt 170 * Returns string representation of a polynomial ptost2.txt 171 short.txt 172 @param poly polynomial (front of linked list) Term.java 173 ** @return String representation, in descending order of degrees 174 1/5 public static string lostring(Node poly) { 176 if (poly == null) 177 return "e"; 178 } 179 180 String retval - poly. term.tostring(); 181 for (Node current - poly.next ; current !- null ; 182 current - current.next) { 183 retval - current.tern.toString() + " + " + retval; 184 } 185 relurti relval; 186 187 188 03 > OUTLINE > JAVA PROJECTS A1 . O SP OneDrive LICE . OneDrive Ln 130 ID AIDS VIIF LI AOODAP! 11:50 2021-03-05 File Edit Selection View Go Run Terminal Help Polytest.java - poly - Visual Studio Code X EXPLORER L. short.txt prest I.txt Polytest.java X 6 Term.java Otest.txt Node.java V OPEN EDITORS 1 UNSAVED Polynomial.java Eptest2.txt short.txt Eptest.txt x Polytestjava Term.java Polynomial.java ptest2.txt Polytestjava > {) poly 1 1 package poly; 2 ! 3 3 import java.io.File; import java.io.IOException; 5 import java.util.Scanner; 4 > 7 8 Otest.txt public class Polytest { static Scanner sci, sc2; slalic Nude polyi, poly2; 9 public static final int ADD - 1; public static final int MULTIPLY = 2; public static final int EVALUATE = 3; public static final int QUIT = 4; 1 Node.java V POLY Otest.txt Node.java Polynomial.java Polytest.java Eptest.txt prestopp.txt ptost2.txt short.tt Term.java 1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public static int getchoice() throws IOException { System.out.println(); System.out.println(ADD + ". ADD polynomial"); System.out.println(MULTIPLY + ". MULTIPLY polynomial"); System.oul.println(EVALUATE + ". EVALUATE polynomial"); System.out.println(QUTT + ". QUIT"); System.out.print("\tenter choice # => "); return (Integer.parseInt(sc1.nextLine())); } public static void add() throws IOException { System.out.print("Enter the file containing the polynomial to add -> "); sc2 - new Scanner(new File(sci.nextLine())); poly2 - Polynonial.read(sc2); System.out.println(" " + Polynomial.tostring(poly2) + " "); System.oul.println("Sum: Polynomial.toString(Polynomial.add(poly1,poly2)) + " "); } 32 33 34 OneDrive LICE . OneDrive 35 30 > OUTLINE 37 > JAVA PROJECTS 28 0A2 . public static void multiply) throws Torxcention ! O + 16 AOODAP! 11:50 2021-03-05 File Edit Selection View Go Run Terminal Help Polytest.java - poly - Visual Studio Code X EXPLORER L. Otest.txt Node.java 1 EUR V OPEN EDITORS 1 UNSAVED Polynomial.java Eptest2.txt short.txt Eptest.txt x Polytest.java Term.java Polynomialjava Eptest2.txt short.txt prest 1.1 O Polytest.java X Term.java Polytest.java > {) poly 37 public static void multiply 38 throws IOException { 30 System.out.print("Enter the file containing the polynomial to multiply => "); SC2 - new Scanner(new File(sc1.nextLine()); 41 poly2 = Polynomial.read(sc2); 42 System.out.println(" " + Polynomial.tostring(poly2) + " "); System.out.println("Product: " + Polynomial.toString(Polynomial.multiply(poly1, poly2)) + " "); } > Otest.txt 44 47 49 1 Node.java V POLY Otest.txt Node.java Polynomial.java Polytest.java Eptest.txt prestopp.txt ptost2.txt short.txt Term.java public static void evaluate() throws TOException { System.out.print("Enter the evaluation point x => "); float x = Float.parseFloat(sc1.nextLine()); System.out.println("value at "X": "Polynomial.evaluate(polyi,x), " "); } 49 50 51 52 53 1 1 Run Debug public static void main(String[] args) throws IOException { scl - new Scanner(System.in); System.out.print("Enter the name of the polynomial tile => "); SC2 - new Scanner(new File(sci.nextLine()); poly1 = Polynomial.read(sc2); System.out.println(" " + Polynomial.tostring(polyt) + " "); 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 int choice = getchoice(); while (choice != QUTT) ( if (choice QUIT) { System.out.println("\tIncorrect choice " + choice); } else { switch (choice) { case ADD: add(); break; case MULTIPLY: multiply(); break; case EVALUATE: evaluate(); break; default: break; } 70 71 72 OneDrive LICE. OneDrive > OUTLINE > JAVA PROJECTS 0A2 . In 1, Col1 Tab Sicer 11:50 ODCAP! 2021-03-05 O E A 16 File Edit Selection View Go Run Terminal Help Polytest.java - poly - Visual Studio Code X L. EXPLORER 6 Term.java Node.java 1 EUR V OPEN EDITORS 1 UNSAVED Polynomial.java Eptest2.txt short.txt Eptest.txt x Polytest.java Term.java > Otest.txt Polynomial.java pest2.txt short.txt prest I.txt Polytest.java X Polytest.java > {} poly 53 Run Debug 54 public static void main(String[] args) throws IOException { 55 sc1 = new Scanner(System.in); 56 System.out.print("Enter the name of the polynomial file -> "); 57 Sc2 - new Scanner(new File(sci.nextLine()); 58 59 poly1 = Polynomial.read(sc2); 60 System.out.println(" " Polynomial.tostring(poly1) " "); 61 62 int choice - getchoice(); while (choice 1= QUTT) { 64 if (choice QUIT) { 65 System.out.println("\tIncorrect choice - + choice); 66 } else { 67 switch (choice) { 68 case ADD: add(); break; 69 Case MULTIPLY: multiply(); break; 78 case EVALUATE: evaluate(); break; 71 default: break; 72 } 73 } 74 choice - getchoice(); 75 76 77 ) 78 } 79 Node.java V POLY Otest.txt Node.java Polynomial.java Polytest.java Eptest.txt prestopp.txt ptost2.txt short.tt Term.java 1 1 OneDrive LICE . OneDrive > OUTLINE > JAVA PROJECTS 0A2 . O SP AOODAP! 11:50 2021-03-05 File Edit Selection View GO Run Run Terminal Help Term.java - poly - Visual Studio Code X EXPLORER L. Polytest.java Term.java X Otest.txt Node.java 1 V OPEN EDITORS 1 UNSAVED Polynomial.java Eptest2.txt short.txt prestitut Polytest.java x Term.java Polynomialjava Eptest2.txt short.it prest 1.1 Term.java > 95 Term > equals(Object) 1 package poly; 2 3 4 * this class implements a term of a polynomial. 5 * Rauthor runb-c8112 7 1 1 Otest.txt 8 Node.java 9 public class Term { V POLY * coefficient of tern. */ public float coerr; 1 Otest.txt Node.java Polynomial.java Polytestjava prest 1.txt presttapp.txt ptost2.txt Eshort.txt Term.java /** * Degree of term. */ public int degree; 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 * Initializes an instance with given coefficient and degree. * @param coeff coefficient * @param degree Degree */ public Term(float coeff, int degree) { this.coeff - coeff; this.degree - degree; } 32 /* (non-Javadoc) * @see java.lang.objecl#equals( java.lang,object) 33 34 35 30 37 OneDrive LICE . OneDrive public boolean equals(object other) return other = null && other instanceof Term & coeff -- (Termother).coeff &R > OUTLINE > JAVA PROJECTS 0A3 . O EL SP ADAPI 11:50 2021-03-05 X L. Otest.txt Term.java X Node.java 1 V POLY File Edit Selection View GO Run Run Terminal Help Term.java - poly - Visual Studio Code EXPLORER Polynomial.java Eptest2.txt short.txt prest 1.1 Polytest.java V OPEN EDITORS 1 UNSAVED Term.java > As Term > equals(object) 27 Polynomial.java public Term(float coeff, int degree) { 28 this.coeff - coeff; Eptest2.txt 29 this.degree = degree; short.txt 38 } Eptest.txt 31 Polytest.java 1 32 /* (non-Javadoc) x Term.java 1 33 * Osee java.lang.objectilequals(java.lang.object) 34 Otest.txt 35 Node.java public boolean equals(object other) | 36 return other !- null && 37 olher inslariceof Term && Otest.txt 38 coeff -- ((Termother).coeff & Node.java 39 degree == ((Term)other). degree; Polynomial.java 1 48 41 42 prest 1.txt /* (non-Javadoc) 43 presttapp.txt * @see java.lang.Object#tostring() 44 ptost2.txt 45 public String tostring() { Eshort.txt 46 if (degree -- 0) { 1 47 return coeff ": } else if (degree == 1) ( reluri coeft + "x"; 50 } else i 51 returni cxift + "X" + degree; 52 } 53 } 54 55 Polytestjava Term.java 40 OneDrive LICE . OneDrive > OUTLINE > JAVA PROJECTS 0A3 . O SP ADAPI 11:50 2021-03-05 X L. 6 Term.java Node.java X > A File Edit Selection View GO Run Run Terminal Help Node.java - poly - Visual Studio Code EXPLORER Polynomialjava pest2.txt short.txt prestitut Polytest.java V OPEN EDITORS 1 UNSAVED Node.java > Polynomial.java 1 1 package poly; 2 Eptest2.txt 3 short.txt 4 * This class implements a linked list node that contains a Term instance. ptest.txt 5 Polytest.java 1 * Gauthor runb-c5112 Term.java 1 7 Otest.txt 8 *) X Nocle.java 1 9 public class Node ( 10 V POLY V 11 Otest.txt 12 * Term instance. Node.java 1 13 Polynomial.java 1 14 Term term; Polytestjava 1 1 15 Eptest1.xt 16 17 prestopp.txt * Next node in linked list. 18 */ ptost2.txt 19 Node next; Eshort.bt 28 Term.java 21 22 * Initializes this node with a term with given coefficient and degree, 23 * pointing to the giver next node. 24 25 * @param coeff coefficient of term 26 * @param deeree Degree of term 27 * @param next Next node 28 */ 29 public Node(float coeff, int degree, Node next) { 30 term - new term(coeft, degree); 31 this.next - next; 32 } 33 34 35 OneDrive LICE OneDrive > OUTLINE > JAVA PROJECTS 0A4 . O SP ADAPI 11:50 2021-03-05
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started