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

Using Java Implement the following Programing Problem: A polynomial of a single

ID: 3886462 • Letter: U

Question

Using Java

Implement the following Programing Problem:
A polynomial of a single variable x with integer coefficients is an expression of the form

p(x)=c0 +c1x+c2x2 +c3x3 +...+cnxn (1) where ci i = 0, 1, . . . , n, are integers. Create a class for polynomials up to the nth degree. Use

this class to implement a calculator of polynomials, which has the following menu:

After the choice is entered, ask the user for the degree of the polynomila he/she wants to create. Then ask for their coefficients and the value at which the polynomial would be evaluated. A sample output for choice a) would be:

Your polynomial is p(x) = 2x^4 - x^2 + 3x - 5

A sample output for choice b) would be:

Explanation / Answer

import java.io.*;
import java.util.*;

class term{
   public int coefficient;
   public int power;
}

class Polynomial{
   
    private term[] poly;
    private   int size;


    public Polynomial() {
         poly = new term[10];
         for (int i = 0; i<10; i++)
             poly[i] = new term();
         size = 0;
       }
    public void addTerm(int a, int b){
          poly[size].coefficient = a;
          poly[size].power = b;
          size = size + 1;
       }
       double value(double x){
          double value = 0;
          for (int i = 0; i<size; i++){
              value = value + poly[i].coefficient * Math.pow(x,poly[i].power);
             
                      
          }
          return value;
       }
       void derivative(){
         
          for (int i = 0; i<size; i++){
             
              if (i > 0){
                 if (poly[i].coefficient*poly[i].power > 0){
                     System.out.print("+");     
                 }
              }
             
              if (poly[i].power > 0)
                 if (poly[i].coefficient != 0){
                    if (poly[i].power -1 >0)
                       System.out.print(String.valueOf(poly[i].coefficient*poly[i].power) + "x^" + String.valueOf(poly[i].power-1) +" ");
                    else
                       System.out.print(String.valueOf(poly[i].coefficient*poly[i].power) + " ");
                }
          }
          System.out.println();
         
       }


}

public class DemoPoly{

   public static void main(String[] args) {
        Polynomial p = new Polynomial();
        int choice = 0;
        double x=0;
        Scanner sc = new Scanner(System.in);

         System.out.println("Enter degree of polynomial :");
        do {
            System.out.println("1.Polynomial Evaluation");
            System.out.println("2.Polynomial Derivative");
            System.out.println("3.Exit");

            System.out.println("Enter your choice :");
            choice = sc.nextInt();
            switch(choice) {
                 case 1: System.out.println("Enter degree of polynomial");
                         int n = sc.nextInt();
                         for (int i = 0; i<=n; i++){
                             int a = sc.nextInt();
                             int b = sc.nextInt();
                             p.addTerm(a,b);
                         }
                         System.out.println("Enter value of x");
                         x = sc.nextDouble();
                         System.out.println("Value of the polynomial is : " + p.value(x));
                         break;
                case 2: System.out.println("The derivative is ");
                         p.derivative();
                         break;

            }
        }while (choice != 3);

   }
}

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