-3

So my teacher assigned me a problem that I have to put the output of an array and pipe(not sure if that's the right word) the array values into the arraylist and then reverse the order. I know I'm very close and just missing something quite obvious. Every time I ask the teacher he just confuses me even more. Thank you for your help. I don't know what I would do without this site. My teacher said we need to have two separate classes. Purse.Java and TestPurse.Java. The array is in the TestPurse class and the arraylist is in the Purse class. Here is my testpurse.java code:

 public class Purse   {
    // TODO Auto-generated constructor stub

    /**
      * @param args
    */
    public static void main(String[] args) {
       Purse coin = new Purse();

       coin.addcoin ("Quarter");
       coin.addcoin ("Dime");
       coin.addcoin ("Nickel");
       coin.addcoin ("Dime");
       System.out.println (coin);
    }

    public void addcoin(String string) {
    // TODO Auto-generated method stub
       ArrayList<String> newpurse = new ArrayList<String>();

      Collections.reverse(newpurse);
      System.out.println (newpurse);
    }

}
Derek Pike
  • 41
  • 1
  • 2
  • 10

3 Answers3

1

Well, here's how you create an ArrayList from an array:

Create ArrayList from array

And this is how you reverse the order of items in an ArrayList:

Sort List in reverse order

Community
  • 1
  • 1
dnault
  • 8,340
  • 1
  • 34
  • 53
  • How do I pull the values for the array if the array is in a different class? I have the reverse working correctly. – Derek Pike Sep 11 '13 at 20:12
  • Purse coin = new Purse(); coin.addcoin ("Quarter"); coin.addcoin ("Dime"); coin.addcoin ("Nickel"); coin.addcoin ("Dime"); System.out.println (coin); In another class: – Derek Pike Sep 11 '13 at 20:14
  • new ArrayList(Arrays.asList(Purse)); Collections.reverse(Purse); System.out.println (Purse); – Derek Pike Sep 11 '13 at 20:14
1

Probably this is what you want

  public class PurseTest {

         public static void main(String[] args) {
             String[] coin = { "Quarter", "Dime", "Nickel", "Dime" };
             Purse purse = new Purse();
             purse.addAllCoin(coin);

            System.out.println(purse);
         }

   }

   public class Purse {
            ArrayList<String> data;

            public Purse() {
              data = new ArrayList<String>();

            }

           public void addAllCoin(String[] strArr) {

              for (String s : strArr) {
                  data.add(s);
              }

            }

           @Override
           public String toString() {
               Collections.reverse(data);
               return data.toString();
           }

   }

Can you please try this

user2720864
  • 8,015
  • 5
  • 48
  • 60
  • That worked thank you very much. Thank you @Prabhorakar for all of your input as well. I couldn't have done it without either one of you. VERY GRATEFUL – Derek Pike Sep 11 '13 at 20:34
  • Sorry look at the comment before this @Prabhaker – Derek Pike Sep 11 '13 at 20:36
  • See if this is what you, missed your comment that time – user2720864 Sep 11 '13 at 20:43
  • 2
    @user2720864 - I hope you learnt as much from doing Derek's homework for him, as he would have done if he had done it himself. – Dawood ibn Kareem Sep 11 '13 at 20:45
  • @DavidWallace I definitely tried doing it myself. I couldn't get past the arraylist though. I am just very thankful that this community will take the time to help out someone as ignorant about programming as me. – Derek Pike Sep 11 '13 at 20:55
0

Updated code

import java.util.ArrayList;
import java.util.Collections;

class Purse   
{
    ArrayList<String> newPurse= new ArrayList<String>();   

    public void addcoin(String string) 
    {           
       newPurse.add(string);          
    }
    @Override
    public String toString()
    {
        return newPurse.toString();
    }
    public String reverse()
    {
       ArrayList<String> al = new ArrayList<String>(newPurse);//making copy of newPurse so that reversing won't affect on it directly.
       Collections.reverse(al);
       return al.toString();
    }

}
public class TestPurse
{
    public static void main(String[] args) 
    {
       Purse coin = new Purse();    
       coin.addcoin ("Quarter");
       coin.addcoin ("Dime");
       coin.addcoin ("Nickel");
       coin.addcoin ("Dime");

       System.out.println (coin.toString());
       System.out.println (coin.reverse());
    }
}
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • this is very similar to what I had originally, but the teacher insists we have a purse class and a testpurse class. That is where all of my confusion is coming from. – Derek Pike Sep 11 '13 at 20:19
  • I changed my purse.java class to reflect the following, but I don't get any output. public void addcoin(String string) { // TODO Auto-generated method stub //new ArrayList(Arrays.asList(Purse)); ArrayList newPurse= new ArrayList(); Collections.reverse(newPurse); System.out.println (newPurse); – Derek Pike Sep 11 '13 at 20:21
  • @DerekPike be specific about your Question otherwise you won't get correct answer.Do one thing Just Copy and paste the question your teacher asked. – Prabhaker A Sep 11 '13 at 20:24
  • I'm sorry. I tried to include it on the original post, but it complained about that as well. Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList. • Supply a method void addCoin(String coinName). • Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]. • Write a method reverse that reverses the sequence of coins in a purse. – Derek Pike Sep 11 '13 at 20:26
  • • Implement a TestPurse class with a main method in it. It will use the toString method to test your code. For example, if reverse is called with a purse Purse[Quarter,Dime,Nickel,Dime], then the purse is changed to Purse[Dime,Nickel,Dime,Quarter]. – Derek Pike Sep 11 '13 at 20:26
  • @DerekPike I have updated the code.Please let me know if you have any doubts – Prabhaker A Sep 11 '13 at 20:38
  • What does the @Override mean? And why do you need the Public String? – Derek Pike Sep 11 '13 at 20:57