{}

Build your resume with HTML & CSS and win $100

Get featured on Programiz PRO and the Wall of Inspiration.

Build your resume with HTML & CSS and win $100

Join Challenge →
Join Challenge →

Build your resume with HTML & CSS and win $100

Get featured on Programiz PRO and the Wall of Inspiration.

Build your resume with HTML & CSS and win $100

Join Challenge →
Join Challenge →
run-icon
Main.java
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.Scanner; import java.util.StringTokenizer; class PolynomialEvaluator { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("What is your polynomial function in the form of f(x)=3x^3 -5x^2 +1x^4 +9x^6 +3.1x^1 +2?"); String polynomial = keyboard.nextLine(); System.out.println("What is your x value?"); double x = keyboard.nextDouble(); double polynomialEvaluator = 0; double polynomialTotal = 0; StringTokenizer st = new StringTokenizer(polynomial); while (st.hasMoreTokens()){ double termValue = 0; StringTokenizer term = new StringTokenizer(st.nextToken(),"x^"); int termTokens = term.countTokens(); if (termTokens == 2){ String supercool = term.nextToken(); Scanner scanner1 = new Scanner(supercool); double coefficient = scanner1.nextDouble(); String supercool2 = term.nextToken(); Scanner scanner2 = new Scanner(supercool2); double exponent = scanner2.nextDouble(); termValue = coefficient*Math.pow(x,exponent); polynomialTotal = polynomialTotal+termValue; } else{ String supercool = term.nextToken(); Scanner scanner3 = new Scanner(supercool); double constant = scanner3.nextDouble(); polynomialTotal = polynomialTotal+constant; } } System.out.println("Your polynomial total is: " + polynomialTotal); } }
Output