Question: i used the solution from early java objects chapter 5 question 30 here is my code fro the driver public class AutoPolicyDriver { public static
i used the solution from early java objects chapter 5 question 30
here is my code fro the driver
public class AutoPolicyDriver {
public static void main(String[] args) {
AutoPolicy policy1 = new AutoPolicy(11111111, "Toyota Camry", "NJ");
policyIsNoFaultState(policy1);
AutoPolicy policy2 = new AutoPolicy(22222222, "Ford Fusion", "WI");
policyIsNoFaultState(policy2);
}
public static void policyIsNoFaultState(AutoPolicy policy) {
System.out.println("The auto policy:");
System.out.printf("Account #: %d; Car: %s;%nState %s %s a no-fault state%n%n", policy.getAccountNumber(), policy.getMakeAndModel(), policy.getState(), (policy.isNoFaultState() ? "is": "is not" ));
}
}
here the code for method called
public class AutoPolicy {
private int accountNumber;
private String makeAndModel;
private String state;
public AutoPolicy (int accountNumber, String makeAndModel, String state) {
this.accountNumber = accountNumber;
this.makeAndModel = makeAndModel;
setState(state);
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public int getAccountNumber() {
return accountNumber;
}
public void setMakeAndModel(String makeAndModel) {
this.makeAndModel = makeAndModel;
}
public String getMakeAndModel() {
return makeAndModel;
}
public void setState(String state) {
if(state.equals("CT") || state.equals("MA") || state.equals("ME") || state.equals("NH")
|| state.equals("NJ") || state.equals("NY") || state.equals("PA") || state.equals("VT")) {
this.state = state;
}
else {System.out.println("Invalid state code");}
}
public String getState() {
return state;
}
public boolean isNoFaultState() {
boolean noFaultState;
switch (getState()) {
case "CT":
case "MA":
case "ME":
case "NH":
case "NJ":
case "NY":
case "PA":
case "VT":
noFaultState = true;
break;
default:
noFaultState = false;
break;
}
return noFaultState;
}
}
the out put from the example is
Invalid state code
The auto policy:
Account #: 11111111, Car: Toyota Camry,
State MA is a no-fault state
The auto policy:
Execption java.lanhg.NullPointerException
my output is
The auto policy:
Account #: 11111111; Car: Toyota Camry;
State NJ is a no-fault state
Invalid state code
The auto policy:
Exception in thread "main" java.lang.NullPointerException
at AutoPolicy.isNoFaultState(AutoPolicy.java:44)
at AutoPolicyDriver.policyIsNoFaultState(AutoPolicyDriver.java:18)
at AutoPolicyDriver.main(AutoPolicyDriver.java:12)
was wondering why the difference
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
