• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with constructor and fields

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THe constructor I wrote for the Stock method is this


Is that right?
 
Richard Goodwinn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Peter Brockway
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
edited: never mind, I figure out what was wrong with my takeTop code.


[ May 09, 2006: Message edited by: Richard Goodwinn ]
 
Richard Goodwinn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the get(int) method of java.util.List allows access to an element wothout removing it.
 
Richard Goodwinn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions as to why my top() and toString() methods don't pass the unit test?

Thanks
 
Peter Brockway
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I figured it out now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic