Re: NEED HELP NOW, DUE TODAY
It helps to post the full stack trace - which tells what line the exception is being thrown, and should point you in the right direction. I'd suggest reading this link: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
add some System.out.println statements to debug and print out the values of the indexes you are trying to access, and look at those loops carefully.
Also, suggested reading: Ease Up at JavaRanch
Re: NEED HELP NOW, DUE TODAY
It should read like a table.
First producing the I's, then x[i]'s which range depending on your min and max, and then y[i] using the forumla in terms of x's.
Lastly, and this is where my error occurs. Im trying to find where the numbers have to Cross ZERO, or basically what my If statments say. But its not working. Help?
Re: NEED HELP NOW, DUE TODAY
Did you read read my last post? None of what you just said suggests otherwise, and stating 'its not working' is fairly meaningless - what's not working? Whats the behavior? Stack trace? Output (expected and received)? Suggested reading: How To Ask Questions The Smart Way
Re: NEED HELP NOW, DUE TODAY
Re: NEED HELP NOW, DUE TODAY
Your inner for loop should probably not be reseting "i", you should probably choose a different variable name such as "j".
Code Java:
double lastY = 0;
for(int i=0;i<numPoints;i++){
System.out.print(i+"\t");
x[0]=minX;
x[numPoints-1]= maxX;
x[i]=x[0]+ i*((maxX-minX)/(numPoints-1));
y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
System.out.print(i);
System.out.print("\t");
System.out.print(x[i]);
System.out.print("\t");
System.out.println(y[i]);
//i gets reset here
for(i=0;i<numPoints;i++){
if (y[i]>=0 && y[i+1]<0){
...
}//for
}//for
Also "y[i+1]", will be null since it hasn't yet been populated with a value. The OutOfBounds exception is occuring when you reach the next to last interation in "y[i+1]". If numPoints = 5, then plugging the values in during the next to last iteration we see:
Code Java:
for (i=0; i<=5;i++) {
if (y[5] >=0 && y[5+1]<0) ...
y[5+1]" also referred to as "y[6]" doesn't exist. Only y[0], y[1], y[2], y[3], y[4], y[5] exist. You could probably eliminate the inner for loop and just keep track of the last y[i] value and compare it to the current y[i] value. Try 1 of the following two code examples:
Since the function appears linear (and a linear function will only cross 0 once) you could do the following:
Code Java:
Scanner input= new Scanner(System.in);
double minX =0.0;
double maxX=0.0;
int numPoints=0;
double lastX=0.0;
double lastY=0.0;
System.out.print("Enter number of points: ");
numPoints=input.nextInt();
System.out.print("Enter min x: ");
minX=input.nextDouble();
System.out.print("Enter max x: ");
maxX=input.nextDouble();
System.out.println("Numpoints: " + numPoints);
double [] x= new double[numPoints];
double [] y= new double[numPoints];
int change=0;
System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
for(int i=0;i<numPoints;i++){
System.out.print(i+"\t");
x[0]=minX;
x[numPoints-1]= maxX;
x[i]=x[0]+ i*((maxX-minX)/(numPoints-1));
y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
System.out.print(i);
System.out.print("\t");
System.out.print(x[i]);
System.out.print("\t");
System.out.println(y[i]);
//go to else if for first iteration
if (i>0){
if(lastY >=0 && y[i]<=0){
//System.out.println("Inside inner if");
change=i;
}//if
else if(lastY<= 0 && y[i]>=0){
//System.out.println("Inside inner else if");
change=i;
}//else if
}//if
//in case the first number starts on 0
else if(y[i]==0){
//System.out.println("Inside inner if");
change=i;
}//else if
//this will print out the value of change during every iteration,
//probably not what you want
//if you only want to print it out once, move it to the outside of the "for" loop
//since the function appears linear it will only cross the "y" axis 1 time,
//then you don't need to use an array, you could just use "double change;"
//and eliminate "numChange"
System.out.print("\t"+change);
System.out.println();
//set lastY=y[i]
lastY = y[i];
}//for
Otherwise, you can do the following for both linear and non-linear functions (which may cross the y axis more than once):
Code Java:
Scanner input= new Scanner(System.in);
double minX =0.0;
double maxX=0.0;
int numPoints=0;
int numChange=0;
double lastX=0.0;
double lastY=0.0;
System.out.print("Enter number of points: ");
numPoints=input.nextInt();
System.out.print("Enter min x: ");
minX=input.nextDouble();
System.out.print("Enter max x: ");
maxX=input.nextDouble();
System.out.println("Numpoints: " + numPoints);
double [] x= new double[numPoints];
double [] y= new double[numPoints];
int [] change= new int[numPoints];
System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
for(int i=0;i<numPoints;i++){
System.out.print(i+"\t");
x[0]=minX;
x[numPoints-1]= maxX;
x[i]=x[0]+ i*((maxX-minX)/(numPoints-1));
y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
System.out.print(i);
System.out.print("\t");
System.out.print(x[i]);
System.out.print("\t");
System.out.println(y[i]);
//go to else if for first iteration
if (i>0){
if(lastY >=0 && y[i]<=0){
//System.out.println("Inside inner if");
change[numChange]=i;
numChange++;
}//if
else if(lastY<= 0 && y[i]>=0){
//System.out.println("Inside inner else if");
change[numChange]=i;
//added numChange++
numChange++;
}//else if
}//if
//in case the first number starts on 0
else if(y[i]==0){
//System.out.println("Inside inner if");
change[numChange]=i;
numChange++;
}//else if
//we will print out the crossing points outside of this for loop
// System.out.print("\t"+change[0]);
//System.out.print("Outside inner for \t"+change[numChange]);
System.out.println();
//set lastY=y[i]
lastY = y[i];
}//for
System.out.print("The function crosses the y-axis at the following i value(s):");
System.out.print("\t");
for (int j=0; j<numChange;j++){
if (j>0){
System.out.print(", " + change[j]);
}//if
else{
System.out.print(change[j]);
}//else
}//for
System.out.println();
System.out.println();