Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 461563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:59:13+00:00 2026-05-12T22:59:13+00:00

I have the following assignment for homework. Requirements Design a class called TokenGiver with

  • 0

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 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:59:14+00:00Added an answer on May 12, 2026 at 10:59 pm

    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);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some ideas for a homework assignment that I have. Consider the following
I have a homework that asks me to do the following: Design a template
For a programming assignment, we have the following requirements: It needs to be a
I have the following code that I am working on for my college assignment.
For a homework assignment in linear algebra, I have solved the following equation using
I have an assignment for a class that is to be done in C#.
I have the following many-to-many relationship: class Assignment < ActiveRecord::Base belongs_to :programmer belongs_to :project
I'm working on a homework assignment for my object oriented design class, and I'm
I have the following assignment: Write a complete 8086 program to perform the calculator
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.