Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Your assignment is to write a class called Polynomial. A polynomial is a fu

ID: 3849061 • Letter: J

Question

JAVA

Your assignment is to write a class called Polynomial. A polynomial is a function of the following form:

f ) = ck nk + ck-1 nk-1 + ... + c1 n + c0

c0, c1, c2, ..., ck are called coefficients. We call k the degree of the polynomial. For our purposes, we will assume that the coefficients are all integers (positive, negative, or 0).

I recommend that you store the coefficients of a Polynomial object in an instance variable which is an array of integers.

You should write the following methods for the Polynomial class

A. (1/2 point) A constructor. It is passed 1 parameter: an array of integers which represents the coefficients of the polynomial.

B. (1 point) A method called simplify. It is a void method which is passed 0 parameters. It ensures that the polynomial’s kth co-efficient is not 0. If a polynomial is created which has no non-zero coefficients, then it should be represented as an array of length 0. The simplify method should be called within the constructor.

C.(2 points) A toString method. As always, toString is passed 0 parameters and returns a String representation of an object. An example of the kind of string that toString should return can be found below.

D. (1/2 point) A degree method. It returns an integer, which is the degree of the polynomial.

E. (1 point) An evaluate method. It is passed 1 parameter x, which is an integer. It should return an integer, which represents f(x), the value of the polynomial f(n) when n is equal to x.

Explanation / Answer

package poly;
import java.io.*;
public class Polytest {
static BufferedReader br1, br2;
static Polynomial p1, p2;
public static final int ADD = 1;
public static final int MULTIPLY = 2;
public static final int EVALUATE = 3;
public static final int QUIT = 4;
public static int getChoice()
throws IOException {
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(" Enter choice # => ");
return (Integer.parseInt(br1.readLine()));
}
public static void add()
throws IOException {
System.out.print("Enter the file containing the polynomial to add => ");
br2 = new BufferedReader(new FileReader(br1.readLine()));
p2 = new Polynomial(br2);
System.out.println(" " + p2 + " ");
System.out.println("Sum: " + p1.add(p2) + " ");
}
public static void multiply()
throws IOException {
System.out.print("Enter the file containing the polynomial to multiply => ");
br2 = new BufferedReader(new FileReader(br1.readLine()));
p2 = new Polynomial(br2);
System.out.println(" " + p2 + " ");
System.out.println("Product: " + p1.multiply(p2) + " ");
}
public static void evaluate()
throws IOException {
System.out.print("Enter the evaluation point x => ");
float x = Float.parseFloat(br1.readLine());
System.out.println("Value at " + x + ": " + p1.evaluate(x) + " ");
}
public static void main(String[] args) throws IOException {
br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the polynomial file => ");
br2 = new BufferedReader(new FileReader(br1.readLine()));
p1 = new Polynomial(br2);
System.out.println(" " + p1 + " ");
int choice = getChoice();
while (choice != QUIT) {
if (choice < 1 || choice > QUIT) {
System.out.println(" Incorrect choice " + choice);
} else {
switch (choice) {
case ADD: add(); break;
case MULTIPLY: multiply(); break;
case EVALUATE: evaluate(); break;
default: break;
}
}
choice = getChoice();
}
}
}