Question: Write a try-catch block to check this expression for division by 0. average = sum / num; The exception to test for is ArithmeticException. How

  1. Write a try-catch block to check this expression for division by 0.

average = sum / num;

The exception to test for is ArithmeticException.

  1. How many steps does the contains() method execute in the worst case?

public boolean contains (int target) {

// Use manyItems and NOT length

for (int i=0; i < manyItems; i++) {

if (data[i] == target)

return true;

}

return false;

}

3. Consider this class diagram for the OrderedBookBag class and the code for its remove() method. Note: The Book class compareTo() method compares book titles.

OrderedBookBag

- data : Book[]

- manyItems : int

+ OrderedBookBag()

+ add ( b : Book ) : void

+ remove ( element : Book ) : void

+ toString () : String

public void remove(Book element) {

boolean found = false;

int i = 0;

// Find it

while (i < manyItems &&

(data[i].compareTo(element) <=0) && !found)

if (data[i].equals(element))

found = true;

else

i++;

// remove it, but only if found

if (found) {

for (int index = i; index < manyItems-1; index++)

data[index] = data[index+1];

// One less item

manyItems--;

}

}

  1. What are the critical test cases (describe in words)? That is, which cases are most likely to cause problems with the remove() method?

  1. Write code for each test case and add those instructions to the scaffolding below.

OrderedBookBag obb = new OrderedBookBag();

Book book1 = new Book("Educated", 2019);

Book book2 = new Book ("Just Mercy", 2020);

Book book3 = new Book("The Eye of the Elephant", 2019);

Book book4 = new Book("The Yellow House", 2019);

obb.insert(book1);

obb.insert(book2);

obb.insert(book3);

obb.insert(book4);

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!