public class PetRecord implements Cloneable
1:
2: /**
3: Class for basic pet records: name, age, and weight.
4: This version implements the Cloneable interface.
5: */
6: public class PetRecord implements Cloneable
7: {
8: private String name;
9: private int age;//in years
10: private double weight;//in pounds
11:
12: public Object clone( )
13: {
14: try
15: {
16: return super.clone( );//Invocation of clone in class Object
17: }
18: catch(CloneNotSupportedException e)
19: {//This should not happen
20: return null; //To keep the compiler happy.
21: }
22: }
23:
24: public void writeOutput( )
25: {
26: System.out.println("Name: " + name);
27: System.out.println("Age: " + age + " years");
28: System.out.println("Weight: " + weight + " pounds");
29: }
30:
31: public PetRecord(String initialName, int initialAge,
32: double initialWeight)
33: {
34: name = initialName;
35: if ((initialAge < 0) || (initialWeight < 0))
36: {
37: System.out.println("Error: Negative age or weight.");
38: System.exit(0);
39: }
40: else
41: {
42: age = initialAge;
43: weight = initialWeight;
44: }
45: }
46:
47: public void set(String newName, int newAge, double newWeight)
48: {
49: name = newName;
50: if ((newAge < 0) || (newWeight < 0))
51: {
52: System.out.println("Error: Negative age or weight.");
53: System.exit(0);
54: }
55: else
56: {
57: age = newAge;
58: weight = newWeight;
59: }
60: }
61:
62: public PetRecord(String initialName)
63: {
64: name = initialName;
65: age = 0;
66: weight = 0;
67: }
68:
69: public void set(String newName)
70: {
71: name = newName; //age and weight are unchanged.
72: }
73:
74: public PetRecord(int initialAge)
75: {
76: name = "No name yet.";
77: weight = 0;
78: if (initialAge < 0)
79: {
80: System.out.println("Error: Negative age.");
81: System.exit(0);
82: }
83: else
84: age = initialAge;
85: }
86:
87: public void set(int newAge)
88: {
89: if (newAge < 0)
90: {
91: System.out.println("Error: Negative age.");
92: System.exit(0);
93: }
94: else
95: age = newAge;
96: //name and weight are unchanged.
97: }
98:
99: public PetRecord(double initialWeight)
100: {
101: name = "No name yet";
102: age = 0;
103: if (initialWeight < 0)
104: {
105: System.out.println("Error: Negative weight.");
106: System.exit(0);
107: }
108: else
109: weight = initialWeight;
110: }
111:
112: public void set(double newWeight)
113: {
114: if (newWeight < 0)
115: {
116: System.out.println("Error: Negative weight.");
117: System.exit(0);
118: }
119: else
120: weight = newWeight; //name and age are unchanged.
121: }
122:
123: public PetRecord( )
124: {
125: name = "No name yet.";
126: age = 0;
127: weight = 0;
128: }
129:
130: public String getName( )
131: {
132: return name;
133: }
134:
135: public int getAge( )
136: {
137: return age;
138: }
139:
140: public double getWeight( )
141: {
142: return weight;
143: }
144: }
145: