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

Problem # 1 : Create a class called polynomial for defining a one variable polyn

ID: 3701844 • Letter: P

Question

Problem # 1 : Create a class called polynomial for defining a one variable polynomial equation and perform some simple algebraic operations on it. Polynomial equation is on the form: Where n is a positive integer and it represents the highest power of the polynomial equation. And c is the coefficient of each term. Write a program to test your class. Provide a constructor that enables an object of this class to be initialized when it's declared Provide a no-argument constructor with default values in case no initializes re provided. Provide public methods that a) Add to the equation a term. It takes as an argument the coefficient of the term and power of the variable x. b) Multiply a term to the equation. It takes as an argument e coefficient of the term and power of the variable x. e) Substitute x with a constant value and calculate the polynomial equation. It l take the value of x as a d) Method toString that is used to print any polynomial perform the following operations The result is stored in a new polynomial equation. th The result is stored in a new polynomial equation. double number and calculate the result. equation in the form: Note that x so it is dropped, also if coefficient is equal to zero it should be dropped as well. And when the coefficient is a negative number the+sign should Sample Run: Enter the polynomial power:3 Enter coefficient of x 3: 4 Enter coefficient of x 2: 0 Enter coefficient of x 1:2 Enter coefficient of x 0: 1 The equation is: 4x 32x1 What would you like to do? 1. Add, 2. Multiply.3. Substitute: 1

Explanation / Answer

//Polynomial.java

import java.util.ArrayList;

class Term {
   // Instance variables
   private int coefficient;
   private int power;

   /**
   * Default constructor
   */
   public Term() {
       this(0, 0);
   }


   public Term(int coefficient, int power) {
       this.coefficient = coefficient;
       this.power = power;
   }


   public boolean isZero() {
       return (this.coefficient == 0);
   }


   public boolean isNegative() {
       return (this.coefficient < 0);
   }

   /**
   * @return the coefficient
   */
   public int getCoefficient() {
       return coefficient;
   }

   /**
   * @return the power
   */
   public int getPower() {
       return power;
   }

   /**
   * @param coefficient
   *            the coefficient to set
   */
   public void setCoefficient(int coefficient) {
       this.coefficient = coefficient;
   }

   /**
   * @param power
   *            the power to set
   */
   public void setPower(int power) {
       this.power = power;
   }

   /**
   * Returns a string representation of this term
   */
   public String toString() {
       // - if coefficient is 0, return "0"
       if (isZero())
           return "0";
       // - else if the power is 0, return the coefficient
       else if (power == 0) {
           if (isNegative())
               return "- " + (coefficient * -1);
           else
               return coefficient + "";
           // - else if the coefficient is 1 and the power is 1, return "x"
       } else if (coefficient == 1 && power == 1)
           return "x";
       // - else if the coefficient is -1 and the power is 1, return "-x"
       else if (coefficient == -1 && power == 1)
           return "- x";
       // - else if the power is 1, return coefficient and "x"
       else if (power == 1) {
           if (isNegative())
               return "- " + (coefficient * -1) + "x";
           else
               return coefficient + "x";
           // - else if coefficient is 1, return "x^" and the power
       } else if (coefficient == 1)
           return "x^" + power;
       // - else if the coefficient is -1, return "-x^" and the power
       else if (coefficient == -1)
           return "- x^" + power;
       // - else return coefficient and "x^" and power
       else {
           if (isNegative())
               return "- " + (coefficient * -1) + "x^" + power;
           else
               return coefficient + "x^" + power;
       }
   }

   /**
   * Evaluates with whatever double is received
   *
   * @param value
   *            value of x
   */
   public double substitute(double x) {
       return coefficient * Math.pow(x, power);
   }
}

public class Polynomial {

   // Instance variable
   private ArrayList<Term> terms;

   /**
   * Default constructor
   */
   public Polynomial() {
       this.terms = new ArrayList<Term>();
   }

   /**
   * Adds a term to the equation
   *
   * @param coefficient
   *            - coefficient of the term
   * @param power
   *            - power of the term
   */
   public void addIn(int coefficient, int power) {
       // Create a new term
       Term newTerm = new Term(coefficient, power);

       // Check if term is zero
       if (newTerm.isZero())
           return;

       // Check if this is the first term to be inserted
       if (this.terms.size() == 0)
           this.terms.add(newTerm);
       else {

           // Traverse the list to find the correct index
           for (int i = 0; i < this.terms.size(); i++) {

               // Get term at i
               Term term = this.terms.get(i);

               // Check power of term with aTerm
               if (newTerm.getPower() == term.getPower()) {
                   // Add in aTerm to term
                   term.setCoefficient(term.getCoefficient() + newTerm.getCoefficient());

                   // Check if term is zero
                   if (term.isZero())
                       this.terms.remove(i);

                   return;

               } else if (newTerm.getPower() > term.getPower()) {
                   this.terms.add(i, newTerm);
                   return;
               }
           }

           // If aTerm is not inserted in the above for loop
           // Add aTerm at the end of the list
           this.terms.add(newTerm);
       }
   }

   /**
   * Adds a term to the equation and returns the resulting equation as a new Polynomial*/

   public Polynomial add(int coefficient, int power) {
       // Create a new polynomial
       Polynomial newPolynomial = new Polynomial();

       // Copy all terms of existing polynomial to newPolynomial
       for (Term term : terms)
           newPolynomial.addIn(term.getCoefficient(), term.getPower());

       // Add new term to the newPolynomial
       newPolynomial.addIn(coefficient, power);

       return newPolynomial;
   }

   /**
   * Multiplies a term to the equation and returns the resulting equation as a
   * new Polynomial/


   public Polynomial multiply(int coefficient, int power) {
       // Create a new polynomial
       Polynomial newPolynomial = new Polynomial();

       // Multiply all terms of existing polynomial with coefficient and power
       // and add the resulting term to newPolynomial
       for (Term term : terms)
           newPolynomial.addIn(term.getCoefficient() * coefficient, term.getPower() + power);

       return newPolynomial;
   }

   /**
   * Substitutes the value of x with a constant value and calculates the
   * polynomial equation
   *
   * @param x
   *            - constant value
   */
   public double substitute(double x) {
       double sum = 0;

       // Substitute the value of x in all terms in theAL
       for (Term term : this.terms)
           sum += term.substitute(x);

       return sum;
   }

   /**
   * Returns a string representation of this polynomial
   */
   public String toString() {
       StringBuffer sb = new StringBuffer();

       // Append all terms
       for (int i = 0; i < this.terms.size(); i++) {
           if (i == 0)
               sb.append(this.terms.get(i));
           else if (this.terms.get(i).isNegative())
               sb.append(" " + this.terms.get(i));
           else
               sb.append(" + " + this.terms.get(i));
       }

       return sb.toString();
   }
}


//PolynomialTest.java

import java.util.Scanner;

public class PolynomialTest {

   /**
   * Displays the menu to the user
   */
   static void menu() {
       System.out.println("What would you like to do?");
       System.out.print("1. Add, 2.Multiply, 3.Substitute: ");
   }

   public static void main(String[] args) {
       // Create Polynomial object
       Polynomial polynomial = new Polynomial();

       // Scanner to get user input
       Scanner in = new Scanner(System.in);

       // Initialize polynomial
       System.out.print("Enter the polynomial power: ");
       int power = in.nextInt();
       for (int i = 0; i <= power; i++) {
           System.out.print("Enter coefficient of x^" + (power - i) + ": ");
           int coefficient = in.nextInt();
           polynomial.addIn(coefficient, power - i);
       }

       // Display polynomial
       System.out.println("The equation is: " + polynomial);

       // Start
       boolean exit = false;
       while (!exit) {
           // Show menu
           menu();

           // Get user choice
           int choice = in.nextInt();

           switch (choice) {
           case 1: // Add
               // Get coefficient and power
               System.out.print("x power: ");
               power = in.nextInt();
               System.out.print("x coefficient: ");
               int coefficient = in.nextInt();

               // Add new term
               Polynomial newPolynomial = polynomial.add(coefficient, power);

               // Display newPolynomial
               System.out.println("The new equation is " + newPolynomial);
               break;

           case 2: // Multiply
               // Get coefficient and power
               System.out.print("x power: ");
               power = in.nextInt();
               System.out.print("x coefficient: ");
               coefficient = in.nextInt();

               // Multiply the new term
               newPolynomial = polynomial.multiply(coefficient, power);

               // Display newPolynomial
               System.out.println("The new equation is " + newPolynomial);
               break;

           case 3: // Substitute
               // Get value of x
               System.out.print("x value: ");
               double value = in.nextDouble();

               // Get result
               System.out.println("Result is: " + polynomial.substitute(value));
               break;

           default: // Exit
               exit = true;
           }
       }

    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote