What's wrong with my progam?
public class Square{
public int x;
public Square() {
int x[] = new int[10];
int y;
x[0] = 7;
}
public void root() {
for (int i = 0; i < 10; i++) {
x[i+1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5));
System.out.println(x[i + 1]);
}
}
}
I don't get what's wrong, my for loop does not seem to be working and it keeps on displaying the error for some reason. Could someone help me figure this out?
Okay, I wrote this program now:
public class Square
{
public double x[];
public void root()
{
double x[] = new double[10];
x[0]=7;
for(int i=0; i<8; i++)
{
x[i+1]=x[i]-(Math.pow(x[i]-2.5,2))/(2*(x[i]-2.5));
System.out.println(x[i+1]);
}
}
}
And it is showing this output:
3.625
3.0625
2.78125
2.640625
2.5703125
2.53515625
2.517578125
java.lang.NullPointerException
at Square.root(Square.java:14)
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at Square.root(Square.java:11)
I don't know why I'm getting these errors. Also, the answer should be 6.25 at some point. But, it doesn't show that output.
This is because you have defined x
previously as just an int
, instead of an array of ints.
Try this:
public class Square {
public int x[];
public Square() {
this.x = new int[10];
int y;
x[0] = 7;
}
public void root() {
for(int i = 0; i < 10; i++) {
x[i + 1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] -2.5));
System.out.println(x[i + 1]);
}
}
}
Your constructor has a local variable int[] x
which is disarded at the end of the constructor.
Try this:
public class Square{
// initialize to array of ten ints
public int x[] = new int[10];
public Square() {
x[0] = 7;
}
public void root() {
for (int i = 0; i < 10; i++) {
x[i+1] = x[i] - (Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5));
System.out.println(x[i + 1]);
}
}
}
Edit: The int y
is local to the constructor, it is discarded at the end of the constructor scope, too.
Here is the commented code explaining the problem :
public class Square
{
// Here, x is defined as an attribute of class Square, of type int
public int x;
public Square()
{
// Here, x is locally defined as a local variable, of type int[]
// It shadows the attribute x. This is considered as a bad practice.
int x[] = new int[10];
int y;
// Here, x is the local variable, of type int[]. It IS an array so
// this line is valid.
x[0]=7;
}// From this point, the local variable x is not defined anymore
// (that is the point of a local variable)
Now here :
public void root()
{
for(int i=0; i<10; i++)
{
// Here, x is referencing the attribute of class Square, which is an int
// But you try to access it as if it was and int[]
x[i+1]
First you must declare x
as an array:
public int[] x;
notice that the java style is not int x[];
Then inside Square()
you have to initialize x
like this:
x = new int[10];
Finally, this:
(Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5))
returns a double
so you have to cast it to int
:
x[i+1] = x[i] - (int) ((Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5)));
So your code should be:
public int[] x;
public void Square() {
x = new int[10];
x[0] = 7;
}
public void root() {
if (x == null)
Square();
for(int i = 0; i < x.length - 1; i++) {
x[i+1] = x[i] - (int) ((Math.pow(x[i] - 2.5, 2)) / (2 * (x[i] - 2.5)));
System.out.println(x[i+1]);
}
}
Inside the loop you are accessing the i + 1
item so the counter of the loop must take values up to x.length - 2
, this is why I have in the code: i < x.length - 1
.
I have removed the declaration of y
from Square()
as it is not used.
public class Square
{
public double x[];
public Square()
{
this.x = new double[10];
x[0]=7;
}
public void root()
{
System.out.println(x[0]);
for(int i=1; i<10; i++)
{
x[i]=x[i-1]-(Math.pow(x[i-1]-2.5,2))/(2*(x[i-1]-2.5));
System.out.println(x[i]);
}
}
}
Try use this code
public class Square
{
public double x[];
public void root()
{
x = new double[10];
x[0]=7;
for(int i=1; i<10; i++)
{
x[i]=x[i-1]-(Math.pow(x[i-1]-2.5,2))/(2*(x[i-1]-2.5));
System.out.println(x[i]);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.