public class Pet2
1: //Pet2.java
2:
3: /**
4: * Revised class for basic pet data: name, age, and weight.
5: * Constructors and set methods call a private method.
6: */
7: public class Pet2
8: {
9: private String name;
10: private int age; //in years
11: private double weight;//in pounds
12:
13: public Pet2
14: (
15: String initialName,
16: int initialAge,
17: double initialWeight
18: )
19: {
20: set(initialName, initialAge, initialWeight);
21: }
22:
23: public Pet2
24: (
25: String initialName
26: )
27: {
28: set(initialName, 0, 0);
29: }
30:
31: public Pet2
32: (
33: int initialAge
34: )
35: {
36: set("No name yet.", initialAge, 0);
37: }
38:
39: public Pet2
40: (
41: double initialWeight
42: )
43: {
44: set("No name yet.", 0, initialWeight);
45: }
46:
47: public Pet2()
48: {
49: set("No name yet.", 0, 0);
50: }
51:
52: public void setPet
53: (
54: String newName,
55: int newAge,
56: double newWeight
57: )
58: {
59: set(newName, newAge, newWeight);
60: }
61:
62: public void setName
63: (
64: String newName
65: )
66: {
67: set(newName, age, weight);//age and weight are unchanged.
68: }
69:
70: public void setAge
71: (
72: int newAge
73: )
74: {
75: set(name, newAge, weight);//name and weight are unchanged.
76: }
77:
78: public void setWeight
79: (
80: double newWeight
81: )
82: {
83: set(name, age, newWeight);//name and age are unchanged.
84: }
85:
86: private void set
87: (
88: String newName,
89: int newAge,
90: double newWeight
91: )
92: {
93: name = newName;
94: if ((newAge < 0) || (newWeight < 0))
95: {
96: System.out.println("Error: Negative age or weight.");
97: System.exit(0);
98: }
99: else
100: {
101: age = newAge;
102: weight = newWeight;
103: }
104: }
105:
106: public String getName()
107: {
108: return name;
109: }
110:
111: public int getAge()
112: {
113: return age;
114: }
115:
116: public double getWeight()
117: {
118: return weight;
119: }
120:
121: public void writeOutput()
122: {
123: System.out.println("Name: " + name);
124: System.out.println("Age: " + age + " years");
125: System.out.println("Weight: " + weight + " pounds");
126: }
127: }