Java Reference
In-Depth Information
If there's a match, then line 62 sets local variable
noFaultState
to
true
and the
switch
statement terminates; otherwise, the
default
case sets
noFaultState
to
false
(line 65).
Then method
isNoFaultState
returns local variable
noFaultState
's value.
For simplicity, we did not validate an
AutoPolicy
's data in the constructor or
set
methods, and we assume that state abbreviations are always two uppercase letters. In addi-
tion, a real
AutoPolicy
class would likely contain many other instance variables and
methods for data such as the account holder's name, address, etc. In Exercise 5.30, you'll be
asked to enhance class
AutoPolicy
by validating its state abbreviation using techniques that
you'll learn in Section 5.9.
1
// Fig. 5.11: AutoPolicy.java
2
// Class that represents an auto insurance policy.
3
public class
AutoPolicy
4
{
5
private int
accountNumber;
// policy account number
6
private
String makeAndModel;
// car that the policy applies to
7
private
String state;
// two-letter state abbreviation
8
9
// constructor
10
public
AutoPolicy(
int
accountNumber, String makeAndModel, String state)
11
{
12
this
.accountNumber = accountNumber;
13
this
.makeAndModel = makeAndModel;
14
this
.state = state;
15
}
16
17
// sets the accountNumber
18
public void
setAccountNumber(
int
accountNumber)
19
{
20
this
.accountNumber = accountNumber;
21
}
22
23
// returns the accountNumber
24
public int
getAccountNumber()
25
{
26
return
accountNumber;
27
}
28
29
// sets the makeAndModel
30
public void
setMakeAndModel(String makeAndModel)
31
{
32
this
.makeAndModel = makeAndModel;
33
}
34
35
// returns the makeAndModel
36
public
String getMakeAndModel()
37
{
38
return
makeAndModel;
39
}
40
Fig. 5.11
|
Class that represents an auto insurance policy. (Part 1 of 2.)