CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017 

Lab 9: Polynomials

In this lab, you will create a class that allows one to use quadratic functions. You should start off with s1 driving and s2 navigating.

1. Introductions

Introduce yourself to your lab partner. Tell your lab partner what your favorite course is this semester.

Polynomial functions

In this lab you will familiarize yourself with the java representation of objects and arrays by working with a class which supports polynomial functions.

Complete the methods in Polynomial.java, as shown below. After you complete each method, demonstrate it to your instructor. Change the driver/navigator roles after each method.

// YOUR NAMES HERE!!!!

public class Polynomial {
  double[] a;  // array of coefficients
  
  
  // default Constructor creates the zero polynomial
  public Polynomial(){
    a = new double[1];
    a[0] = 0.0D;
  }
  
  
  // Constructor based on an array of coefficents
  public Polynomial(double[] A){
    a = new double[1]; // REPLACE
    a[0] = 0.0D;       // REPLACE
  }
  
  
  // Copy Constructor
  public Polynomial(Polynomial p){
    a = new double[1]; // REPLACE
    a[0] = 0.0D;       // REPLACE
  }
  
  
  // return ith coefficient (0 returns constant term)
  public double coeff(int i){
    return 0.0;  // REPLACE
  }
  
  
  // returns the degree of this polynomial based on array length
  public int degree() {
    return 0;  // REPLACE
  }
  
  
  // toString method - works as is!
  public String toString(){
    String s="";
    for(int i=a.length-1; i>=0; i--){
      s+=a[i];
      if (i!=0) s+="x^"+i+" + ";
    }
    return s;
  }
  
  
  // Evaluate polynomial at x
  // consider using Horner's method: 
  //   f(x) = a[0] + x(a[1] + x(a[2] + x(... + x(a[n-1]+ xa[n])...)))
  public double f(double x){
    return 0.0;  // REPLACE
  }
  
  
  // add the constant b to each term of p
  public static Polynomial add(Polynomial p, double b){
    return new Polynomial();  // REPLACE
  }
  
  
  // multiply each term of p by the constant b
  public static Polynomial multiply(Polynomial p, double b){
    return new Polynomial();  // REPLACE
  }
  
  
  // add two polynomials
  public static Polynomial add(Polynomial p1, Polynomial p2){
    return new Polynomial();  // REPLACE
  }
  
  
  // multiply two polynomials
  public static Polynomial multiply(Polynomial p1, Polynomial p2){
    return new Polynomial();  // REPLACE
  }
  
  
  // Use the power rule to differentiate p(x)
  // be careful when degree is zero
  public Polynomial derivative(){
    return new Polynomial();  // REPLACE
  }
  
  
  // Use the power rule to integrate p(x), assume constant C is zero
  public Polynomial integrate(){
    return new Polynomial();  // REPLACE
  }
  
  
  // Integrate this polynomial from x0 to x1
  public double integral(double x0, double x1) {
    Polynomial p = integrate();  // REPLACE
    return 0.0;  // REPLACE
  }
  
  
  public static void main(String args[]){
    Polynomial p[] = new Polynomial[10];
    int n = 0;
    p[n++] = new Polynomial();                                   // p(x) = 0;
    p[n++] = new Polynomial(new double[] {1.0});                 // p(x) = 1;
    p[n++] = new Polynomial(new double[] {2.0, -1.0});           // p(x) = -x + 2;
    p[n++] = new Polynomial(new double[] {6.0, 5.0, 1.0});       // p(x) = x^2 + 5x + 6;
    p[n++] = new Polynomial(new double[] {1.0, 5.0, 7.0, 3.0});  // p(x) = 3x^3 + 7x^2 + 5x + 1
    
    for (int i = 0;  i < n;  i++) {
      System.out.println("p(x) = " + p[i]);
      System.out.println("degree: " + p[i].degree());
      Polynomial r = new Polynomial(p[i]);
      System.out.println("r(x) = " + r);
      System.out.println("degree: " + r.degree());
      
      System.out.printf("p(%f) = %f = %f\n", 0.0, p[i].f(0.0), p[i].coeff(0));
      System.out.printf("p(%f) = %f\n", 1.0, p[i].f(1.0));
      System.out.printf("p(%f) = %f\n", -1.0, p[i].f(-1.0));
      
      Polynomial q = p[i].derivative();
      System.out.println("p'(x) = " + q);
      System.out.println("p' degree: " + q.degree());
      
      q = p[i].integrate();
      System.out.println("P(x) = " + q);
      System.out.println("P degree: " + q.degree());
      
      double v = p[i].integral(1.0, 2.0);
      System.out.println("integral from 1.0 to 2.0 = " + v);
      
      r = Polynomial.add(p[i],1.0);
      System.out.println("p(x) + 1 = " + r);
      System.out.println("degree: " + r.degree());
      
      r = Polynomial.multiply(p[i],2.0);
      System.out.println("p(x) * 2 = " + r);
      System.out.println("degree: " + r.degree());
      
      r = Polynomial.multiply(p[i],p[(i-1+n)%n]);
      System.out.println("(" + p[i] + ") * (" + p[(i-1+n)%n] + ") = " + r);
      System.out.println("degree: " + r.degree());
      
      r = Polynomial.add(p[i],p[(i-1+n)%n]);
      System.out.println("(" + p[i] + ") + (" + p[(i-1+n)%n] + ") = " + r);
      System.out.println("degree: " + r.degree());
      System.out.println("--------------------------------------------------------");
    }
  }
}




