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 7781197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:09:00+00:00 2026-06-01T19:09:00+00:00

I feel like I am getting close using the debugger but am still not

  • 0

I feel like I am getting close using the debugger but am still not able to figure this one out.

I am walking through the following code

namespace Taxes
{

public class Rates
{


    //A class constructor that assigns default values 
    public Rates()
    {
        incLimit = 30000;
        lowTaxRate = .15;
        highTaxRate = .28;
    }
    //A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
    public Rates(int lim, double low, double high)
    {
        incLimit = lim;
        lowTaxRate = low;
        highTaxRate = high;
    }
    //  A CalculateTax method that takes an income parameter and computes the tax as follows:
    public int CalculateTax(int income)
    {
        //determine if the income is above or below the limit and calculate the tax owed based on the correct rate
        int taxOwed;

        if (income < incLimit)
            taxOwed = Convert.ToInt32(income * lowTaxRate); 
        else 
            taxOwed = Convert.ToInt32(income * highTaxRate);

        return taxOwed;
    }


} 

// The Taxpayer class is a comparable class
public class Taxpayer : IComparable
{
    //Use get and set accessors.

    private int taxOwed;

    string SSN
    { set; get; }
    int grossIncome
    { set; get; }
    int TaxOwed {
        get
        {
            return taxOwed;
        }
    }

    int IComparable.CompareTo(Object o)
    {
        int returnVal;
        Taxpayer temp = (Taxpayer)o;
        if (this.taxOwed > temp.taxOwed)
            returnVal = 1;
        else if (this.taxOwed < temp.taxOwed)
            returnVal = -1;
        else returnVal = 0;

        return returnVal;

    }  

    public static Rates GetRates()
    {
        //  Local method data members for income limit, low rate and high rate.
        int incLimit;
        double lowRate;
        double highRate;
        string userInput;
        //Rates myRates = new Rates(incLimit, lowRate, highRate);
        //Rates rates = new Rates();

        //  Prompt the user to enter a selection for either default settings or user input of settings.
        Console.Write("Would you like the default values (D) or would you like to enter the values (E)?:  ");

        // if they want the default values or enter their own
        userInput = (Console.ReadLine());
        if (userInput == "D" || userInput == "d")
        {
            Rates myRates = new Rates();
            return myRates;
            //Rates.Rates();
            //rates.CalculateTax(incLimit);
        }

        else if (userInput == "E" || userInput == "e")
        {
            Console.Write("Please enter the income limit: ");
            incLimit = Convert.ToInt32(Console.ReadLine());
            Console.Write("Please enter the low rate: ");
            lowRate = Convert.ToDouble(Console.ReadLine());
            Console.Write("Please enter the high rate: ");
            highRate = Convert.ToDouble(Console.ReadLine());


            Rates myRates = new Rates(incLimit, lowRate, highRate);
            return myRates;
            //rates.CalculateTax(incLimit);
        }
        else return null;
    }

    static void Main(string[] args)
    {

        Taxpayer[] taxArray = new Taxpayer[5];

        //Rates taxRates = new Rates();
        //  Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
        for (int x = 0; x < taxArray.Length; ++x)
        {
            taxArray[x] = new Taxpayer();
            Console.Write("Please enter the Social Security Number for taxpayer {0}:  ", x + 1);
            taxArray[x].SSN = Console.ReadLine();

            Console.Write("Please enter the gross income for taxpayer {0}:  ", x + 1);
            taxArray[x].grossIncome = Convert.ToInt32(Console.ReadLine());
            //taxArray[x].taxOwed = taxRates.CalculateTax(taxArray[x].grossIncome);

        }

        Rates myRate = Taxpayer.GetRates();
        //Taxpayer.GetRates();

        //  Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
        for (int i = 0; i < taxArray.Length; ++i)
        {
            Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, myRate.CalculateTax(taxArray[i].grossIncome));//taxArray[i].taxOwed);

        } 
        //  Implement a for-loop that will sort the five objects in order by the amount of tax owed 
        Array.Sort(taxArray);
        Console.WriteLine("Sorted by tax owed");
        for (int i = 0; i < taxArray.Length; ++i)
        {
            //double taxes = myTax.CalculateTax(taxArray[i].grossIncome);
            Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, myRate.CalculateTax(taxArray[i].grossIncome));

        }
    }  

} 

} 

I have everything solved except that the sort is not sorting by the tax owed amount for some reason now.

  • 1 1 Answer
  • 0 Views
  • 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-06-01T19:09:02+00:00Added an answer on June 1, 2026 at 7:09 pm

    An aside regarding Taxpayer.GetRates(): the taxpayer class shouldn’t be responsible for determining the tax rates. If taxpayers could determine tax rates, tax rates would most likely be zero. It might make more sense to move that into the Rates class.

    This answer to your question shows an example that should help you understand where you’re going wrong. If you would like a specific suggestion related to your code, please post a complete program that compiles. The sample code you’ve posted does not compile (error: “the name ‘taxRates’ does not exist in the current context”).

    To answer your question:

    How do I instantiate a class so I can use its methods without calling the default constructor?

    As others have noted, you need to retain a reference to the newly instantiated object, so you can use that instance when you read the values from the fields.

    Consider:

    void SetRates()
    {
        Rates rates = new Rates(10000, 0.3, 0.4);
    }
    
    void UseRates(Taxpayer taxpayer)
    {
        taxpayer.Tax = new Rates().CalculateTax(taxpayer.Income);
    }
    
    void Main()
    {
        SetRates();                              // creates an object and throws it away
        Taxpayer taxpayer = new Taxpayer(20000);
        UseRates(taxpayer);                      // creates a new object with default values
        Console.WriteLine(taxpayer.Tax);
    }
    

    Instead, return the object you created in SetRates() (and call it GetRates() instead). Then pass it into the UseRates method:

    Rates GetRates()
    {
        return new Rates(10000, 0.3, 0.4);
    }
    
    void UseRates(Taxpayer taxpayer, Rates rates)
    {
        taxpayer.Tax = rates.CalculateTax(taxpayer.Income);
    }
    
    void Main()
    {
        Rates rates = GetRates();
        Taxpayer taxpayer = new Taxpayer(20000);
        UseRates(taxpayer, rates);
        Console.WriteLine(taxpayer.Tax);
    }
    

    With regard to your edited code, you have

    Rates myTax = new Rates();
    Taxpayer.GetRates();
    

    Now Taxpayer.GetRates() assigns some values to a Rates instance, but it’s not the same Rates instance you created with the statement Rates myTax = new Rates(). The statement Rates myTax = new Rates() calls the default Rates constructor, and that’s the instance that you use later in the method for calculating the tax! This explains why your tax is always calculated with the default values.

    The GetRates method operates on a different instance of the Rates class. That different instance is created by one of the new Rates(... expressions in the body of the GetRates method. That instance has the rates you want to use, but it is basically trapped inside the method. Why is it trapped? Because you only assign the instance to a local variable, and local variables are not accessible outside the method in which they are declared.

    It’s a bit like this: instead of Rates, we have Car, and instead of GetRates we have FillFuelTank. Your code is then doing the following:

    Get a new car          //Rates myTax = new Rates();
    Go to the gas station  //Taxpayer.GetRates();
    

    Now, the GetRates() method … I mean, the FillFuelTank method … does this:

    Get a new car with fuel in it //Rates myRates = new Rates(incLimit, lowRate, highRate)
    

    You see what you’ve done? You’ve driven to the gas station in your new car, got a second car with fuel in it, and then you went back to the first car and drove away — without putting any fuel into it.

    One solution to that would be to pass myTax as an argument to the GetRates() method; a better solution would be to return a Rates instance from the GetRates() method.

    You wrote:

    My need for Rates myTax = new Rates(); in main is so that I can call the calculateTax method to do just that. If there is another way to call that method without calling the default constructor, I am all ears or fingers.

    The expression new Rates() calls the default constructor. So, the way to call calculateTax without calling the default constructor is to call the parameterized constructor instead:

    Rates rates = new Rates(limit, lowRate, highRate);
    double tax = rates.CalculateTax(taxpayer.Income);
    

    You might say, “but I did call the parameterized constructor in the GetRates method!” And there’s the problem, again, of filling the wrong car with fuel, because the Rates object in the GetRates method is a different object. You call the parameterized constructor on the object you don’t use, and you call the default constructor on the object you do use.

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

Sidebar

Related Questions

I feel like this might be really simple but I'm just not getting it
I feel like I'm trying to do something simple but I am not getting
I feel like this is something I should already know, but I'm just not
I feel like this is easy but I am missing something... Using jQuery, I
I feel like this should be a no brainer, but clearly I'm missing something...
I feel like this is a stupid question, but I can't think of a
I have been trying to figure this out for awhile now and I feel
I feel like this question has been asked many times, but the solution is
Quick question. feel like a noob but haven't found the right syntax for this
I feel like this will end in a facepalm moment but I've been banging

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.