Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package question2;
- /*represents a polynomial with a
- * degree and coefficients
- * can have a whole number degree
- * and a array of coefficients sized based on degree
- */
- public class Polynomial {
- int degree;
- double[] coefficients;
- /*
- * regular constructor for polynomial
- * only fills the degree variable of object
- */
- public Polynomial(int deg) {
- degree=deg;
- coefficients=new double[deg+1];
- }
- /*
- * copy constructor
- */
- public Polynomial(Polynomial p) {
- this.degree=p.degree;
- this.coefficients=p.coefficients;
- }
- /*
- * sets value of coefficient in array, use in loop the index
- * is the number of coefficients in polynomial equation
- * based on standard polynomial equation
- */
- public void setCoefficient(int index, double value) {
- coefficients[index]=value;
- }
- /*
- * supply value of coefficient from array, use in loop
- */
- public double getCoefficient(int index) {
- double value=this.coefficients[index];
- return value;
- }
- /*
- * Calculates y of fully constructed polynomial given a x
- */
- public double evaluate(double x){
- double value=this.coefficients[0];
- for(int i=0; i<=this.degree; i++) {
- value=value+( (this.coefficients[i]) * (Math.pow(x, i)) );
- }
- return value;
- }
- /*
- * Determines if two polynomials are equal
- * compares degree and coefficients in order
- */
- public boolean equals(Polynomial poly) {
- boolean isOrdered=false;
- if(poly.degree==this.degree) {
- for(int i=0; i<this.degree; i++) {
- if(this.coefficients[i]==poly.coefficients[i]) {
- isOrdered=true; }
- else {
- isOrdered=false; }
- }
- }
- if(isOrdered) {
- return true;
- }
- else {
- return false;
- }
- }
- /*
- * converts polynomial to string
- * as visual of equation
- */
- public String toString() {
- String out=""+this.coefficients[0];
- for(int i=1; i<=this.degree; i++) {
- if(this.coefficients[i]>0)
- out=out+" + ("+this.coefficients[i]+")x^"+i;
- }
- return out;
- }
- }
- --------------------------------------------------------------------
- package question2;
- import java.util.Scanner;
- public class PolynomialTester {
- public static void main(String[] args) {
- Scanner keyb= new Scanner(System.in);
- System.out.print("Enter degree of polynomial: ");
- int degree=keyb.nextInt();
- if(degree<=0) {
- System.out.print("Invalid degree, re-enter: ");
- degree=keyb.nextInt();
- }
- Polynomial poly1=new Polynomial(degree);
- double input;
- for(int i=0; i<=degree; i++) {
- System.out.print("Enter Coeff for degree "+i+": ");
- input=keyb.nextDouble();
- poly1.setCoefficient(i, input);
- }
- System.out.print("Polynomial: "+poly1.toString());
- boolean exit=false;
- String userInput="";
- double x=0;
- do {
- System.out.println();
- System.out.print("Enter a value of x for which to evaluate the polynomial: ");
- userInput=keyb.next();
- try {
- x=Double.parseDouble(userInput);
- System.out.print("For x ="+x+", polynomial = "+poly1.evaluate(x));
- }catch (NumberFormatException e){
- if (userInput.compareTo("quit") == 0) {
- exit = true;
- }
- else {
- exit = false;
- }
- }
- }while(!exit);
- System.out.print("Created copy of polynomial");
- Polynomial poly2=new Polynomial(poly1);
- System.out.println();
- System.out.print("Polynomials are equal: "+poly1.equals(poly2));
- System.out.println();
- System.out.println("To make them unequal enter a index and coefficent to change:");
- int index=keyb.nextInt();
- input=keyb.nextDouble();
- poly2.setCoefficient(index, input);
- System.out.print("Polynomials are equal: "+poly1.equals(poly2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement