Wallet class implements a wallet

Assignment Help Basic Computer Science
Reference no: EM131029588

Wallet Java Lab

Problem specification

Your new Wallet class implements a wallet that contains banknotes. A banknote is representedasanintforsimplicity,1fora$1bill,5fora$5bill,andsoon. Youare required to use just a simple array of int to hold the banknotes. You may NOT use an array list.

Here are some example wallets printed out:

Wallet[5, 50, 10, 5]
Wallet[]
Wallet[1, 5, 10, 50, 5]
  

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 a[])
  
    {
        // your code goes here
  

}

    public String toString()
  
    {
        // your code goes here
  

}

    public int value()
  
    {
        // your code goes here
  

}

public void reverse()
  
{
    // your code goes here
  

}

public void add(int banknote)
  
{
    // your code goes here
  

}

public void transfer(Wallet donor)
  
{
    // your code goes here
  

}

public void sort()
  
{
    // your code goes here
  

}

public boolean remove(int banknote)
  
{
    // your code goes here
  

}

public boolean sameBanknotesSameOrder(Wallet other)
  
    {
        // your code goes here
  
    }
    //EXTRA CREDIT methods follow...
  
    public Wallet removeBanknotePairs(Wallet w)
  
    {
        // your code goes here
  

} }

Instance variables
Use exactly and only these instance variables:

Wallet uses the contents[] array to store ints representing banknotes. count holds the number of banknotes in contents[], so must be updated every time a banknote is added or removed.

count is 0 when a wallet is empty. MAX is the maximum number of banknotes a wallet can hold, so count is MAX - 1 when a wallet is full. count will always be the index where the next banknote is to be added.

Banknotes in a wallet are maintained in the order of arrival. A new banknote is simply added to the end of contents[] and count is incremented by 1.

When a banknote is removed from a wallet, the first occurrence of the value is removed fromcontents[]andcountisdecrementedby1. Importantly,allbanknotesabovethe removed value must be moved 'down' so that no 'holes' open in the array. For example, if wallet w is Wallet[1, 5, 10, 50, 5]:

- then after w.remove(5), w will be Wallet[1, 10, 50, 5]:

Methods
You will complete the method bodies as follows:

public Wallet()
  

initialize the wallet to empty

? allocate memory for the contents[] array

? initialize count

public Wallet(int a[])
initialize the wallet using the array of int named a[] an overloaded constructor

? allocate memory for the contents[] array

? initialize contents[] from a[]

? initialize count

public String toString()
  

return a textual representation of the wallet, in the standard format. For example:

   Wallet[5, 50, 10, 5]
  

? use a StringBuffer to build the returned value

? convert the StringBuffer to a String and return the String

public int value()
  

calculate the value of the banknotes in the wallet. For example, if wallet is: Wallet[5, 50, 10, 5], value is 70

? must use count to do this, to traverse ONLY the banknotes in the wallet

? (cannot use contents.length since this traverses every element in the array

including elements that may not have been explicitly initialized, which is dangerous)

? return this number of dollars

public void reverse()
  

reverse the order of banknotes in a wallet e.g.
Wallet[1, 5, 10, 50, 5] when reversed becomes Wallet[5, 50, 10, 5, 1]

(IMPORTANT NOTE: do not create a new Wallet object!)

? suggested algorithm:

start an index pointing at the first banknote in contents[]
start another index pointing at the last of the banknotes in contents[] while these indexes do not meet or cross over

swap values at the indexes (use a temp variable to do this) move two indexes towards one another

public void add(int banknote)
add banknote to the wallet
? banknoteisthebanknotetoadde.g.50,5,etc

? add banknote to the end of contents[] and update count

public void transfer(Wallet donor)
transfer the contents of one wallet (the donor wallet) to the end of another, the receiver. (The receiver will be the Wallet object calling the method (i.e. the invoking object)). Leavethedonorwalletempty. Forexample:

if the receiver is Wallet[5, 10, 50, 50]
and donor is Wallet[5, 5, 10, 1, 50, 5]
then after the transfer:
receiver will be Wallet[5, 10, 50, 50, 5, 5, 10, 1, 50, 5] and donor will be Wallet[]

? should call the add() method as you implement this

? to set a wallet to empty, simply set its count to 0

public void sort()
  

sort the banknotes in a wallet into ascending order e.g.
Wallet[5, 50, 10, 5, 1] when sorted becomes Wallet[1, 5, 5, 10, 50]

(IMPORTANT NOTE: do not create a new Wallet object!)

? suggested algorithm:

? we saw in Week 7 a sort method that works for an array of Integer objects containing MAX elements

? take this method and modify syntax to work for contents[] and count

public boolean remove(int banknote) removefirstoccurrenceofbanknotefromthewallet. Returntrueifbanknotewas removed, false otherwise

? (this banknote may not be in the wallet)

? if banknote is removed, must update contents[] so that no holes appear, and count

public boolean sameBanknotesSameOrder(Wallet other)
  

return whether two wallets have exactly the same banknotes in exactly the same order Extra credit

public Wallet removeBanknotePairs(Wallet w)
  

create a new wallet and add to it pairs of banknotes removed from two other wallets. Return the new wallet. For example:

if w1 is Wallet[5, 1, 50, 20, 50, 5]

and w2 is Wallet[20, 10, 5, 5, 5, 50, 10] then after the method call
Wallet w3 = w1.removeBanknotePairs(w2);

w1 will be: Wallet[1, 50]
w2 will be: Wallet[10, 5, 10]
w3 will be: Wallet[5, 5, 50, 50, 20, 20, 5, 5]

The WalletTester class
TheWalletTesterclasstestsyournewWalletclass. SourcecodeforWalletTesteris given below, and can be downloaded from Blackboard, Course Documents, Week 10 folder, Example programs.

/**
 * Test the Wallet class.
 *
 * @author Anthony W. Smith
 * @version 5/31/2028
 */
  
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);
  
        // show the contents of myWallet
        System.out.println("myWallet contains: " +
  
                                        myWallet.toString());
  
        // print the value of myWallet
        System.out.println("nvalue of myWallet is: $" +
  
                                            myWallet.value());
  
        // reverse the order of banknotes in myWallet
        myWallet.reverse();
        System.out.println("nmyWallet reversed contains: " +
  
                                          myWallet.toString());
  
        // transfer all the banknotes from myWallet to yourWallet!
        Wallet yourWallet = new Wallet();
  
yourWallet.add(1);
yourWallet.transfer(myWallet);
System.out.println("nnow myWallet contains: " +
  
                                     myWallet.toString());
System.out.println("yourWallet contains: " +
  
                                    yourWallet.toString());
  
// sort yourWallet
yourWallet.sort();
System.out.println("nyourWallet sorted is: " +
  
                                   yourWallet.toString());
  
// remove all $5 banknotes from yourWallet
while (yourWallet.remove(5))
  
    ;
System.out.println("nyourWallet with $5s removed is: " +
  
                                    yourWallet.toString());
  
// check whether two wallets have the same banknotes
// in the same order
int b[] = {10, 5, 10};
Wallet tom = new Wallet(b);
  
int c[] = {10, 10, 5};
Wallet dick = new Wallet(c);
  
int d[] = {10, 5, 10};
Wallet harry = new Wallet(d);
  
System.out.println(
     "ntom 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));
  
// EXTRA CREDIT - compare two wallets and remove banknote pairs
int e[] = {5, 1, 50, 20, 50, 5};
Wallet w1 = new Wallet(e);
  
int f[] = {20, 10, 5, 5, 5, 50, 10};
Wallet w2 = new Wallet(f);
  
Wallet w3 = w1.removeBanknotePairs(w2);
System.out.println("nw1 is: " + w1.toString());
System.out.println("w2 is: " + w2.toString());
System.out.println("w3 is: " + w3.toString());}}  

Extra credit

When you have completed all other methods, implement and test removeBanknotePairs() for 20% extra credit. You must test your method using the w1 and w2 wallets given above in the description of removeBanknotePairs().

Reference no: EM131029588

Questions Cloud

What was ofra sherman predicament : What did Glaxo-Welcome do? What should they have done? Did Ofra Sherman do the right thing? What would you have done? What was Ofra Sherman's predicament? How did she get into it? How do you evaluate her actions as described in the case
Optimal inventory cost and number of orders. : The demand for an item is 8000 units per annum and the unit cost is Re.1/-. Inventory carrying charges of 20% of average inventory cost and ordering cost is Rs. 12.50 per order. Calculate optimal order quantity, optimal order time, optimal inventory ..
Example of operating system : Which of the following is not an example of Operating System? (a) Windows 98 (b) BSD Unix
What does the number 3.03 represent : Describe how you arrived at your answer by showing your work/setup with the slope formula.
Wallet class implements a wallet : Your new Wallet class implements a wallet that contains banknotes. A banknote is representedasanintforsimplicity,1fora$1bill,5fora$5bill,andsoon. Youare required to use just a simple array of int to hold the banknotes. You may NOT use an array list.
What is the probability of the given question : If the annual test results are independent, what is the probability that in any one year a mammogram will produce a false-positive result?
Determine average elevation of water level for each year : These data are shown in Table 8.9 . Use the data in the file to answer the following questions: Determine the average elevation of the water level for each year and for the 8-year period over which the data were collected.
Compare fixed versus floating exchange rate regimes : Compare and contrast fixed versus floating exchange rate regimes. Which do you view as preferable and why? Given that today we operate in a world dominated by a floating rate regime, what factors explain changes that arise over time?
Find the economic lot size : A company uses annually 24,000 units of raw material, which costs Rs. 1.25 per units. Placing each order cost Rs. 22.50, and the carrying cost is 5.4%of the average inventory. Find the economic lot size and the total inventory cost including material..

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Security of the facilities

Write 2 pages of the physical security policy section for Home Depot regarding their security incident. Include the follow: Security of the facilities: -Physical entry controls -Security offices, rooms, and facilities

  What are search methods

What are search methods? Do you feel search methods are relevant to topics in artificial intelligence ? If yes, please explain why and how. Specifically, describe how search methods would be used for rule-based systems, expert systems, resolu..

  Determine and print the average age of your family

Design a program that will allow a user to Input a list of your family members along with their age and state where they reside. Determine and print the average age of your family and print the names of anyone who lives in Oregon.

  Professional interactive media developers

DIGIMAX Multimedia is a multimedia content development company would like to release various educational CD/DVD in the market. In this regards, the company wants to approach professional interactive media developers

  State of six conditional flags after instructions executes

Forecast the state of six 8086 conditional flags after each of instructions which are given below executes. MOV AL,AH b. ADD BL,CL c. ADD CL,DH d. OR CX,BX

  Write a statement that changes the value of 6 in the array

Write a statement that changes the value of 6 in the array to a 12.

  Technologies dependent on the use of cryptography

How are biometric technologies dependent on the use of cryptography?

  What exactly active directory folders purposes

Active Directory folders (not shared folders) are unique objects in Active Directory.

  Transmitting station montiors bus during transmission

If each transmitting station montiors bus during transmission, how long before it notices the interference in seconds? In bit times?

  Research on professional communication

You will individually research a topic on some aspect of professional communication. You will then prepare a 1-2 page outline using the standard outline format (shown below) that would be used in a training session. At the end of your outline yo..

  Purpose of the boot loader during the boot-up process?

purpose of the boot loader during the boot-up process?

  Create a sequentially numbered array

Create a sequentially numbered array of 50 integers. Then use the Random_range procedure to shuffle the array in a random order.(Each number will only appear once in the array.) Display the shuffled array.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd