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

Anthony W. Smith, 2017 Purpose Purpose of this lab is for you to develop a progr

ID: 3829365 • Letter: A

Question


Anthony W. Smith, 2017 Purpose Purpose of this lab is for you to develop a program where many objects are created from a class. Primitives and objects are passed as parameters to class methods. Problem specification Your new wallet class implements a wallet that contains banknotes. A banknote is represented as an int for simplicity, 1 for a SI bill, 5 for a $5 bill, and so on. You are required to use a simple array ofint to hold the banknotes. You may NOT use an array list. Here are some example wallets printed out: Wallet (5, 50 10, 51 Wallet wallet (1, 5, 10, 50 51 Here's the outline of the wallet class. You will implement each method as described below. public class Wallet max possible of banknotes in a wallet private static final int MAX 10 private int contents private int count; number of banknotes stored in contents public Wallet your code goes here public wallet int all) your code goes here public String toString your code goes here public int value your code goes here public void add (int banknote)

Explanation / Answer


public class Wallet {
   private final int MAX = 10;
   private int contents[];
   private int count;
  
   // constructors
   public Wallet(){
       contents = new int[MAX];
       count = 0;
   }
  
   public Wallet(int a[]){
       contents = new int[MAX];
       for(int i=0; i < a.length; i++){
           contents[i] = a[i];
       }
       count = a.length;
   }
  
   public String toString(){
       StringBuilder sb = new StringBuilder("Wallet [");
       for(int i = 0; i < count; i++){
           sb.append(contents[i]);
           if(i < count - 1)
               sb.append(", ");
       }
       //sb.append(contents[count-1]);
       sb.append("]");
       return sb.toString();
   }
  
   // return sum of contents
   public int value(){
       int sum = 0;
       for(int i = 0; i < count; i++){
           sum += contents[i];
       }
      
       return sum;
   }
  
   // add banknotes at end
   public void add(int banknotes){
       contents[count++] = banknotes;
   }
  
   // transfer money from donor to reciever
   public void transfer(Wallet donor){
       for(int i = count, j = 0; i < donor.count + count; i++){
           contents[i] = donor.contents[j++];
          
       }
      
       for(int i = 0; i < donor.count; i++){
           donor.contents[i] = 0;
       }
       count += donor.count;
       donor.count = 0;
   }
  
   // remove banknote from contents
   public boolean remove(int banknote){
       boolean flag = false;
       int i;
       for(i = 0; i < count; i++){
           if(contents[i] == banknote){
               contents[i] = -1;
               flag = true;
               break;
           }
       }
      
       if(flag){
           count--;
           while(i < count){
               contents[i] = contents[i+1];
               i++;
           }
       }
      
       return flag;
   }
  
   public boolean sameBankNotesSameOrder(Wallet other){
       boolean flag = true;
       if(count != other.count){
           return false;
       }
      
       for(int i = 0; i < count; i++){
           if(contents[i] != other.contents[i]){
               flag = false;
               break;
           }
       }
       return flag;
   }
}

/*************************WalletTester*************************************/


public class WalletTester {
   public static void main(String args[]){
       // create a new wallet object using an array
       int a[] = {5, 50, 10, 5};
       Wallet myWallet = new Wallet(a);
       // print the value of wallet
       System.out.println("my wallet contains: " + myWallet.toString());
       System.out.println(" value of my wallet is: $" + myWallet.value());
      
       // transfer all banknotes from my wallet to your wallet
       Wallet yourWallet = new Wallet();
       yourWallet.add(1);
       yourWallet.transfer(myWallet);
       System.out.println(" now my wallet contains: " + myWallet.toString());
       System.out.println(" your wallet contains: " + yourWallet.toString());
      
       // remove all 5 dollar notes
       while(yourWallet.remove(5));
       System.out.println(" your wallet with $5s removed is: " + yourWallet.toString());
      
       int b[] = {10, 5, 10};
       Wallet tom = new Wallet(b);
      
       int c[] = {10, 5, 10, 1};
       Wallet _dick = new Wallet(c);
      
       int d[] = {10, 5, 10};
       Wallet harry = new Wallet(d);
      
       System.out.println(" tom has same banknotes in same order as _dick: " + tom.sameBankNotesSameOrder(_dick));
       System.out.println(" tom has same banknotes in same order as harry: " + tom.sameBankNotesSameOrder(harry));
   }  
}

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