I have an assignment to reverse the sequence of coins in a purse. I cannot use any methods from the Collections or Arrays Class. I have the following code but have no idea what to do from here to get the purse contents to reverse.
import java.util.ArrayList;
public class Purse
{
public Purse()
{
coins = new ArrayList<String>();
}
public void addCoin(String coinName)
{
coins.add(coinName);
}
public String toString()
{
return coins.reverse();
}
public void reverse()
{
for (int j=0; j < coins.size(); j++)
{
}
}
private ArrayList<String> coins;
}
public class PurseTester
{
public static void main(String[] args)
{
Purse p = new Purse();
p.addCoin("Quarter");
p.addCoin("Dime");
p.addCoin("Nickel");
p.addCoin("Dime");
System.out.println("Original purse: " + p.toString());
System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");
p.reverse();
System.out.println("Reversed purse: " + p.toString());
System.out.println("Expected: Purse[Dime,Nickel,Dime,Quarter]");;
}
}