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 the code:

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

This is a homework assignment. I’m afraid you might be in the wrong place.

What would be the right place?

It’s also unclear what you’re asking for or what troubleshooting you did. An error arose but you identified the fix. Did you implement that fix? Did it not work? Is there a different email message?

Also if an admin’s around, this isn’t Exercism-related so it’s not in the right sub-forum. Maybe we put it in the catch-all programming channel at Programming - Exercism?

I didn’t implement that fix and it didn’t work. If you run the entire code , there would be one error within line 95 where the error in the Java code says class WatchException is public, should be declared in a file named WatchException.java.

I’m just wondering if I need that to be in a file named WatchException.java.

Try it out and see what happens.