Question
I'm stuck in part one of my project. Any help is really appreciated.
Part 1:
Part 2:
Part 3:
Example Runs:
Here's what i've got so far:
import java.util. *;
public class PolyAreaOne
{
public static double evalPoly(double[] c, double x)
{
double y, power; //Variables
y = 0;
power = 1;
for(int i = 0; i < c.length; i++){
y = y + c[i] * power;
power *= x;
}
return y;
}
public static void main (String[] args)
{
Scanner in = new Scanner(System.in); //Scanner for user input
double x, y; // Variables
int degree,powerX;
double a,b;
int nsteps;
String answer = "yes";
String answer2 = "yes";
while (answer2.equals("yes"))
{
System.out.print("Enter degree of polynomial: ");
degree = in.nextInt();
double []c=new double[degree+1]; //the n+1 coefficient
for(int i=degree;i>=0;i--){
System.out.print ("Enter coefficent" + i + ":");
c[i]=in.nextDouble();
}
answer = "yes"; //
while (answer.equals("yes"))
{
System.out.println ("Please enter x: ");
x = in.nextDouble();
y = 0;
powerX=1;
for(int i=0;i<=degree;i++){
y=y+c[i]*powerX;
powerX*=x;
}
System.out.println ("The answer is: " + y);
System.out.println ("Evaluate another x?: ");
answer = in.next();
}
System.out.println ("Another Polynomial?");
answer2 = in.next();
System.out.println ("Thanks, End of Program.");
}
}
}
Explanation / Answer
Okay, I ran your code, and it looks like so far you have it setting up the co-efficients for the polynomial correctly, and it will calculate a given X for that polynomial correctly. It doesn't look like you are using your function to calculate the polynomial, though. Instead of asking for a single x, you need to ask for a starting x, ending x, and how many steps the user wants, and set them accordingly. Then you run a loop to repeatedly call evalPoly with every value of x... but judging by your answer to my comment, you already know this, but are unsure of the exact form the loop should take. So I'll just give you code. Here is the code for your program with a for loop added: http://pastebin.com/EJK6vk2h Here it is with a while loop instead of a for loop (like your assignment gave, though a for loop is better suited to this): http://pastebin.com/8YKrr2fm Notice that the only difference between the two is changing "for" to "while", moving the initial condition of "steps" to right before the loop, and moving "steps++" to the end of the loop. The for loop does this exact same thing, but it lets you set the initial condition and increment in a convenient place. In both cases, they set x to initially be at the beginning x value. You call evalPoly with this x value and your array of co-efficients, and it returns the resulting f(x). With both of these values in hand, you can print out the output. Then you increase x by the stepsize (calculated before the loop), and increase the number of steps you have taken. The loop then repeats, and it does so until you have completed the number of steps necessary to get from your initial value to the ending value. I worked off of the example you gave, but an alternate loop would be this: while (x