aspose file tools*
The moose likes Beginning Java and the fly likes Need help with constructor and fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with constructor and fields" Watch "Need help with constructor and fields" New topic
Author

Need help with constructor and fields

Richard Goodwin
Greenhorn

Joined: May 01, 2006
Posts: 6
Hi there,

I am a bit stumped on a problem.

I am currently working on a solitaire game code. I need to create a class called Stock (a non graphical version). I need to implement a constructor with only a single parameter plus a few methods called top, takeTop, numCardsRemaining and toString.

At the moment my Stock class is currently empty, as I cannot work out what the fields and constructor should be.

The Javadoc for the stock class is sort of helpful. It says...
Constructor summary
Stock(Deck deck)

To develop the Stock class you need to understand the Deck class, so here is the code for the deck class.
Thanks

Deck class code


So basically, I am wondering how do I do the constructor and fields for the Stock class.

Thanks

[ May 03, 2006: Message edited by: Richard Goodwin ]

Also: is this the appropriate forum for this question? Should I have posted it in the intermediate forum instead?
[ May 03, 2006: Message edited by: Richard Goodwin ]
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
It looks like Stock only needs a single field - the Deck that is
used to construct it. All the methods you mention could be
implemented using this field.

(The Deck documentation is a little strange as this class does
not model a full deck.)
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
I still can't get the Stock class to work. I can't even figure out how to get the public Card takeTop() method to work.

The javadoc says

public Card takeTop()
Take the top card from the stock.
Returns:
the card that was on top of the stock or null if the stock was empty. A call to this method reduces the number of cards remaining in the stock by one.

Im sort of lost! I suspect I will need to create an array for this method, but every one Ive tried has failed to compile. Bah I suck at Java!

Help?

Thanks
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
THe constructor I wrote for the Stock method is this


Is that right?
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Hmm so these are the methods I need to create for the Stock class.

public void addAtEnd(Card card)
add the given card to the end of the stock of the cards

public int numCardsRemaining()
the number of cards in the stock (including the top card)
Returns: the number of cards

public Card takeTop()
take the top card from the stock
returns: the card that was on top of the stock or null if the stock was empty. A call to this method reduces the number of cards remaining in the stock by one.

public Card top()
the top card of the stock
returns: the top card of the stock, or null if the stock is empty. A call to this method does not remove the card from the top but just returns a reference to it.

public String toString
String representation of a Stock object
returns: a string representing the Stock. THe string consists of the String representation of the top card of the Stock followed by a space and the number of other cards in parentheses with a '+' character in front of the number. For example DK (+17) is a Stock with the King of Diamonds at the top and 17 other cards (face down). If the Stock contains just one card - the top card - the same format is used, e.g., DK (+0). However, if the stock is empty, the string "empty" is returned instead.




That is the javadoc for the methods I need to implement in the Stock class. What I need is a nudge to get me started in the right direction. At first I thought I would need to create an array but since Im calling upon the Deck class I don't think I should need to create an array, right?
I do want to learn to code myself, but at the moment I cant even get my head around the simple methods outlined above.

Thanks for all the help so far
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

At first I thought I would need to create an array but since Im calling upon the Deck class I don't think I should need to create an array, right?


Correct.

It is actually pretty straightforward. There is a one to one mapping between the methods that you need to write, and the methods of the Deck class. The only exception is the top() method, which probably need two calls to the Deck class.

Henry
[ May 07, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
There is a one to one mapping between the methods that you need to write

To give you a "for instance":

[ May 07, 2006: Message edited by: Peter Brockway ]
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Thanks

I thought I had it for a minute there. As I could of added these methods to class Deck.....

public void add(int i,Card c)
{
cards.add(i,c);
}

public void remove(int i)
{
cards.remove(i);
}

public Card getCard(int i)
{
return cards.get(i);
}



....and then called them in the Stock class like this...


public void addAtEnd(Card card)
{
deck.add(0,card);
}

public Card top() {
if(deck.numCardsRemaining() == 0)
{
return null;
}
else {
return deck.getCard(numCardsRemaining()-1);
}
}

public Card takeTop()
{
Card c = top();
deck.remove(numCardsRemaining()-1);
return c;
}


But then I realised that the assignement instructions say that we are not allowed to modify the Deck class.

Hmm looks like I need to think down a different path.

[ May 07, 2006: Message edited by: Richard Goodwinn ]
[ May 07, 2006: Message edited by: Richard Goodwinn ]
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
But then I realised that the assignement instructions say that we are not allowed to modify the Deck class.

I haven't looked at your code in detail (to see if it does what you want it
to do - I guess you can do that) but you should note that you haven't modified the Deck class. What you have done is use the Deck class, which seems to be the whole point of the exercise.

I've just had a look in more detail and you seem to regard the top of the Stock as the bottom of the Deck (and vice versa). This is fine if you are
consistent about it, and it is what you mean to do. (But, in the spirit of this forum, a method like takeTop() would get you shot in some saloons...)
[ May 07, 2006: Message edited by: Peter Brockway ]
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
I've just had a closer look at what you posted: and yes you do alter the Deck class, but you don't have to.

Unless there is some pressing reason not to, imagine the Stock's cards are in exactly the same order as the deck that it contains. The Stock methods you want can mostly just call the similar methods in Deck (like the example I posted before.)

The only tricky one (as Henry pointed out) is top(), where you have to get the card from the Deck, put it back, and then return it to the caller.
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Oh ok thanks for that

I managed to get the addAtEnd method working fine, but am having a bit of trouble with the top and takeTop methods.

The takeTop method needs to return the card that was on top of the stock or null if the stock was empty. A call to the method also needs to reduce the number of cards remaining in the stock by one.

My attempts to make it work have been using an if statement. Is that the right way to be doing it? Because all my attempts have so far have either not compiled or just plain dont work.

Thanks
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
edited: never mind, I figure out what was wrong with my takeTop code.


[ May 09, 2006: Message edited by: Richard Goodwinn ]
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
From the javadoc...
public Card top()
the top card of the stock
returns: the top card of the stock, or null if the stock is empty. A call to this method does not remove the card from the top but just returns a reference to it.

How do I implement the top method so that it does not remove the top card but instead just return a reference to it?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
the get(int) method of java.util.List allows access to an element wothout removing it.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Also I am having a bit of trouble with the toString() method. It compiles but isnt passing the unit test.

The code I have for it is


The suitToString() and pipToString() methods in there are from the Card class.

Any suggestions as to why that code isnt passing the unit test?
THanks
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Any suggestions as to why my top() and toString() methods don't pass the unit test?

Thanks
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
How do I implement the top method so that it does not remove the top card but instead just return a reference to it?


Unfortunately the cards List of Deck is not accessible and you can't change the Deck class, so using the get(int) method seems to be ruled out. What I suggested before was:
(1) card from the Deck - use nextCard()
(2) put it back - use addAtStart()
(3) return the card to the caller - use return

It might seem strange that you can both put the card back at the top of the Deck and return it to the caller of top(). But remember a variable declared as a Card (and commonly called a Card) has as its value a reference to a Card object. Real cards can only be in one place at a time, but there can be lots of references to the same Card all over the place.

I am having a bit of trouble with the toString() method. It compiles but isnt passing the unit test.
How does it fail?

One problem I can see with your toString() method is that you call suitToString() etc even if c is null. This is not good. You should test if(c==null) straight after you call top(), and, if it is null, return "Empty" straight away.
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Unfortunately the cards List of Deck is not accessible and you can't change the Deck class, so using the get(int) method seems to be ruled out. What I suggested before was:
(1) card from the Deck - use nextCard()
(2) put it back - use addAtStart()
(3) return the card to the caller - use return


Thanks for that. I've been busy for the past few days so havent had time to work on my project.
Ive been trying to add the addAtStart() method to my code but havent been successful. This is the approach I am using...



That code doesnt actually compile, but Ive tried alot of different variations of that theme. Am I actually correct in using an if statement then adding the deck.addAtStart() method after the if statement?
I was trying to think of another approach I could use which uses both the nextCard() and addAtStart() methods but so far that is all I can come up with.

Maybe its just too early in the morning for my brain to be functioning
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
In your code:

public Card top()
{
if(deck.numCardsRemaining() == 0)
{
return null;
}
else
{
return deck.nextCard();

}
deck.addAtStart(Card);
}
you are returning too soon. (The compiler would have told you this). Think about introducing a new variable:
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Thanks! I figured it out now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with constructor and fields