Question: I ran this code for each test. Test 1, and test 2 work but not test 3 because I have to modify this method. Specification:

I ran this code for each test. Test 1, and test 2 work but not test 3 because I have to modify this method.

Specification: LetterInventorysubtract(LetterInventory other) "Constructs and returns a new LetterInventory object that represents the result of subtracting the other inventory from this inventory (i.e., subtracting the counts in the other inventory from this objects counts). If any resulting count would be negative, your method should return null. The two LetterInventoryobjects being subtracted (this and other) should not be changed by this method

public LetterInventory subtract(LetterInventory other) {

LetterInventory inventory2 = new LetterInventory(this.toString() );

for (int i = 0; i < count.length; i++) {

inventory2.count[i] = inventory2.count[i] - other.count[i];

if (inventory2.count[i] < 0) {

return null;

}

}

return inventory2;

}

}

console output :

Testing these two strings:

i1: "aaggghhhh"

i2: "ggg"

constructing i1 and i2

constructing and testing i1.add(i2)

inventory = [aagggggghhhh]

toString, size, isEmpty, and count all passed

constructing and testing i2.add(i1)

inventory = [aagggggghhhh]

toString, size, isEmpty, and count all passed

constructing and testing i1.subtract(i2)

inventory = [aahhhh]

size failed

correct size = 6

your size = 9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!