Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* * Represents a wallet with dollar bills form various denominations */ public class Wallet { private int ones; private int fives; private int tens;

/* * Represents a wallet with dollar bills form various denominations */ public class Wallet {

private int ones; private int fives; private int tens; private int twenties; private int hundreds;

public Wallet(int ones, int fives, int tens, int twenties, int hundreds) { super(); this.ones = ones; this.fives = fives; this.tens = tens; this.twenties = twenties; this.hundreds = hundreds; } /* * Creates a new wallet with exactly the same bills as the parameter wallet */ public Wallet(Wallet w) { this.ones = w.ones; this.fives = w.fives; this.tens = w.tens; this.twenties = w.twenties; this.hundreds = w.hundreds; } /* * Return true if and only if the parameter wallet contans exactly the same bills * as the target object. */ public boolean equals(Object o) { if (!(o instanceof Wallet)) { return false; } Wallet w = (Wallet) o; return ((w.ones == this.ones) && (w.fives == this.fives) && (w.tens == this.tens) && (w.twenties == this.twenties) && (w.hundreds == this.hundreds)); }

/* * Creates a new wallet with the minimal number of bills necessary to represent the * number of dollars received as a parameter */ public Wallet(int dollars) { // YOUR CODE HERE }

// Getters public int getOnes() { return ones; } public int getFives() { return fives; } public int getTens() { return tens; } public int getTwenties() { return twenties; } public int getHundreds() { return hundreds; } /* * Returns the amount of money in dollars contained in the wallet */ public int getAmount() { // YOUR CODE HERE return 0; // Dummy return. Please change and remove comment } /* * Modifies the target wallet to add the bills from the parameter wallet */ public Wallet add(Wallet w2) { // YOUR CODE HERE return null; // Dummy return. Please change and remove comment }

/* * Modifies the target wallet to add the minimal number of bills necessary to add * the amount of money represented by the dollars parameter */ public Wallet add(int dollars) { // YOUR CODE HERE return null; // Dummy return. Please change and remove comment }

/* * Modifies the target wallet to contain the same amount as before, but using the minimal * number of bills */ public Wallet minimize() { // YOUR CODE HERE return null; // Dummy return. Please change and remove comment } }

Here's the JUnit test (JUnit 4):

import static org.junit.Assert.*;

import org.junit.Before; import org.junit.Test;

public class WalletTest {

private Wallet zeroAll; private Wallet oneOfEach; private Wallet twoOfEach; private Wallet allOneHundred; private Wallet minimal1; private Wallet minimal2; private Wallet notMinimal1; private Wallet notMinimal2;

@Before public void setUp() { zeroAll = new Wallet(0,0,0,0,0); oneOfEach = new Wallet(1,1,1,1,1); // $136 twoOfEach = new Wallet(2,2,2,2,2); // $272 allOneHundred = new Wallet(100,20,10,5,1); // $500 minimal1 = new Wallet(4, 1, 1, 3, 2); // $279 minimal2 = new Wallet(3, 1, 0, 2, 2); // $248 notMinimal1 = new Wallet(279,0,0,0,0); notMinimal2 = new Wallet(203,1,0,2,0); }

@Test public void testAmountConstructor() { Wallet newZero = new Wallet(0); assertEquals(newZero, zeroAll); Wallet twoSeventyNine = new Wallet(279); assertEquals(twoSeventyNine, minimal1); Wallet twoFortyEight = new Wallet(248); assertEquals(twoFortyEight, minimal2); }

@Test public void testGetAmount() { assertEquals(0, zeroAll.getAmount()); assertEquals(136, oneOfEach.getAmount()); assertEquals(500, allOneHundred.getAmount()); assertEquals(279, minimal1.getAmount()); assertEquals(248, minimal2.getAmount()); }

@Test public void testAddWallet() { assertTrue(zeroAll == zeroAll.add(zeroAll)); assertEquals(zeroAll,zeroAll.add(zeroAll)); assertEquals(oneOfEach,new Wallet(zeroAll).add(oneOfEach)); assertEquals(oneOfEach,new Wallet(oneOfEach).add(zeroAll)); assertEquals(twoOfEach,new Wallet(oneOfEach).add(oneOfEach)); } @Test public void testAddAmount() { assertTrue(zeroAll == zeroAll.add(0)); assertEquals(oneOfEach,new Wallet(zeroAll).add(136)); assertEquals(minimal1,new Wallet(zeroAll).add(279)); assertEquals(minimal2,new Wallet(zeroAll).add(248)); } @Test public void minimize() { assertTrue(zeroAll.minimize()==zeroAll); assertEquals(zeroAll, new Wallet(zeroAll).minimize()); assertEquals(minimal1, new Wallet(notMinimal1).minimize()); assertEquals(minimal2, new Wallet(notMinimal2).minimize()); }

}

I already did the first three methods. I only need help in the last two. I mean, the methods add and minimize.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions