views:

361

answers:

5

I have the following assignment for homework.

Requirements

  • design a class called TokenGiver with the following elements:
    • a default constructor, a parametrized constructor that takes an int
    • a method that adds a specified number of tokens to the number of tokens
    • a method that subtracts exactly ONE token from your number of tokens
    • a method that returns the number of tokens in your object

Other Requirements:

  1. create a TokenGiver object
  2. store 10 tokens in it
  3. ask the TokenGiver object how many tokens it has and display the result
  4. take 2 tokens out of the TokenGiver object
  5. ask the TokenGiver object how many tokens it has and display the result

Question

Is there a better way to subtract two tokens at once from my Main() method, or is calling the GetToken() method twice the only way?

Code Snippet:

using System;

class Program
{
const int NUM_TOKENS = 10;

    static void Main()
    {

        TokenGiver tokenMachine = new TokenGiver(NUM_TOKENS);
        Console.WriteLine("Current number of tokens = {0}", 
                tokenMachine.CountTokens());
        tokenMachine.GetToken();
        tokenMachine.GetToken();
        Console.WriteLine("New number of tokens = {0}", 
                tokenMachine.CountTokens());

        Console.ReadLine();
    }

}

class TokenGiver
{
    private int numTokens;

    public TokenGiver()
    {
        numTokens = 0;
    }

    public TokenGiver(int t)
    {
        numTokens = t;
    }

    public void AddTokens(int t)
    {
        numTokens += t;
    }

    public void GetToken()
    {
        numTokens--;
    }

    public int CountTokens()
    {
        return numTokens;
    }
}
+1  A: 

Well, whether or not there is a better way to extract two tokens than by calling GetToken twice seems irrelevant because one of your requirements is:

(the class shall have) a method that subtracts exactly ONE token from your number of tokens

So, it seems you are stuck with two calls. Since this is a highly contrived assignment you may as well just stick to the requirements. If you really want to learn something start your own personal project. :)


Also, as an aside, you can chain constructors in C#. So this:

public TokenGiver()
{
    numTokens = 0;
}

public TokenGiver(int t)
{
    numTokens = t;
}

...becomes...

public TokenGiver() : this(0) { }

public TokenGiver(int t)
{
    numTokens = t;
}
Ed Swangren
A: 

Given the requirements, you have to call GetToken twice... But of course it would be possible to create an overload for this method that would take the number of tokens to subtract as a parameter.

As a side note : GetToken is a poorly choosed name... usually a method whose name starts with "Get" is expected to return something. You could call it "TakeToken" instead, or something similar

Thomas Levesque
+2  A: 

There is a better way, as Ed said. But with your assignment saying that you need a method to subtract exactly 1 Token, you are doing it how you should.

public void GetToken(int t)
{
    numTokens -= t;
}

then you would could call GetToken(2);

jsmith
Ok, I think I'll ask my professor to clarify the requirements, because I would much rather do it this way.
Alex
I'm also guessing he is teaching OOP basics.. Meaning that in an Object Oriented world, this Object can only dispense Exactly ONE token at a time. Meaning if you were to build an object with those state attributes. Then you would not create a method that could dispense more than one Token at a time. Since that machine cannot do so.
jsmith
ok, that makes sense, thanks for your answer!
Alex
A: 

His requirements say to create and THEN add the 10 tokens. You call the constructor with a 10 -- call the constructor with void and then add the ten. I believe this was the assignment.

Hogan
A: 

To simply answer your question, what's wrong with tokenMachine.AddTokens(-2)? Given the requirements, it doesn't seem to be out of the question. The .NET framework also commonly uses this construction as well (e.g. DateTime.AddDays() takes a negative number to subtract).

However, a bad instructor may mark you off for this, using the argument that "[he intended to specify that] the machine can only dispense one token at a time", so it may be best to clarify the specifications.

lc