Use C# slove the problem Instructions Rename your class name from Program.cs to
ID: 3595889 • Letter: U
Question
Use C# slove the problem
Instructions
Rename your class name from Program.cs to SpendingCalculator.cs
Implement the following methods.
1. Method name: CalculatePerDiem Purpose: This method calculates how many dollars one can spend per day for a given total number of dollars and a given number of days. It returns the per diem value as its formal return value. It stores the dollars left in the parameter passed to it by reference. Parameters: 3 integers, with the following characteristics:
dollars, required parameter change, required parameter, passed by reference days, optional parameter, default value of 5
Return Type: int
In the body of the method:
a) Divide dollars by days using the remainder operator. Assign the resulting value to change.
b) Divide dollars by days (integer division). Return the resulting value using the return keyword.
2. Complete the Main method:
Even though the main method runs first, you should write it last, so the methods it calls are already written.
In the body of the Main method:
a) Put in WriteLine showing a statement that you are going to pass a value type by reference
b) Declare totalDollars, leftoverChange, perDiem, numberOfDays all as integers. Initialize totalDollars to 500 and all other variables to zero.
c) Put in WriteLine statements to make the Console look similar to the sample output i.e. welcome statement, message saying they have a hundred dollars, etc.
d) Call CalculatePerDiem. Pass to it only the variables for total dollars and change. The variable for change will need to be passed by reference. The “number of days parameter” is optional – do not pass an argument for “number of days”. Store the return value in perDiem.
e) Tell the user how much the per diem is for 5 days and how much money will be left over. You can hardcode the number 5 here since that is not available in a variable!
f) Ask the user for the number of days. Store it in the appropriate variable you have declared.
g) Call CalculatePerDiem again. This time, pass it all the appropriate arguments including the “number of days” that was just specified by the user. Store the return value in perDiem.
h) Tell the user how much the per diem is for the specified number of days and how much money will be left over.
Make sure you noted how you are able to access value-type variables that are not declared within a given method if they are passed by reference to that method. Passing value- types by reference OR passing reference-types by value results in similar behavior (we will see this when we start passing “reference types” between methods).
Track the value of variables in your program
Place a breakpoint in main and use F11 to step through your application
o Note the flow of the program especially when a method call is made
Use the Locals window to see values of your local variables.
o Note which variables are accessible in each method and which variables go out of scope.
o Note how the return value of methods is processed.
Additional Learning Exercises
Investigating Scope Further
You will find that even though the CalculatePerDiem method did not have access to the perDiem variable that is local to the main method, it was able to put a value in that variable because of the way references work.
Go and verify the scope of perDiem. In the body of the CalculatePerDiem method, use a WriteLine to output perDiem. If it doesn’t work, comment it out. If it does work, put a comment in explaining why it worked. Convince yourself of the way scope works - that perDiem is truly inaccessible in that method. Then, try various ways to get access to variables you don’t have access to, by passing variables by reference.
Make a change to the Method
After your program is working, to get more practice, change the behavior of the method, so it accepts both the change as well as the per diem variables by reference. In this case, you will change the return type to either 1) void, and not return anything or 2) keep it as an int and return a zero. Alternatively, create an overloaded method that accepts the two variables by references
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpendingCalculatorApp
{
class SpendingCalculator
{
private static int CalculatePerDiem(int dollars, ref int change, int days = 5){
change = dollars % days;
return (dollars / days);
}
private static void ReadStudentsName(string [] stringArray)
{
for (int i = 0; i < stringArray.Length; i++)
{
Console.WriteLine("Enter a name to the position " + (i + 1) + ":");
stringArray[i] = Console.ReadLine();
}
}
static void Main(string[] args)
{
//Passing value type by reference
Console.WriteLine("**** Passing value type by reference **** ");
int totalDollars = 500;
int leftoverChange = 0;
int perDiem = 0;
int numberOfDays = 5;
Console.WriteLine("You have ${0}", totalDollars);
perDiem = SpendingCalculator.CalculatePerDiem(totalDollars, ref leftoverChange);
Console.WriteLine("With {0}, your per diem is {1} over {2} days with ${3} left. ", totalDollars, perDiem, numberOfDays, leftoverChange);
Console.WriteLine("How many days is your trip");
numberOfDays = Convert.ToInt32(Console.ReadLine());
perDiem = SpendingCalculator.CalculatePerDiem(totalDollars, ref leftoverChange, numberOfDays);
Console.WriteLine("With {0}, your per diem is {1} over {2} days with ${3} left. ", totalDollars, perDiem, numberOfDays, leftoverChange);
//Passing value type by reference
Console.WriteLine("**** Passing reference type by value **** ");
string[] nameArray = new string[5];
SpendingCalculator.ReadStudentsName(nameArray);
Console.WriteLine(" The contents of nameArray are:");
for (int i = 0; i < nameArray.Length; i++)
{
Console.WriteLine(nameArray[i]);
}
Console.ReadLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.