Some body out there PLEASE PLEASE tell me why this aint working
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Abarahm_Katz
Posted On:   Friday, April 26, 2002 05:09 AM

This is a program that simulates a Tower of cans(coke etc). It uses a Stack (linked list implementation... that works fine)and the code below calls the public methods to push or pop and remove all cans from a tower. array of stacks are initialized. You start from top of tower and find a level that isnt empty (if tower is empty you push a can on bottom level). Another can is pushed on bottom level (now so far 2 cans) then the next can is pushed on top of the 2 cans and so on. A can on top always spans two cans. The tower grows from bottom left upwards. Eg: after inserting 3 cans: -3 1 2 and after 6 cans: --6 -4 5 1 2 3 etc. cans are inser   More>>


This is a program that simulates a Tower of cans(coke etc). It uses a Stack (linked list implementation... that works fine)and the code below calls the public methods to push or pop and remove all cans from a tower.

array of stacks are initialized. You start from top of tower and find a level that isnt empty (if tower is empty you push a can on bottom level). Another can is pushed on bottom level (now so far 2 cans) then the next can is pushed on top of the 2 cans and so on. A can on top always spans two cans.

The tower grows from bottom left upwards. Eg:
after inserting 3 cans:

-3

1 2

and after 6 cans:

--6

-4 5

1 2 3 etc.

cans are inserted according to x and y coordinates.
My code below simulates some of the behaviour. But it builds the tower wrongly. Please can some body out there see what my code is doing wrong? PLEASE



import javax.swing.*;


public class TowerOfCans implements TowersOfCansInterface


{

private myStack[] myStackArray;

public TowerOfCans()


{


myStackArray = new myStack[11];

for(int i = 1; i < myStackArray.length; i++)

{

myStackArray[i] = new myStack();

}

}


public coord addCan(int can) // uses class coord

{

coord coordinates= new coord();

int j = myStackArray.length - 1;

boolean found = false;

while( j > 1 && !found)

{

if(myStackArray[j].size() == 0)

{

j--;

}

else

{

found = true;

}

}


if(myStackArray[j].size() - myStackArray[j+1].size() == 2) // can on top of 2 cans

{

myStackArray[j+1].push(new Integer(can));

coordinates.x = j+1; // .x = level number

coordinates.y = myStackArray[j+1].size(); // .y = stack size

}

else

{

myStackArray[j].push(new Integer(can));

coordinates.x = j;

coordinates.y = myStackArray[j].size();

}

return coordinates;

}


private void checkLimit()

{

if(myStackArray[1].size() == 10)

{

JOptionPane.showMessageDialog(null,
"55 cans limit reached for safe handling",
"Information",
JOptionPane.INFORMATION_MESSAGE);

}

}


public coord removeCan()

{

coord coordinates= new coord();

int j = myStackArray.length - 1;

boolean found = false;

while( j > 1 && !found)

{

if(myStackArray[j].size() != 0)

{

found = true;

}

else

{

j--;

}

}

if(myStackArray[j].size() - myStackArray[j-1].size() < 0) // can on top of 2 cans

{

myStackArray[j].pop();

coordinates.x = j;

coordinates.y = myStackArray[j-1].size();

}


return coordinates;

}


public void clearTower()

{
myStackArray = new myStack[11];

for(int i = 1; i < myStackArray.length; i++)

{

myStackArray[i] = new myStack();

}

}

}

MANY THANKX

   <<Less
About | Sitemap | Contact