I took my CS-1 course in C++ and I'm now at a new university and they use Java here. I've been learning the new syntax and doing my old C++ labs and I've hit a wall with a pass-by-reference one.
My understanding so far is Java is pass-by-value but how can I achieve the goal of this problem?
Write a program that tells what coins to give out for any amount of change from 1 cent to 497 cents. Since we wouldn’t use pennies, we need to round any cents to near 5 or 10’s. For example, if the amount is 368 cents, the rounded number is 370 cents and if the amount is 367 cents, the rounded number is 365 cents. The change would be 1 toonie (two dollar coin), 1 loonie, 2 quarters, 2 dimes for 370 cents. Use coin denominations of 2 dollars (toonie), 1 dollar (loonie), 25 cents (quarters), 10 cents (dimes), and 5 cents (nickels). Your program will use the following function:
Void computeCoin(int coinValue, int &number, int &amountLeft);
Note that this function needs to return two values so you must use reference variables.
For example, suppose the value of the variable amountLeft is 370 cents. Then, after the following call, the value of number will be 1 and the value of amountLeft will be 170 (because if you take 1 toonie from 370 cents, that leaves 170 cents):
computeCoin(200, number, amountLeft);
Print the value of number with the coin name before you make the following call:
computeCoin(100, number, amountLeft);
and so on. Include a loop that lets the user repeat this computation for new input values until the user enter the sentinel value to stop the program.
In C++ I could just use reference variables and the value of the coin could be changed. So far what I've learned is that objects is a way to use references, I don't know how to really go about this or if there is a better way. I've included the C++ code here:
#include <iostream>
using namespace std;
void computeCoin(int coinValue, int & number, int & amountLeft);
int main()
{
int number, change, amountLeft, remainder;
do
{
cout << "Enter the change (-1 to end): ";
cin >> change;
while (change>497)
{
cout << "The change should be less than 497. Try again.\n\n";
cout << "Enter the change (-1 to end): ";
cin >> change;
}
if (change != -1)
{
// round to near 5 or 10's
remainder = change % 5;
if (remainder <= 2)
amountLeft = change - remainder;
else
amountLeft = change + 5 - remainder;
if (amountLeft == 0)
cout << "No change.";
else
{
cout << amountLeft << " cents can be given as\n";
// compute the number of toonies
computeCoin(200, number, amountLeft);
if (number>0)
cout << number << " toonie(s) ";
// compute the number of loonies
computeCoin(100, number, amountLeft);
if (number>0)
cout << number << " loonie(s) ";
// compute the number of quarters
computeCoin(25, number, amountLeft);
if (number>0)
cout << number << " quarter(s) ";
// compute the number of dimes
computeCoin(10, number, amountLeft);
if (number>0)
cout << number << " dime(s) ";
// compute the number of nickels
computeCoin(5, number, amountLeft);
if (number>0)
cout << number << " nickel(s) ";
}
cout << endl << endl;
}
} while (change != -1);
cout << "\nGoodbye\n";
return 0;
}
void computeCoin(int coinValue, int &number, int &amountLeft)
{
number = amountLeft / coinValue;
amountLeft %= coinValue;
}