Your output should look like the following when complete:

p(x) = 0.0
degree: 0
r(x) = 0.0
degree: 0
p(0.000000) = 0.000000 = 0.000000
p(1.000000) = 0.000000
p(-1.000000) = 0.000000
p'(x) = 0.0
p' degree: 0
P(x) = 0.0x^1 + 0.0
P degree: 1
integral from 1.0 to 2.0 = 0.0
p(x) + 1 = 1.0
degree: 0
p(x) * 2 = 0.0
degree: 0
(0.0) * (3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0) = 0.0x^3 + 0.0x^2 + 0.0x^1 + 0.0
degree: 3
(0.0) + (3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0) = 3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0
degree: 3
--------------------------------------------------------
p(x) = 1.0
degree: 0
r(x) = 1.0
degree: 0
p(0.000000) = 1.000000 = 1.000000
p(1.000000) = 1.000000
p(-1.000000) = 1.000000
p'(x) = 0.0
p' degree: 0
P(x) = 1.0x^1 + 0.0
P degree: 1
integral from 1.0 to 2.0 = 1.0
p(x) + 1 = 2.0
degree: 0
p(x) * 2 = 2.0
degree: 0
(1.0) * (0.0) = 0.0
degree: 0
(1.0) + (0.0) = 1.0
degree: 0
--------------------------------------------------------
p(x) = -1.0x^1 + 2.0
degree: 1
r(x) = -1.0x^1 + 2.0
degree: 1
p(0.000000) = 2.000000 = 2.000000
p(1.000000) = 1.000000
p(-1.000000) = 3.000000
p'(x) = -1.0
p' degree: 0
P(x) = -0.5x^2 + 2.0x^1 + 0.0
P degree: 2
integral from 1.0 to 2.0 = 0.5
p(x) + 1 = 0.0x^1 + 3.0
degree: 1
p(x) * 2 = -2.0x^1 + 4.0
degree: 1
(-1.0x^1 + 2.0) * (1.0) = -1.0x^1 + 2.0
degree: 1
(-1.0x^1 + 2.0) + (1.0) = -1.0x^1 + 3.0
degree: 1
--------------------------------------------------------
p(x) = 1.0x^2 + 5.0x^1 + 6.0
degree: 2
r(x) = 1.0x^2 + 5.0x^1 + 6.0
degree: 2
p(0.000000) = 6.000000 = 6.000000
p(1.000000) = 12.000000
p(-1.000000) = 2.000000
p'(x) = 2.0x^1 + 5.0
p' degree: 1
P(x) = 0.3333333333333333x^3 + 2.5x^2 + 6.0x^1 + 0.0
P degree: 3
integral from 1.0 to 2.0 = 15.83333333333333
p(x) + 1 = 2.0x^2 + 6.0x^1 + 7.0
degree: 2
p(x) * 2 = 2.0x^2 + 10.0x^1 + 12.0
degree: 2
(1.0x^2 + 5.0x^1 + 6.0) * (-1.0x^1 + 2.0) = -1.0x^3 + -3.0x^2 + 4.0x^1 + 12.0
degree: 3
(1.0x^2 + 5.0x^1 + 6.0) + (-1.0x^1 + 2.0) = 1.0x^2 + 4.0x^1 + 8.0
degree: 2
--------------------------------------------------------
p(x) = 3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0
degree: 3
r(x) = 3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0
degree: 3
p(0.000000) = 1.000000 = 1.000000
p(1.000000) = 16.000000
p(-1.000000) = 0.000000
p'(x) = 9.0x^2 + 14.0x^1 + 5.0
p' degree: 2
P(x) = 0.75x^4 + 2.3333333333333335x^3 + 2.5x^2 + 1.0x^1 + 0.0
P degree: 4
integral from 1.0 to 2.0 = 36.083333333333336
p(x) + 1 = 4.0x^3 + 8.0x^2 + 6.0x^1 + 2.0
degree: 3
p(x) * 2 = 6.0x^3 + 14.0x^2 + 10.0x^1 + 2.0
degree: 3
(3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0) * (1.0x^2 + 5.0x^1 + 6.0) = 
   3.0x^5 + 22.0x^4 + 58.0x^3 + 68.0x^2 + 35.0x^1 + 6.0
degree: 5
(3.0x^3 + 7.0x^2 + 5.0x^1 + 1.0) + (1.0x^2 + 5.0x^1 + 6.0) = 
   3.0x^3 + 8.0x^2 + 10.0x^1 + 7.0
degree: 3
--------------------------------------------------------

When you are finished, email your lab files to your lab partner and instructor.


Copyright © 2017, David A. Reimann. All rights reserved.