Click to See Complete Forum and Search --> : Need help on a JBuilder project


f1seb
10-05-2003, 12:09 AM
Hi i need some help, I'll probably need help on more then one method so I would appreciate if you guys stuck around and help me out till i get finished with it. Im programming in java using Jbuilder.

I need to write a toString method that returns a String with the contents of the Tray array. This is where im stuck on right now. PLEASE HELP!!!!

monkeykidney
10-05-2003, 08:50 AM
Is this Tray Object something you wrote yourself? Because toString methods are not, in general, that hard to write. PYCP!

f1seb
10-05-2003, 10:46 AM
Yes Tray is something that I'm writing myself here is what I have so far.


import tiles.Tile;
import tiles.TileBag;
//import select.Select;

public class Tray
{

private Tile [ ] tileArray;
private int SizeOfStack = 0;

/** adds a tile to the Tray */
public void addToTile(Tile insertTile)
{
tileArray[SizeOfStack] = insertTile;

}//end addToTile

/** returns the tile on the tray throws Index Exception */
public Tile returnTile(int index) throws Exception

{ //index too small
if (SizeOfStack < 0) throw new Exception("Cant index the tile with " +index);
//index too large
if (index >= SizeOfStack) throw new Exception("Cant use index: "+index+"on a tray of size: "+SizeOfStack);
return tileArray[index];
}//end returnTile

/** toString method that will return a String with the Tiles of the Tray */
public String toString()
{

}//end toString

/** length method will return the number of Tile in the Tray */
public int length()
{
return SizeOfStack;
}

}//end of Tray class

Basically this is what I have to do. these are my directions for the project:

Write a class called Tray
This class will have an array of Tiles variable
There will be a method in Tray to add a new Tile parameter to the Tray.
There will be a method that returns the Tile indexed by it's int parameter, it should throw and exception if index is incorrect.
There will be a toString method that returns a String with Tiles of the Tray
There will be a length method that returns the number of Tiles selected by the user.
There will be a select method that returns an array of Tiles selected by the user.
You will derive a WordPlayer class from project 1.
The WordPlayer class will have a Tray variable in it containing the Tray of Tiles of the user.
It will have a getTray method that returns the Tray.
It will have a setTray method that sets the Tray to it's parameter.
It will override the toString method to also have the Tray in the string.
It will have a main method that plays Sher Words 1
In Sher words 1 each WordPlayer gets a new tile from its tray from a TileBag.
If a WordPlayer has a word of at least 3 letters in his tray the WordPlayer can declare it.
The WordPlayer gets the points from the Tiles in the word. Each WordPlayer clears his Tray(no tiles in it) and a new Tile Bag is started.
If the WordPlayer doesn't have more then 100 points the game continues.

for extra credit check words against dictionary.
Im not up for extra credit so im just going to ask the other player if the declared word is a word or not. Here are the files he provided me with.

f1seb
10-05-2003, 10:47 AM
It should play somethign like scrabble but much easier this is only the 2nd programming class CMP211.

bwkaz
10-05-2003, 02:26 PM
Originally posted by f1seb
/** adds a tile to the Tray */
public void addToTile(Tile insertTile)
{
tileArray[SizeOfStack] = insertTile;

}//end addToTile For starters, if you want to keep the indentation, use and tags around your code. ;)

Another thing I noticed right off the bat -- what is going to happen if the user calls addToTile() twice, expecting to add two separate tiles to the tray? I know this isn't what you asked about, but that's not the point of this comment. :)

Also, your "index too small" comment doesn't really make much sense. It doesn't seem that you're even checking the index in that test...

As for the question you asked, I don't think it'd be a good idea to flat-out give you answers (your teacher may very well also be reading these forums, after all), but I do think that hinting at what kind of angle to use would be all right. So, here goes.

Most of the work for it has already been done for you for toString(). By this I mean the definition of what toString() should do -- normally you'd have to figure out what determines the "state" of the tray, and then figure a way to put it into a string. Here, you only have to do the second half, because you've been told that the state consists of which tiles are in the tray.

Each tile has a value (in Scrabble at least, the value would usually be the letter, and possibly also the point count), and I'm assuming it's easy to find that value with your Tile class. So all you'd need to do to create the appropriate string is to string together all the values of all the existing tiles, with spaces (or some other separator) between them.

f1seb
10-05-2003, 07:10 PM
Oh I didn't catch that one, thanks. But the toString is still giving me trouble and I'm running out of time. The textbook is crap and I keep getting errors each time I try something new.

bwkaz
10-06-2003, 07:24 PM
Hmm. Well, try something like this:

Create a String variable inside toString(). This will be the return value.

Append the value of each tile to that string variable, and then append a space.

Return the value.

If you want, you can pull the last space off the value before you return it.

If you're getting errors, then posting them might be helpful too. ;)