21.4k views
0 votes
Write a class named "LicensePlate". It should store two pieces of information typically found on a US license plate:

the display string the plate. Call this field displayString.

The State that the plate comes from, stored as a two character String (ie. CA or AZ). Call this field "stateString"

It should have two constructors:

A two argument constructor that accepts two Strings. One for the displayString and one for the stateString. If the displayString is empty, or if the stateString is not exactly two characters, throw an IllegalArgumentException.

A one argument constructor that accepts a LicensePlate object, and creates a new one with the same displayString, and stateString. This constructor should invoke the two argument constructor.

And these methods...

Getters for the stateString and displayString. (No need for setters).

A boolean equals method that accepts a LicensePlate object and returns true if its strings match the receiving objects. (It is okay to use an Object parameter on this if you prefer).

Just write the class. Do not write anything else.

User Maxouhell
by
8.1k points

1 Answer

5 votes

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.

User Jakko
by
8.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.