IN JAVA Problem A Polynomials of degree 2 20 points Download

IN JAVA

Problem A: Polynomials of degree 2 (20 points) Download the main() method from the Polynomial.java file. So far, only the main() method is written and you must complete the file to add functionality to add and evaluate degree two polynomials. The order of the coefficients go from largest power to smallest, so entering “1 2 3” should give you the polynomial: x2 + 2x + 3. YOU CANNOT CHANGE THE MAIN() METHOD, except for renaming the Polynomial class to your internet ID.

Example 1 (user input is bold):

Enter coefficients for the polynomial: 1 2 3

What polynomial to add? 4 5 6

Result: a=5.0, b=7.0, c=9.0

Where do you want to evaluate the polynomial at? 10 f(10.0)=579.0

Example 2 (user input is bold):

Enter coefficients for the polynomial: 1 4 2

What polynomial to add? 0 ­-3 ­-1

Result: a=1.0, b=1.0, c=1.0

Where do you want to evaluate the polynomial at? 0 f(0.0)=1.0

Solution

import java.util.Scanner;

public class Polynomial {
   private double a;
   private double b;
   private double c;
  
   //getters for A
   public double getA() {
       return a;
   }
   //getters for B
   public double getB() {
       return b;
   }
   //getters for C
   public double getC() {
       return c;
   }
  
   //Constructor to assign the values to a,b and c
public Polynomial(double a, double b, double c) {
       this.a=a;
       this.b=b;
       this.c=c;
   }

   public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println(\"Enter coefficients for the polynomial: \");
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
  
Polynomial p1 = new Polynomial(a,b,c);
  
System.out.println(\"What polynomial to add?\");
a = in.nextDouble();
b = in.nextDouble();
c = in.nextDouble();
  
Polynomial p2 = new Polynomial(a,b,c);
  
Polynomial result = p1.add(p2);
System.out.println(\"Result: a=\"+result.getA()+\", b=\"+result.getB()+\", c=\"+result.getC());

System.out.println(\"Where do you want to evaluate the polynomial at?\");
double x = in.nextDouble();
System.out.println(\"f(\"+x+\")=\"+result.eval(x));
}

   // eval method to evaluate the polynomial function
   //return the string of the sum
   private String eval(double x) {
       double sum=0;
       sum+=this.a*(x*x);
       sum+=this.b*x;
       sum+=this.c;
       return String.valueOf(sum);
   }

   //add method to add the new polynomial to the existing one.
   //return the polynomial after adding
   private Polynomial add(Polynomial p2) {
       this.a+=p2.a;
       this.b+=p2.b;
       this.c+=p2.c;
      
       return this;
      
   }   
}

------------------output--------------------
Enter coefficients for the polynomial:
1
2
3
What polynomial to add?
4
5
6
Result: a=5.0, b=7.0, c=9.0
Where do you want to evaluate the polynomial at?
10
f(10.0)=579.0

---------------------------
Enter coefficients for the polynomial:
1
4
2
What polynomial to add?
0
-3
-1
Result: a=1.0, b=1.0, c=1.0
Where do you want to evaluate the polynomial at?
0
f(0.0)=1.0

Note: all the methods are explained using comments. Feel free to ask questions. God bless you!

IN JAVA Problem A: Polynomials of degree 2 (20 points) Download the main() method from the Polynomial.java file. So far, only the main() method is written and y
IN JAVA Problem A: Polynomials of degree 2 (20 points) Download the main() method from the Polynomial.java file. So far, only the main() method is written and y
IN JAVA Problem A: Polynomials of degree 2 (20 points) Download the main() method from the Polynomial.java file. So far, only the main() method is written and y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site