Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Question 4: An Exceptional Stamp Collection

Pay special attention to BOLDED items below, which are often changes from the similar problem from Midterm
#1.
Please make sure to click the "Save" button before your time runs out. If you do not save your work, it will
result in a score of 0 on this question!

Consider an application under construction for a US-Canadian stamp collection where stamps from
Canada and USA
are registered and displayed.
Look carefully at the following StampCollection class.
Note that a watchList has been added as a field and constructor parameter, and the specification of addStamp
has changed significantly.
package bgzebir;

import stampcollection.exception.*;

import java.util.ArrayList;

import java.util.List;

// Represents a collection of stamps.

public class StampCollection {

private List<Stamp> stamps;

private List<String> watchList;

// EFFECTS: constructs a stamp collection without any stamps present

// and with an empty watchlist

public StampCollection() {

this(new ArrayList<>());

// EFFECTS: constructs a stamp collection without any stamps present

// and with (a copy of) the given watch list

public StampCollection(List<String> watchList) {

this.stamps = new ArrayList<>();

this.watchList = new ArrayList<>(watchList);

// MODIFIES: this

// EFFECTS: adds the given stamp to the collection. Insertion order is

// maintained. Duplicate stamps with distinct stamp IDs are allowed,

// so stamps with the same title/country/value can be added multiple times.

// IF: the ID of stamp matches the ID of any stamp already in the collection,

// THROWS: TwinnedStampException and does not add the stamp.

// The exception's message is "Duplicate: " followed by the stamp's id.

// OTHERWISE, IF: the ID of the stamp is on the watchList

// THROWS: KnockoffStampException but DOES add the stamp first.

// The exception's message is "Watchlist: " followed by the stamp's id.

public void addStamp(Stamp stamp) throws TwinnedStampException, KnockoffStampException {

this.stamps.add(stamp); // INCORRECT BODY TO BE REPLACED IN FINAL PART

// EFFECTS: returns a list of all stamps that have a value higher or equal

// the provided minValue. Duplicate stamps are allowed. No order is

// guaranteed among the returned stamps.

// IF: minValue < 0

// THROWS: Java's built-in IllegalArgumentException (an unchecked exception)

// with the exception text: "minValue was negative: " followed by

// the value passed in

public List<Stamp> getStamps(int minValue) {

return null; // STUB

Assume that there is a class Stamp with fields id (String), title (String), country (String)
and value (int) and a
constructor that sets the four fields. In addition, getters for all fields are
present: getId(), getTitle(), getValue(),
and getCountry(). No additional information is required for this question.
For this question, we are providing the documentation for a set of useful methods from the List interface:
boolean contains(Object o) - returns true if this list contains the specified element.
int size() - returns the number of elements in this list.
boolean add(E e) - appends the specified element to the end of this list. Returns true if this list changed as a
result of the call.
In addition, we are providing the documentation for a set of useful assertions from the JUnit library:
static void assertEquals(Object expected, Object actual) - asserts that two objects are equal.
static void assertTrue(boolean condition) - asserts that a condition is true.
static void assertFalse(boolean condition) - asserts that a condition is false.
static void fail(String message) - automatically fails an assertion with the given message.
Part 1
Below is a skeleton of a test suite for the addStamp method in the StampCollection class.
For this test suite, you
can assume that all other methods in the StampCollection class are implemented
in accordance with their
specification. Complete the design of the corresponding test class below, given the following
notes (please read
these carefully!):
1. The case that each method tests must be derived from the name of the test method and associated
comments.
2. You are only allowed to change the test method bodies and no other code.
3. You are allowed to copy/paste code between the test methods.
4. You do not need to add any import statements.
5. You may NOT use a @BeforeEach-tagged method.
6. Tests must verify the final contents of the stamp collection.
7. If a test expects a particular exception, it must check that exception's message: the result of calling
its getMessage() method. (Ignore unexpected exceptions' messages.)
8. Tests must be concise, complete, and clear.
TestStampCollection.java
1 package bgzebir;
2
3 import org.junit.jupiter.api.Test;
4 import stampcollection.exception.*;
5
6 import static org.junit.jupiter.api.Assertions.fail;
7
8 public class TestStampCollection {
9
10 @Test
11 // Tests that addStamp throws a TwinnedStampException when attempting to add
12 // a stamp with the same ID, even though no other fields of the stamps match.
13 // (Assume a different test already confirms that a valid stamp can be added.)
14 public void testAddTwinnedStampById() {
15 // You may delete or use the code in this function body as you see fit.
16 StampCollection stampCollection = new StampCollection();
17 Stamp stamp1 = new Stamp("id", "octan", "Canada", 8661);
18 Stamp stamp2 = new Stamp("id", "octan2", "US", 8662);
19
20 try {
21 stampCollection.addStamp(stamp1);
22 stampCollection.addStamp(stamp2);
23 fail("Should not have reached this line");
24 } catch (TwinnedStampException e) {
25 assertTrue(!e.getMessage().isEmpty());
26 // System.out.println("Duplicate: " + stamp2.getId());
27 // all good
28 } catch (KnockoffStampExcepption e) {
29 fail("Unexpected KnockoffStampException");
30 }
31 assertEquals(1, stampCollection.size());
32 assertTrue(stampCollection.contains(stamp1));
33 // assertEquals(stamp1, stampCollection.get(0));
34 }
35 }
36
36
37
38 // public void testAddTwinnedStampById() {
39 // // You may delete or use the code in this function body as you see fit.
40 // StampCollection stampCollection = new StampCollection();
41 // Stamp stamp1 = new Stamp("id", "octan", "Canada", 8661);
42 // Stamp stamp2 = new Stamp("id", "octan2", "US", 8662);
43 // try {
44 // stampCollection.addStamp(stamp1);
45 // stampCollection.addStamp(stamp2);
46 // fail("should not reach this line");
47 // } catch (TwinnedStampException e) {
48 // System.out.println("all goes as planned!");
49 // } catch (KnockoffStampException e) {
50 // fail("no KnockoffStampException");
51 // }
52
53 // }
54
55

Restore original file

Part 2
Correct the implementation of the addStamp method in the StampCollection class below.
The code in the editor
below is the same as the code presented at the top of the question.
You must only change the body of the
addStamp method and no other code.

For this part, you need to know that each stamp exception's constructor takes the problematic Stamp as
its only argument.
StampCollection.java
1 package bgzebir;
2
3 import stampcollection.exception.*;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 // Represents a collection of stamps.
9 public class StampCollection {
10
11 private List<Stamp> stamps;
12 private List<String> watchList;
13
14 // EFFECTS: constructs a stamp collection without any stamps present
15 // and with an empty watchlist
16 public StampCollection() {
17 this(new ArrayList<>());
18 }
19
20 // EFFECTS: constructs a stamp collection without any stamps present
21 // and with (a copy of) the given watch list
22 public StampCollection(List<String> watchList) {
23 this.stamps = new ArrayList<>();
24 this.watchList = new ArrayList<>(watchList);
25 }
26
27 // MODIFIES: this
28 // EFFECTS: adds the given stamp to the collection. Insertion order is
29 // maintained. Duplicate stamps with distinct stamp IDs are allowed,
30 // so stamps with the same title/country/value can be added multiple times.
31 // IF: the ID of stamp matches the ID of any stamp already in the collection,
32 // THROWS: TwinnedStampException and does not add the stamp.
33 // The exception's message is "Duplicate: " followed by the stamp's id.
34 // OTHERWISE, IF: the ID of the stamp is on the watchList
35 // THROWS: KnockoffStampException but DOES add the stamp first.
36 // The exception's message is "Watchlist: " followed by the stamp's id.
37 public void addStamp(Stamp stamp) throws TwinnedStampException, KnockoffStampException
        {
38 String targetId = stamp.getId();
39
40 for (Stamp currentStamp : stamps) {
41 if (currentStamp.getId().equals(targetId)) {
42 throw new TwinnedStampException("Duplicate: " + targetId);
43 }
44 }
45
46 // if all goes well: no duplicated id happened
47 this.stamps.add(stamp);
48
49 for (String curretnWatch : watchlist) {
50 if ( tW t h l (t tId)) {
50 if (currentWatch.equals(targetId)) {
51 throw new KnockoffStampException("Watchlist: " + targetId);
52 }
53 }
54 }
55
56 // EFFECTS: returns a list of all stamps that have a value higher or equal
57 // the provided minValue. Duplicate stamps are allowed. No order is
58 // guaranteed among the returned stamps.
59 // IF: minValue < 0
60 // THROWS: Java's built-in IllegalArgumentException (an unchecked exception)
61 // with the exception text: "minValue was negative: " followed by
62 // the value passed in
63 public List<Stamp> getStamps(int minValue) {
64 return null; // STUB
65 }
66 }
67

Restore original file

This question is complete and cannot be answered again.

Correct answer

Part 1

Part 2

Feedback from the Course Staff


(for submitted answer 5) hide

Open the GradeScope link on Canvas for feedback. Your results are in Variant B

Submitted answer
5 hide
Submitted at 2022-11-07 18:46:34 (PST) manual grading: 82%

Part 1

Part 2

Files
TestStampCollection.java Download Show preview
uploaded
StampCollection.java Download Show preview
uploaded

Submitted answer
4 show
Submitted at 2022-11-07 18:44:51 (PST)

Submitted answer
3 show
Submitted at 2022-11-07 18:41:19 (PST)

Show/hide older submissions

Exam 2

Assessment overview

Question
Submission status: 0%
Total points: 37 /45
Manually-graded question
Report an error in this question

Previous question Next question

Attached files
No attached files
Attachments can't be added or deleted because the assessment is closed.

You might also like