Manuel Diaz

Ranch Hand

Posts: 79

posted 12 years ago

Hi!. I have a problem using this code. I want to make centered squares by using this formula:

Fractal (x, y, r)

if r > 0

Fractal (x+r, y-r, r div 2)

Square (x, r, r)

Here is what I have so far:

-------------------------------------------------------------------

public static void Square (int x, int y, int r)

{

c.drawRect(x,y,2*r,2*r);

}

public static void Fractal (int x,int y,int r)

{

if (r > 0)

{

Fractal (x-r, y+r, r / 2);

Square (x,r,r);

}

}

-------------------------------------------------------------

I call this method by:

Fractal (x,y,r);

But, when I run the program the program draw the squares but not centered. I ask, HOW THIS CAN HAPPEN?. I don't know what is missing?. Maybe because Im not using the "div", when I try to type this:

"Fractal (x+r, y-r, r div 2)"

The program returns an error. I dont know how to call "div".

Fractal (x, y, r)

if r > 0

Fractal (x+r, y-r, r div 2)

Square (x, r, r)

Here is what I have so far:

-------------------------------------------------------------------

public static void Square (int x, int y, int r)

{

c.drawRect(x,y,2*r,2*r);

}

public static void Fractal (int x,int y,int r)

{

if (r > 0)

{

Fractal (x-r, y+r, r / 2);

Square (x,r,r);

}

}

-------------------------------------------------------------

I call this method by:

Fractal (x,y,r);

But, when I run the program the program draw the squares but not centered. I ask, HOW THIS CAN HAPPEN?. I don't know what is missing?. Maybe because Im not using the "div", when I try to type this:

"Fractal (x+r, y-r, r div 2)"

The program returns an error. I dont know how to call "div".

Note: I love programming.

Nick George

Ranch Hand

Posts: 815

Manuel Diaz

Ranch Hand

Posts: 79

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

In mathematical terms "div" usually means to perform integer division. Of course, Java does this automatically with the / operator when it's two operands are both integer types.

Layne

Originally posted by Nick George:

what is div supposed to do?

'/' is divide, (6/3=2)

'%' is modulus, (10%3=1)

what is 6 div 2?

In mathematical terms "div" usually means to perform integer division. Of course, Java does this automatically with the / operator when it's two operands are both integer types.

Layne

Layne Lund

Ranch Hand

Posts: 3061