need help:fill the missing parts. Only the missing parts are filled. import java
ID: 665118 • Letter: N
Question
need help:fill the missing parts.
Only the missing parts are filled.
import java.util.Scanner;
public class PolynomialTest
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
PolynomialTerm[] terms;int hiExp;
double constant, x, result = 0.0;
System.out.println("Highest exponent: ");
hiExp = scan.nextInt();
terms = new PolynomialTerm[hiExp];
// loop code to get the terms of the polynomial and fill the array
System.out.println("Constant: ");;
constant = scan.nextDouble();
System.out.printf("nThe polynomial is: ");
for (PolynomialTerm t:terms) System.out.print(t+"+");
System.out.println(constant);
System.out.print(" Indicate a value of x: ");
x = scan.nextDouble();
// code to calculate the value of the polynomial at x
// print the result
System.out.printf("The value at x = %.1f is : %.1f ", x, result);
}
}
Explanation / Answer
import java.io.*;
public class PolyArithmetic
{
private int[] coefficient; // coefficient
private int power; // power of polynomial
public PolyArithmetic(int m, int n)
{
coefficient = new int[n+1];
coefficient[n] = m;
power = polyPower();
}
// return the power of this polynomial
public int polyPower()
{
int d = 0;
for (int i = 0; i < coefficient.length; i++)
if (coefficient[i] != 0)
d = i;
return d;
}
// return z = m + n
public PolyArithmetic add(PolyArithmetic n)
{
PolyArithmetic m = this;
PolyArithmetic z = new PolyArithmetic(0, Math.max(m.power, n.power));
for (int i = 0; i <= m.power; i++)
z.coefficient[i] += m.coefficient[i];
for (int i = 0; i <= n.power; i++)
z.coefficient[i] += n.coefficient[i];
z.power = z.polyPower();
return z;
}
//return z = m – n
public PolyArithmetic sub(PolyArithmetic n)
{
PolyArithmetic m = this;
PolyArithmetic z = new PolyArithmetic(0, Math.max(m.power,n.power));
for (int i = 0; i <= m.power; i++)
z.coefficient[i] += m.coefficient[i];
for (int i = 0; i <= n.power; i++)
z.coefficient[i] -= n.coefficient[i];
z.power = z.polyPower();
return z;
}
// for converting to string
public String toString()
{
if (power == 0)
return "" + coefficient[0];
if (power == 1)
return coefficient[1] +"x+" + coefficient[0];
String s = coefficient[power]+"x^"+ power;
for (int i = power-1; i >= 0; i--)
{
if(coefficient[i] == 0)
continue;
else if (coefficient[i] > 0)
s = s + " + " + ( coefficient[i]);
else if (coefficient[i] < 0)
s = s + " - " + (-coefficient[i]);
if(i == 1)
s = s + "x";
else if (i > 1)
s = s + "x^" + i;
}return s;
}
// main method
public static void main(String[] args)
{
PolyArithmetic a1 = new PolyArithmetic(5,4); //5x^4
PolyArithmetic a2 = new PolyArithmetic(4,3); //4x^3
PolyArithmetic a3 = new PolyArithmetic(3,2); //3x^2
PolyArithmetic a4 = new PolyArithmetic(2,1); //2x^1
PolyArithmetic a = a1.add(a2).add(a3).add(a4);
PolyArithmetic b1 = new PolyArithmetic(4,4); //4x^4
PolyArithmetic b2 = new PolyArithmetic(4,3); //4x^3
PolyArithmetic b3 = new PolyArithmetic(4,2); //4x^2
PolyArithmetic b4 = new PolyArithmetic(4,0); //4
PolyArithmetic b = b1.add(b2).add(b3).add(b4);
System.out.println("a(x)= " + a);
System.out.println("b(x)= " + b);
System.out.println("a(x) + b(x) = " + a.add(b));
System.out.println("a(x) - b(x) = " + a.sub(b));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.