Answer:
public class LicensePlate {
private String displayString;
private String stateString;
public LicensePlate(String displayString, String stateString) {
if (displayString.isEmpty() || stateString.length() != 2) {
throw new IllegalArgumentException();
}
this.displayString = displayString;
this.stateString = stateString;
}
public LicensePlate(LicensePlate other) {
this(other.displayString, other.stateString);
}
public String getDisplayString() {
return displayString;
}
public String getStateString() {
return stateString;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
LicensePlate other = (LicensePlate) obj;
return displayString.equals(other.displayString) && stateString.equals(other.stateString);
}
}
Step-by-step explanation:
- The `LicensePlate` class has two private fields: `displayString` and `stateString` to store the license plate information.
- The two-argument constructor checks if the `displayString` is empty or if the `stateString` is not exactly two characters. If either condition is true, an `IllegalArgumentException` is thrown. Otherwise, the fields are initialized with the provided values.
- The one-argument constructor invokes the two-argument constructor using the `displayString` and `stateString` of the provided `LicensePlate` object.
- Getter methods (`getDisplayString()` and `getStateString()`) are provided to retrieve the values of the `displayString` and `stateString` fields, respectively.
- The `equals()` method overrides the default `equals()` method inherited from the `Object` class. It compares the `displayString` and `stateString` fields of two `LicensePlate` objects and returns `true` if they match.