You will implement and test a preliminary version of the polynomial class , usin
ID: 670326 • Letter: Y
Question
You will implement and test a preliminary version of the polynomial class, using a small array to store the coefficients of the polynomial in order of increasing exponents, following the design pattern of object-oriented approach. Suppose the range of the exponents is in [0, 30).
Use an array as a private member variable.
insert(), adding a new term on specific exponent.
remove(), deleting a term.
Have a add() method, deciding the parameters and the return value by yourself.
Have a sub() method, deciding the parameters and the return value by yourself.
A printout() method to display the polynomial expression.
And other variables, or methods if needed.
Do the algorithm analysis on the implemented method, and give the big O notation estimation.
You will also need to write a PolynomialTest program to initialize two linear polynomial objects and apply the operation on it and also display the result. For example, P1=x + 3x5 - 5x8; P2= 2x3 - 4x5+ 2x7; So P3= P1+P2= x +2x3 - x5+ 2x7 - 5x8, P4 = P1-P2 = x - 2x3 + 7x5- 2x7 - 5x8.
COSC 3331: Data Structures and Algorithms class in Java
Help! Its a project
It has to run and work on a java based. Show full code
Explanation / Answer
import java.util.Scanner;
public class PolyNomialTest {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Enter polynomial 1");
Polynomial pn1 = PolyNomialTest.readPn(sc);
System.out.println("Enter polynomial 2");
Polynomial pn2 = PolyNomialTest.readPn(sc);
pn1.print();
pn2.print();
Polynomial pn3 = pn1.add(pn2);
System.out.println(" Addition ");
pn3.print();
Polynomial pn4 = pn1.subtract(pn2);
System.out.println(" Subtraction ");
pn4.print();
}
public static Polynomial readPn(Scanner sc) {
Polynomial pn = new Polynomial();
int[] expCoeff = new int[Polynomial.MAX_EXPONENT];
System.out.println("Enter -1 for exponent power to quit Enter polynomial terms: ");
while(true) {
System.out.println("Enter exponent power : ");
int exp = sc.nextInt();
if(exp < 0)
break;
System.out.println("Enter Coefficient of exponent : ");
int coeff = sc.nextInt();
pn.insert(exp, coeff);
}
return pn;
}
}
class Polynomial {
private int[] expCoeff; //in increasing order o = x^0, 1 = x^1 etc
public static int MAX_EXPONENT = 30;
public Polynomial() {
expCoeff = new int[MAX_EXPONENT];
for(int i = 0; i <MAX_EXPONENT; i++ )
expCoeff[i] = 0;
}
public void insert(int term, int coeff) {
if(term < MAX_EXPONENT)
expCoeff[term] = coeff;
}
public void remove(int term) {
expCoeff[term] = 0;
}
public Polynomial add(Polynomial p2) {
Polynomial p3 = new Polynomial();
int[] coeff = new int[MAX_EXPONENT];
for(int i = 0; i <MAX_EXPONENT; i++ ) {
coeff[i]= this.getExpCoeff(i)+ p2.getExpCoeff(i);
}
p3.setExpCoeff(coeff);
return p3;
}
public Polynomial subtract(Polynomial p2) {
Polynomial p3 = new Polynomial();
int[] coeff = new int[MAX_EXPONENT];
for(int i = 0; i <MAX_EXPONENT; i++ ) {
coeff[i]= this.getExpCoeff(i) - p2.getExpCoeff(i);
}
p3.setExpCoeff(coeff);
return p3;
}
public void print() {
String pn = "";
for(int i = MAX_EXPONENT-1; i >=0; i-- ) {
pn += pn.equals("") ? "" : "+";
if(expCoeff[i] != 0) {
pn += expCoeff[i];
if(i != 0)
pn += " * x^"+i;
}
}
System.out.println(pn);
}
public int getExpCoeff(int term) {
return expCoeff[term];
}
public int[] getExpCoeff() {
return expCoeff;
}
public void setExpCoeff(int[] expCoeff) {
this.expCoeff = expCoeff;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.