FA/Purse

From ggc

Jump to: navigation, search

001 package FA;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 
007 /**
008  * Alternate Project: Problems from chapter 7.
009  @author Frank Anderson
010  @author Derrick Dixon    */
011 
012 /** The class Purse contains methods to:
013   * 1. add coins by name to an ArrayList.
014     2. Display the contents of a purse in the order coins are entered.
015     3. Display the contents of a purse in the reverse order of entry.
016     4. Transfers the contents of one purse to that of another.
017     5. Check to see if the collection of coins in a purse are identical
018        in name and order.
019     6. Check to see if the contents of the coins in a purse are identical, 
020        regardless of the order of entry into the purse. */
021 public class Purse
022 {
023 
024   private ArrayList<String> purseContents;
025   public Purse()
026   {
027     purseContents= new ArrayList<String>();
028   }
029 
030 
031   /** Problem 7.2  works*/
032   /** addCoin adds the name of a coin to the ArrayList, purseContents. */
033   public void addCoin(String coinName)
034   {
035     purseContents.add(coinName);
036   }
037 
038   /** toString takes the elements of the ArrayList purseContents and displays
039     * them within bracket as a list separated by commas. The list is displayed
040     * in the same order as the coins were added to the ArrayList. */
041   /** @return the list of elements in order of entry.  */
042   public String toString()
043   {
044 
045     String output="";
046     if(purseContents.size()==0)
047       return "[]";
048     else if(purseContents.size()==1)
049       return "["+purseContents.get(0)+"]";
050     else
051     {
052       output="[";
053       for(int = 0;i<purseContents.size()-1; i++)
054         output = output + purseContents.get(i)+", ";
055       output = output + purseContents.get(purseContents.size()-1)+"]";
056       return output;
057     }
058   }
059 
060   /** Problem 7.3 works  */
061   /**  The method reverse() returns a string of the contents of the ArrayList
062     *  purseContents in the opposite order in which they were entered. The list
063     *  is enclosed in brackets and each element is separated by a comma. */
064   /** @ return the list of elements presented in reverse order. */
065   public String reverse()
066   {
067     String output="";
068     output ="[";
069     for(int = purseContents.size()-1;i>=1; i--)
070       output = output + purseContents.get(i)+", ";
071     output = output + purseContents.get(0)+"]";
072     return output;
073   }
074 
075   /** Problem 7.4 */
076   /** Takes the contents of one purse and adds it to another;
077       empties the content of the purse whose contents were transferred.   */
078   public void transfer(Purse other)
079   {
080     purseContents.addAll(other.purseContents);
081     other.purseContents.clear();
082   }
083 
084 
085   /** Problem 7.5 */
086   /**  sameContents is a method that determins whether two purses have
087     *  the same contents, meaning that they have the same coins in the 
088     *  same order. */
089   /** @return  true or false depending if order is the same.  */
090   public boolean sameContents(Purse other)
091 
092   {
093     if(purseContents.size()!=other.purseContents.size())
094       return false;
095     for(int i=0;i<other.purseContents.size();i++)
096     {
097       if(purseContents.get(i)!= other.purseContents.get(i))
098         return false;
099     }
100     return true;
101   }
102   /** Problem 7.6 */
103   /**  sameCoins is a method that determines whether two purses have
104     *  the same coins, regardless of there order. */
105   /** @return  true or false depending if coins in the purse are the same.  */
106   public boolean sameCoins(Purse other)
107   {
108     int count1Penny = 0;
109     int count2Penny = 0;
110     int count1Nickel = 0;
111     int count2Nickel = 0;
112     int count1Dime = 0;
113     int count2Dime = 0;
114     int count1Quarter = 0;
115     int count2Quarter = 0;
116 
117     if(purseContents.size()!=other.purseContents.size())
118     {
119       return false;
120     }
121     for(int i=0;i<other.purseContents.size();i++)
122     {
123       if(purseContents.get(i).equals("penny"))
124       {
125         count1Penny++;
126       }
127       if(other.purseContents.get(i).equals("penny"))
128       {
129         count2Penny++;
130       }
131       if(purseContents.get(i).equals("nickel"))
132       {
133         count1Nickel++;
134       }
135       if(other.purseContents.get(i).equals("nickel"))
136       {
137         count2Nickel++;
138       }
139       if(purseContents.get(i).equals("dime"))
140       {
141         count1Dime++;
142       }
143       if(other.purseContents.get(i).equals("dime"))
144       {
145         count2Dime++;
146       }
147       if(purseContents.get(i).equals("quarter"))
148       {
149         count1Quarter++;
150       }
151       if(other.purseContents.get(i).equals("quarter"))
152       {
153         count2Quarter++;
154       }
155     }
156     if((count1Penny==count2Penny)  && (count1Nickel==count2Nickel&& (count1Dime==count2Dime&& (count1Quarter==count2Quarter) )
157     {
158       return true;
159     }
160     else
161       return false;
162   }
163 
164 }


Download/View FA/Purse.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter