Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package question2;
  2.  
  3. /*represents a polynomial with a
  4.  *  degree and coefficients
  5.  *  can have a whole number degree
  6.  *  and a array of coefficients sized based on degree
  7.  */
  8. public class Polynomial {
  9.  
  10.     int degree;
  11.     double[] coefficients;
  12.  
  13. /*
  14.  * regular constructor for polynomial
  15.  * only fills the degree variable of object
  16.  */
  17. public Polynomial(int deg) {
  18.     degree=deg;
  19.     coefficients=new double[deg+1];
  20. }
  21.  
  22. /*
  23.  * copy constructor
  24.  */
  25. public Polynomial(Polynomial p) {
  26.     this.degree=p.degree;
  27.     this.coefficients=p.coefficients;
  28. }
  29.  
  30. /*
  31.  * sets value of coefficient in array, use in loop the index
  32.  * is the number of coefficients in polynomial equation
  33.  * based on standard polynomial equation
  34.  */
  35. public void setCoefficient(int index, double value) {
  36.     coefficients[index]=value;
  37. }
  38.  
  39. /*
  40.  * supply value of coefficient from array, use in loop
  41.  */
  42. public double getCoefficient(int index) {
  43.     double value=this.coefficients[index];
  44.     return value;
  45. }
  46.  
  47. /*
  48.  * Calculates y of fully constructed polynomial given a x
  49.  */
  50. public double evaluate(double x){
  51.     double value=this.coefficients[0];
  52.     for(int i=0; i<=this.degree; i++) {
  53.         value=value+(  (this.coefficients[i])  *   (Math.pow(x, i))    );
  54.     }
  55.     return value;
  56.    
  57. }
  58.  
  59. /*
  60.  * Determines if two polynomials are equal
  61.  * compares degree and coefficients in order
  62.  */
  63. public boolean equals(Polynomial poly) {
  64.     boolean isOrdered=false;
  65.    
  66.     if(poly.degree==this.degree) {
  67.         for(int i=0; i<this.degree; i++) {
  68.             if(this.coefficients[i]==poly.coefficients[i]) {
  69.             isOrdered=true; }
  70.             else {
  71.                 isOrdered=false; }
  72.        
  73.         }  
  74.     }
  75. if(isOrdered) {
  76.     return true;
  77. }
  78. else {
  79.     return false;
  80. }
  81.    
  82. }
  83.    
  84.  
  85. /*
  86.  * converts polynomial to string
  87.  * as visual of equation
  88.  */
  89. public String toString() {
  90.     String out=""+this.coefficients[0];
  91.    
  92.    
  93.     for(int i=1; i<=this.degree; i++) {
  94.         if(this.coefficients[i]>0)
  95.         out=out+" + ("+this.coefficients[i]+")x^"+i;
  96.     }
  97.     return out;
  98. }
  99.  
  100. }
  101. --------------------------------------------------------------------
  102. package question2;
  103. import java.util.Scanner;
  104.  
  105. public class PolynomialTester {
  106.  
  107.     public static void main(String[] args) {
  108.        
  109.         Scanner keyb= new Scanner(System.in);
  110.        
  111.         System.out.print("Enter degree of polynomial: ");
  112.         int degree=keyb.nextInt();
  113.         if(degree<=0) {
  114.             System.out.print("Invalid degree, re-enter: ");
  115.             degree=keyb.nextInt();
  116.         }
  117.        
  118.         Polynomial poly1=new Polynomial(degree);
  119.         double input;
  120.        
  121.         for(int i=0; i<=degree; i++) {
  122.             System.out.print("Enter Coeff for degree "+i+": ");
  123.             input=keyb.nextDouble();
  124.             poly1.setCoefficient(i, input);
  125.         }  
  126.        
  127.         System.out.print("Polynomial: "+poly1.toString());
  128.         boolean exit=false;
  129.         String userInput="";
  130.         double x=0;
  131.        
  132.         do {
  133.             System.out.println();
  134.             System.out.print("Enter a value of x for which to evaluate the polynomial: ");
  135.             userInput=keyb.next();
  136.             try {
  137.                 x=Double.parseDouble(userInput);
  138.                 System.out.print("For x ="+x+", polynomial = "+poly1.evaluate(x));
  139.             }catch (NumberFormatException e){
  140.                 if (userInput.compareTo("quit") == 0) {
  141.                     exit = true;
  142.                 }
  143.                 else {
  144.                     exit = false;
  145.                 }
  146.             }
  147.         }while(!exit);
  148.        
  149.         System.out.print("Created copy of polynomial");
  150.         Polynomial poly2=new Polynomial(poly1);
  151.         System.out.println();
  152.         System.out.print("Polynomials are equal: "+poly1.equals(poly2));
  153.         System.out.println();
  154.         System.out.println("To make them unequal enter a index and coefficent to change:");
  155.         int index=keyb.nextInt();
  156.         input=keyb.nextDouble();
  157.         poly2.setCoefficient(index, input);
  158.         System.out.print("Polynomials are equal: "+poly1.equals(poly2));
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement