-1

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;
}
KenP
  • 77
  • 1
  • 9
  • Return a `class Result { int number; int amountLeft; }`. Java was designed with less errorproneness in mind. `f(x);` will never assign something to x. – Joop Eggen May 28 '16 at 17:04
  • 3
    Please read this *excellent* answer to your question. [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – paulsm4 May 28 '16 at 17:06
  • 1
    you can do something like Void computeCoin(int coinValue, Integer number[], Integer amountLeft[]); and use number[0] = ... but this is not a good practice generally. – nikolap May 28 '16 at 17:08
  • 1
    You have a function that operates on a state and modifies. One way to do that is to let the state be the state of a class type object, and the function a member function. – Cheers and hth. - Alf May 28 '16 at 17:08

1 Answers1

1

You can (and probably should) return an object as a result as Joop Eggen indicated in the comments.

An alternative that sometimes works (technically it would always work, but should not always be used) is to pass in a mutable object like this:

class MutableInteger
{
   private int value;

   public void setValue(final int val)
   {
      value = val;
   }

   public int getValue()
   {
      return value;
   }
}

then pass in an instance of that instead of the int parameter into your method.

Basically that is like a struct in C. Again, I would suggest returning a result in this specific case though.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163