DriverNotReadyException

Report a typo

Suppose you have a DriverNotReadyException class with the following declaration:

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

Also, you have the following class Driver:

class Driver {
    String name;
    Boolean isDoorLocked;
    Boolean isBeltFastened;

    public void closeDoor() {
        this.isDoorLocked = true;
    }

    public void fastenBelt() {
        this.isBeltFastened = true;
    }

    public void drive(boolean isBeltFastened, boolean isDoorLocked) throws DriverNotReadyException {
        //put your code here
    }
}

Your task is to write a drive() method which prints to the console the string Here we go if both arguments are true, or otherwise throws the DriverNotReady exception with the message Close the door or fasten your seatbelt

Sample Input 1:

Sam
true
false

Sample Output 1:

Close the door or fasten your seatbelt
Write a program in Java 17
import java.util.Scanner;

class Driver {
String name;
Boolean isDoorLocked;
Boolean isBeltFastened;

public Driver(String name, Boolean isDoorLocked, Boolean isBeltFastened) {
this.name = name;
this.isDoorLocked = isDoorLocked;
this.isBeltFastened = isBeltFastened;

}

public void closeDoor() {
this.isDoorLocked = true;
}

public void fastenBelt() {
this.isBeltFastened = true;
}

public void drive(boolean isBeltFastened, boolean isDoorLocked) throws DriverNotReadyException {
//put your code
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
Boolean isDoorLocked = scanner.nextBoolean();
Boolean isBeltFastened = scanner.nextBoolean();
Driver driver = new Driver(name, isDoorLocked, isBeltFastened);
try {
driver.drive(isBeltFastened, isDoorLocked);
} catch (DriverNotReadyException e) {
System.out.println(e.getMessage());
}

}
}

class DriverNotReadyException extends Exception {
public DriverNotReadyException(String message) {
super(message);
}
}
___

Create a free account to access the full topic