How do I fix the error in my Java code that says: 'class WatchException is public, should be declared in a file named WatchException.java'? I understand this error means my public class WatchException needs to be in a file named WatchException.java

This is my code based on the question:

public class Watch {
private int wserial;
private double wprice;
private String wdesc;
private int wtype;

public Watch(int wserial, double wprice, String wdesc, int wtype) throws WatchException {
    setSerial(wserial);
    setPrice(wprice);
    setDesc(wdesc);
    setType(wtype);
}

public void setSerial(int wserial) throws WatchException {
    if (wserial >= 10001 && wserial <= 99999) {
        this.wserial = wserial;
    } else {
        throw new WatchException("Invalid serial number. Must be between 10001 and 99999.");
    }
}

public void setPrice(double wprice) throws WatchException {
    if (wprice >= 0 && wprice <= 500000) {
        this.wprice = wprice;
    } else {
        throw new WatchException("Invalid price. Must be between 0 and 500,000.");
    }
}

public void setDesc(String wdesc) throws WatchException {
    if (wdesc != null && wdesc.trim().length() >= 2) {
        this.wdesc = wdesc;
    } else {
        throw new WatchException("Invalid description. Must be at least 2 non-blank characters.");
    }
}

public void setType(int wtype) throws WatchException {
    if (wtype >= 1 && wtype <= 5) {
        this.wtype = wtype;
    } else {
        throw new WatchException("Invalid type. Must be between 1 and 5.");
    }
}

public int getSerial() {
    return wserial;
}

public double getPrice() {
    return wprice;
}

public String getDesc() {
    return wdesc;
}

public int getType() {
    return wtype;
}

@Override
public String toString() {
    String typeString;
    switch (wtype) {
    case 1:
        typeString = "Quartz";
        break;
    case 2:
        typeString = "Solar";
        break;
    case 3:
        typeString = "Mechanical";
        break;
    case 4:
        typeString = "Automatic";
        break;
    case 5:
        typeString = "Digital";
        break;
    default:
        typeString = "Unknown";
        break;
    }

    return String.format("Watch Serial: %d\nPrice: $%.2f\nDescription: %s\nType: %s",
                         wserial, wprice, wdesc, typeString);
}

}

public class WatchException extends Exception {
private String message;
public WatchException() {
super();
}

public WatchException(String message) {
    super(message);
    this.message = message;
}

public void setMessage(String msg) {
    this.message = msg;
}

public String getMessage() {
    return message;
}

}

AND HERE IS THE LINK TO FOLLOW THE INSTRUCTIONS SINCE THE CODE NEEDS TO FOLLOW THE INSTRUCTIONS FOR FULL CREDIT:

Explain like I’m five on how I can fix this, make sure that your solution makes the code able to function like in the instructions. And that the code 100% follows the instructions too

Got it…

BUT, do you have a solution?

Good question for your existing thread :blush: