This site requires JavaScript, please enable it in your browser!
Greenfoot back
mntng
mntng wrote ...

2014/10/19

need help with loop

mntng mntng

2014/10/19

#
no compilation error, but the world cant't be constructed and it tells me that an infinite loop may exist. public class ChessWorld extends GWorld { static { // Initialize the world GWorld.setWidth(8); GWorld.setHeight(4); GWorld.setCellSize(67); } /** * Constructor for objects of class TestWorld. * */ public ChessWorld() { initialize(); } /** * Initialize the world */ public void initialize() { initializeFirstLineByWhileLoop(); } /** * (Completed in the skeleton code) * Remove all the chess pieces */ private void removeAllChessPieces() { GWorld.removeObjectsFromWorld( GWorld.getAllObjects("Chess") ); } public void initializeFirstLineByWhileLoop() { int width = GWorld.width(); int i = 0; while (i < width) { Chess newChess = new Chess(); GWorld.addOneObject(newChess, i, 0); i = i + 1; } } }
ILOVELABRADORS ILOVELABRADORS

2014/10/19

#
Perhaps this link will help?
Super_Hippo Super_Hippo

2014/10/19

#
What exactly do you want to do? I guess 'GWorld' is a subclass of world. And the 'ChessWorld' is a subclass of this 'GWorld'. Why do you change the width, height etc. of the GWorld in the ChessWorld? And add objects in another world? This doesn't really make sense to me. You probably also created new methods which should do the same as the normal ones, but are static, so you can just use them on the class. Why do you do that?
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
Super_Hippo wrote...
Why do you change the width, height etc. of the GWorld in the ChessWorld? And add objects in another world? This doesn't really make sense to me.
I agree, but also I think it's rather strange to use the GWorld.width() to set the value of width at initializeFirstLineByWhileLoop. Just to make sure nothing weird happens you should probably just give width a normal integer value. Allso seen you're making Gworld.setWidth() into a static that's probably gonna mean Gworld.getWidth is gonna return the same value every time right? So what use is it to make width into something flashy instead of a normal int if the value you set it to isn't going to change anyway?
Super_Hippo Super_Hippo

2014/10/19

#
'static' doesn't mean that it won't change. That would be 'final'. If the (static) method 'width' in 'GWorld' wouldn't return an int, it wouldn't work anyway.
for (int i=0; i<GWorld.width(); i++)
{
   Chess newChess = new Chess();
   GWorld.addOneObject(newChess, i, 0);
}
This would do the same as your 'initializeFirstLineByWhileLoop' method. But I still don't know what was your idea of using the worlds like this.
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
That's not what I was implying.. but let's just keep it with what you're saying then k? Nvm my part in that case guys. Guess I just don't get it...
mntng mntng

2014/10/19

#
This is my homework assigned by my teaching assistant. The world is set by them and I also have no clue about why they did this way. Anyway, my task is to place the chess pieces horizontally on the first line. So finally the code is exactly the same as above, and no compilation errors exist. But the world cannot be constructed. Why?
Super_Hippo Super_Hippo

2014/10/19

#
Actually I wonder why there isn't an error. Doesn't it always have to start with calling the constructor of the 'World' class? So starting with 'super(...)'?
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
Can't you just take the old code from school and then put them next to eachother? A bit of debugging that way will probably show what you changed and what goes wrong... right?
danpost danpost

2014/10/21

#
It looks like your ChessWorld class needs a constructor that calls the constructor of the GWorld class for it to construct. Probably something like this (however, I have not seen the GWorld class code to be sure):
public ChessWorld()
{
    super();
}
Also, the ChessWorld class should be the initial class for your project (right click on the ChessWorld class icon and select 'new ChessWorld()' -- this will make it the world initially created when reset/compiled).
danpost danpost

2014/10/21

#
Super_Hippo wrote...
Actually I wonder why there isn't an error. Doesn't it always have to start with calling the constructor of the 'World' class? So starting with 'super(...)'?
A class does not need a constructor, provided that an implicit 'super();' call will reach a no-argument constructor of the class it extends. An error will ensue only if the superclass does not have a no-argument constructor. I, for one, would like to view the code of the GWorld class that the ChessWorld class extends. I presume it has something like this:
public GWorld()
{
    super(width(), height(), cellsize());
}
danpost danpost

2014/10/21

#
Something does concern me, however, about the GWorld class. It seems to contain static methods for adding and removing objects from a world instance; but, 'static' infers that the member (whether a field or a method) belongs to the class, not to an object (or instance) created from that class. Making these methods static is basically a programming 'cheat' and should not be encouraged by instructors or teaching assistants.
davmac davmac

2014/10/21

#
mntng: if you comment out this line:
 initializeFirstLineByWhileLoop();
... does the world still fail to construct? (Basically, you need to trim the code down until you find out what part actually causes the problem. Then we can investigate further). It would help if we could see the code for GWorld (if you have it).
mntng mntng

2014/10/21

#
Actually, I just opened the file again, without changing anything, really. And... no problem exists. I don't know what happens, it just works again. But thank you guys, I really learn a lot from you.
You need to login to post a reply.