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