Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following class has a method with a side effect: /** A purse computes the to

ID: 3686419 • Letter: T

Question

The following class has a method with a side effect:

/**

A purse computes the total value of a collection of coins.

*/

public class Purse

{

private double total;

/**

Constructs an empty purse.

*/

public Purse()

{

total = 0;

}

/**

Adds a coin to the purse.

@param aCoin the coin to add

*/

public void add(Coin aCoin)

{

total = total + aCoin.getValue();

System.out.println("The total is now " + total);

}

/**

Gets the total value of the coins in the purse.

@return the sum of all coin values

*/

public double getTotal()

{

return total;

}

}

Describe the side effect and explain why it is not desirable.

How would you eliminate the side effect in the add method?

Explanation / Answer

Answer:

There is no method to assign coin value. The total will stay zero (0) forever.

In the code I found & quote, it is invalid keyword. Name cannot start with &. Print statement has error.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote