Work with tokens

I have the following homework assignment.

Requirements

Create a class called TokenGiver with the following elements:

  • default constructor, a parameterized constructor that accepts int
  • method that adds the specified number of tokens to the number of tokens
  • a method that subtracts exactly ONE token from your number of tokens
  • method that returns the number of tokens in your object

Other requirements:

  • create a TokenGiver object
  • store 10 tokens in it
  • ask the TokenGiver object how many tokens it has and displays the result
  • take 2 tokens from the TokenGiver object
  • ask the TokenGiver object how many tokens it has and displays the result

Question

Is there a better way to subtract two tokens at once from my Main() method or GetToken() call GetToken() twice?

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; } } 
+4
source share
5 answers

There is a better way, as Ed said. But with your task saying that you need a method to subtract exactly 1 token, you do it right.

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

then you could call GetToken (2);

+2
source

Well, is there a better way to extract two tokens than by calling GetToken twice, it seems inappropriate, because one of your requirements:

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

So it seems you are stuck in two challenges. Since this is a very clever task, you can just stick to the requirements. If you really want to learn something, start your own personal project. :)


Also, as an aside, you can associate constructors with C #. So:

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

... becomes ...

 public TokenGiver() : this(0) { } public TokenGiver(int t) { numTokens = t; } 
+1
source

Given the requirements, you should call GetToken twice ... But, of course, you could create an overload for this method, as a result of which the number of tokens would be subtracted as a parameter.

As a side note: GetToken is a poorly chosen name ... usually a method whose name begins with "Get" is expected to return something. You could call it โ€œTakeToken,โ€ or something like that.

0
source

His requirements say create, and then add 10 tokens. You invoke the constructor with a 10-call constructor with void and then add ten. I believe this was a task.

0
source

To just answer your question, what happened to tokenMachine.AddTokens(-2) ? Given the requirements, this seems to be out of the question. The .NET framework also commonly uses this construct (e.g. DateTime.AddDays() takes a negative number for subtraction).

However, a bad instructor can mark you up for this, using the argument that "[he intended to indicate this], the machine can only give out one token at a time," so itโ€™s best to clarify the specifications.

0
source

All Articles