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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.