Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The methods are defined as instance methods on the <code>Park</code> class type, so you need to call the method with a reference to an instance of <code>Park</code>. Additionally, each method takes 2 parameters, so you'd have to provide them at the time you call the methods:</p> <pre><code>Console.WriteLine("Annual cost per visitor: " + aPark.CalculateCostPerVisitor( ... /* actual parameters here */)); Console.WriteLine("Revenue: " + aPark.CalculateRevenue( ... /* actual parameters here */)); </code></pre> <p>However, since you've defined <code>annualBudget</code>, <code>noOfVisitors</code>, and <code>fee</code> as fields of your <code>Park</code> class, I think it's likely that you never really intended these values to passed in as parameters&mdash;or at the very least you're confused about whether these should really be parameters or if the class should calculate the result from the field values. </p> <p>I'd recommend you remove these parameters, and simply calculate the results from the field values:</p> <pre><code>public double CalculateCostPerVisitor() { //divide annual budget by number of visitors this.costPerVisitor = this.annualBudget / this.noVisitors; return this.costPerVisitor; } public double CalculateRevenue() { //No of Visitors times Fee this.revenue = this.noVisitors * this.fee; return this.revenue; } ... Console.WriteLine("Annual cost per visitor: " + aPark.CalculateCostPerVisitor()); Console.WriteLine("Revenue: " + aPark.CalculateRevenue()); </code></pre> <hr> <p>Not entirely relevant to the question, but there are a few other things wrong (or at least very <em>strange</em>) about your class:</p> <ol> <li><p><code>Park aPark = new Park();</code><br> You are creating new instance of <code>Park</code> inside every <code>Park</code>, this is bound to lead to a stack overflow error if you try to create a single instance. You should remove this line from your class file.</p></li> <li><p><code>name = Console.ReadLine();</code><br> You are reading from the console every time you try to get the value from a property. This is wrong on many levels. All of your properties should just get / set private values and try to do as little work as possible. If you want to allow the user to specify the name in the console, that should be done in your <code>Main</code> method like this:</p> <pre><code>aPark.Name = Console.ReadLine(); </code></pre></li> <li><p><code>fee = double.Parse(sFee);</code><br> <code>sFee = Console.ReadLine();</code><br> I'm not entirely sure what's going on here, but it's backwards. You need to read the input from the console first, then try to parse it. And again, it should be done in the <code>Main</code> method, look like this:</p> <pre><code>string sFee = Console.ReadLine(); aPark.Fee = double.Parse(sFee); </code></pre></li> <li><p>Once you've corrected the properties following the above steps, you can drop the private backing fields and dramatically simplify your code using automatic properties, like this:</p> <pre><code>public string Name { get; set; } </code></pre></li> <li><p>You should generally avoid public fields. If you keep <code>costPerVisitor</code> as a member of your class, you should probably make it a property, like this:</p> <pre><code>public double CostPerVisitor { get; set; } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload