Question: Java Programming package lab6; import java.util.Scanner; public class Lab6 { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int option; do {

Java Programming

package lab6;

import java.util.Scanner;

public class Lab6 {

public static void main(String[] args) {

Scanner cin = new Scanner(System.in);

int option;

do

{

menu();

System.out.print("Select an option -> ");

option = cin.nextInt();

double x;

if(option == 1){

System.out.print("Enter the degree of the polynomial -> ");

int degree = cin.nextInt();

double[] poly = new double [degree + 1];

int i =0;

if (degree < 0)

System.out.print("Error: The degree of the polynomial must be nonnegative.");

else

{

System.out.print("Enter the coefficients -> ");

while(cin.hasNextDouble()){

poly[i] = cin.nextDouble();

i++;

}

}

}

else if(option == 2) {

if (poly.length == 0)

System.out.println("Error: No polynomial has been created.");

else

{

polyToString(poly);

}

}

else if(option == 3) {

if (poly.length == 0)

System.out.println("Error: No polynomial has been created.");

else {

System.out.print("Enter a value at which the polynomial will be evaluated -> ");

x = cin.nextDouble();

polyToString(poly);

polyEval(poly,x);

}

}

} while (option != 0);

}

public static void menu() {

System.out.println("U N I V A R I A T E P O L Y N O M I A L A P P L I C A T I O N");

System.out.println("=================================================================");

System.out.println("Create a Univariate Polynomial............................... [1]");

System.out.println("Display the Polynomial in Standard Form.......................[2]");

System.out.println("Evaluate the Polynomial at a Point............................[3]");

System.out.println("Exit Program..................................................[0]");

System.out.println("=================================================================");

}

public static String polyToString(double[] poly) {

System.out.print("f(x) = ");

String strPoly = "";

if(poly.length == 1)

strPoly = "" + poly[0];

else if (poly.length == 2) {

if (poly[0] == 1) {

strPoly = "x";

if (poly[1] < 0)

strPoly = strPoly + " - " + poly[1] * -1;

else if (poly[1] > 0)

strPoly = strPoly + " + " + poly[1];

}

else if (poly[0] == -1) {

strPoly = "-x";

if (poly[1] < 0)

strPoly = strPoly + " - " + poly[1] * -1;

else if (poly[1] > 0)

strPoly = strPoly + " + " + poly[1];

}

else if (poly[0] == 0) {

strPoly = "";

if (poly[1] < 0)

strPoly = strPoly + " - " + poly[1] * -1;

else if (poly[1] > 0)

strPoly = strPoly + " + " + poly[1];

}

}

else {

strPoly = poly[0] + "x^" + (poly.length - 1);

for(int i = 0; i < poly.length - 2; i++) {

if (poly[i] == 1)

strPoly = strPoly + " + x^" + (poly.length - i);

else if (poly[i] == -1)

strPoly = strPoly + " - x^" + (poly.length - 1);

else if(poly[i] < 0)

strPoly = strPoly + " - " + (poly[i] * -1) + "x^" + (poly.length - i);

else if(poly[i] > 0)

strPoly = strPoly + " - " + poly[i] + "x^" + (poly.length - i);

}

if (poly[poly.length - 2] == 1)

strPoly = strPoly + " + x";

else if (poly[poly.length - 2] == -1)

strPoly = strPoly + " - x";

else if (poly[poly.length - 2] > 0)

strPoly = strPoly + " + " + poly[poly.length - 2] + "x";

else if (poly[poly.length - 2] < 0)

strPoly = strPoly + " - " + (poly[poly.length - 2] * -1) + "x";

if (poly[poly.length - 1] < 0)

strPoly = strPoly + " - " + (poly[poly.length - 1] * -1);

if (poly[poly.length - 1] > 0)

strPoly = strPoly + " + " + (poly[poly.length - 1]);

System.out.print(strPoly);

} return strPoly;

}

public static double polyEval(double[] poly, double x) {

double eval = 0;

for(int i = 0; i < poly.length; i++) {

eval = eval + (poly[i] * Math.pow(x,poly.length - i));

}

return eval;

}

}

Code in the main method won't run because the of the poly array. Not sure what to do to fix it.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!