Java Reference
In-Depth Information
41
// sets the state
42
public void
setState(String state)
43
{
44
this
.state = state;
45
}
46
47
// returns the state
48
public
String getState()
49
{
50
return
state;
51
}
52
53
// predicate method returns whether the state has no-fault insurance
54
public boolean
isNoFaultState()
55
{
56
boolean
noFaultState;
57
58
// determine whether state has no-fault auto insurance
switch
(getState())
// get AutoPolicy object's state abbreviation
{
case
"MA"
:
case
"NJ"
:
case
"NY"
:
case
"PA"
:
noFaultState =
true
;
break
;
default
:
noFaultState =
false
;
break
;
}
59
60
61
62
63
64
65
66
67
68
69
return
noFaultState;
70
}
71
}
// end class AutoPolicy
Fig. 5.11
|
Class that represents an auto insurance policy. (Part 2 of 2.)
Class
AutoPolicyTest
Class
AutoPolicyTest
(Fig. 5.12) creates two
AutoPolicy
objects (lines 8-11 in
main
).
Lines 14-15 pass each object to
static
method
policyInNoFaultState
(lines 20-28),
which uses
AutoPolicy
methods to determine and display whether the object it receives
represents a policy in a no-fault auto insurance state.
1
// Fig. 5.12: AutoPolicyTest.java
2
// Demonstrating Strings in switch.
3
public class
AutoPolicyTest
4
{
5
public static void
main(String[] args)
6
{
7
// create two AutoPolicy objects
8
AutoPolicy policy1 =
9
new
AutoPolicy(
11111111
,
"Toyota Camry"
,
"NJ"
);
10
AutoPolicy policy2 =
11
new
AutoPolicy(
22222222
,
"Ford Fusion"
,
"ME"
);
Fig. 5.12
|
Demonstrating
String
s in
switch
. (Part 1 of 2.)