1. This question involves simulating an archaeological dig using the ArtifactDig class. The class tracks the artifacts found during a digging session. You will write a constructor and a method in the ArtifactDig class.
public class ArtifactDig
{
/** A string containing the names of found artifacts, separated by spaces */
private String excavatedItems; // To be initialized in part (a)
/** The total number of artifacts found */
private int itemsFound; // To be initialized in part (a)
/**
* Simulates digging in a specific location for a given number of layers.
* Updates excavatedItems and itemsFound as described in part (a)
* Precondition: layers > 0
*/
public ArtifactDig(int layers)
{ /* to be implemented in part (a) */ }
/**
* Returns the artifact found in the current layer, or null if nothing is found.
*/
public String digLayer()
{ /* implementation not shown */ }
/**
* Returns true if the artifact target appears in excavatedItems
* at least quantity times; otherwise, returns false.
* Precondition: excavatedItems contains words separated by single spaces.
* target is a single word with no spaces.
*/
public boolean checkCollection(String target, int quantity)
{ /* to be implemented in part (b) */ }
}Write the ArtifactDig constructor, which initializes the instance variables and simulates a dig of the specified number of layers. The constructor should initialize excavatedItems to an empty string and itemsFound to 0. It should then call the helper method digLayer exactly layers times. If digLayer returns a non-null value (representing an artifact), that artifact name should be appended to excavatedItems followed by a single space, and itemsFound should be incremented.
Example 1
| Method Call | Return Value |
|---|---|
| digLayer() | "mask" |
| digLayer() | null |
| digLayer() | "pottery" |
| digLayer() | "mask" |
| digLayer() | null |
/** * Simulates digging in a specific location for a given number of layers. * Updates excavatedItems and itemsFound as described in part (a) * Precondition: layers > 0 */ public ArtifactDig(int layers)
Write the checkCollection method, which returns true if the specified target artifact appears in excavatedItems at least quantity times, and false otherwise. You may assume that target is not a substring of any other artifact name (e.g., if target is "pot", it will not match "pottery").
Example 1
| Method Call | Return Value |
|---|---|
| checkCollection("mask", 2) | true |
| checkCollection("pottery", 2) | false |
| checkCollection("bone", 1) | false |
/** * Returns true if the artifact target appears in excavatedItems * at least quantity times; otherwise, returns false. * Precondition: excavatedItems contains words separated by single spaces. * target is a single word with no spaces. */ public boolean checkCollection(String target, int quantity)
00:00