Some AP CS A Free Response problems can benefit from the application of standard algorithms. Using standard algorithms where appropriate reduces the chance of errors. This problem is intended as practice with identifying situations for which standard algorithms are appropriate, and applying the standard algorithms to those situations.
Please don’t use this problem to practice your timing for the AP CS A Free Response section. Use only actual released AP CS FR for timing practice. The approach for this problem is identical to a real AP CS A FR.
Problem description
The Specimen
class is used to store information about the material and quality of a specimen.
public class Specimen
{
private String material;
private int quality;
// Precondition: material != null
public Specimen(String material, int quality)
{
this.material = material;
this.quality = quality;
}
public String material()
{
return material;
}
public int quality()
{
return quality;
}
}
The method below appears in a class other than Specimen
.
public static Specimen handleNewSpecimen(
ArrayList<Specimen> specList, Specimen newSpec)
{ /* to be implemented */ }
The parameter specList
is a list of specimens, sorted in ascending order by material. No 2 specimens in specList
have the same material.
The method handles newSpec
, a new specimen, according to the rules below.
If there is no specimen in specList
with the same material as newSpec
, newSpec
is inserted into specList
in the correct position to maintain the sorted order.
If there is a specimen in specList
with the same material as newSpec
, the quality of newSpec
is compared with the quality of the specimen in the list. If the quality of newSpec
is greater than the quality of the specimen in the list, the specimen in the list is replaced with newSpec
. If the quality of newSpec
is less than or equal to the quality of the specimen in the list, the list is not changed.
The method returns the specimen from specList
that was replaced by newSpec
, or null
if no specimen was replaced.
Consider the examples below. Each Specimen
object is represented as:
{material, quality}
Example 1
// before method call
specList -> [{"iron", 20}, {"mercury", 5}, {"tin", 10}]
newSpec -> {"mercury", 10}
handleNewSpecimen(specList, newSpec); // returns {"mercury", 5}
// after method call
specimens -> [{"iron", 20}, {"mercury", 10}, {"tin", 10}]
Example 2
// before method call
specList -> [{"iron", 20}, {"mercury", 5}, {"tin", 10}]
newSpec -> {"tin", 10}
handleNewSpecimen(specList, newSpec); // returns null
// after method call
specList -> [{"iron", 20}, {"mercury", 10}, {"tin", 10}]
Example 3
// before method call
specList -> [{"iron", 20}, {"mercury", 5}, {"tin", 10}]
newSpec -> {"silver", 10}
handleNewSpecimen(specList, newSpec); // returns null
// after method call
specList -> [{"iron", 20}, {"mercury", 5}, {"silver", 10}, {"tin", 10}]
Example 4
// before method call
specList -> []
newSpec -> {"gold", 5}
handleNewSpecimen(specList, newSpec); // returns null
// after method call
specList -> [{"gold", 5}]
Example 5
// before method call
specList -> [{"iron", 20}, {"mercury", 5}, {"tin", 10}]
newSpec -> {"carbon", 20}
handleNewSpecimen(specList, newSpec); // returns null
// after method call
specList -> [{"carbon", 20}, {"iron", 20}, {"mercury", 5}, {"tin", 10}]
Example 6
// before method call
specList -> [{"iron", 20}, {"mercury", 5}, {"tin", 10}]
newSpec -> {"zinc", 20}
handleNewSpecimen(specList, newSpec); // returns null
// after method call
specList -> [{"iron", 20}, {"mercury", 5}, {"tin", 10}, {"zinc", 20}]
handleNewSpecimen
method
Implement method handleNewSpecimen
.
public static Specimen handleNewSpecimen(
ArrayList<Specimen> specList, Specimen newSpec)
Solution & comments
See the Specimen solution or review it with AP CS Tutor Brandon Horn.