1 public class Value { 2 protected int value; 3 4 public Value(int value) { 5 this.value = value; 6 } 7 8 public int getValue() { 9 return this.value; 10 } 11 12 public void setValue(int value) { 13 this.value = value; 14 } 15 16 public boolean isPositive() { 17 return this.value > 0; 18 } 19 20 public int compare(int value) { 21 if (value < this.value) { 22 return -1; 23 } else if (value == this.value) { 24 return 0; 25 } else { 26 return 1; 27 } 28 } 29 30 public int add(int value) { 31 return this.value + value; 32 } 33 34 public Value newValue() { 35 return new Value(this.getValue()); 36 } 37 38 public static void main(String[] args) { 39 Value v = new Value(123); 40 if (v.getValue() != 123) { 41 System.err.println("v.getValue() failed!"); 42 } else { 43 System.out.println("v.getValue() correct: " + v.getValue()); 44 } 45 v.setValue(456); 46 if (v.getValue() != 456) { 47 System.err.println("v.setValue(456) or v.getValue() failed!"); 48 } else { 49 System.out.println("v.getValue() correct: " + v.getValue()); 50 } 51 if (!v.isPositive()) { 52 System.err.println("v.isPositive() failed!"); 53 } else { 54 System.out.println("v.isPositive() correct: " + v.isPositive()); 55 } 56 v.setValue(-789); 57 if (v.isPositive()) { 58 System.err.println("v.isPositive() failed!"); 59 } else { 60 System.out.println("v.isPositive() correct: " + v.isPositive()); 61 } 62 if (v.compare(-790) != -1) { 63 System.err.println("v.compare(-790) failed!"); 64 } else { 65 System.out.println("v.compare(-790) correct: " + v.compare(-790)); 66 } 67 if (v.compare(-788) != 1) { 68 System.err.println("v.compare() failed!"); 69 } else { 70 System.out.println("v.compare(-788) correct: " + v.compare(-788)); 71 } 72 if (v.compare(-789) != 0) { 73 System.err.println("v.compare() failed!"); 74 } else { 75 System.out.println("v.compare(-789) correct: " + v.compare(-789)); 76 } 77 Value v2 = v.newValue(); 78 if (v == v2) { 79 System.err.println("v.newValue() failed!"); 80 } 81 v2.setValue(123); 82 if (v.getValue() == v2.getValue()) { 83 System.err.println("v.newValue() failed (to establish separate members)!"); 84 } else { 85 System.out.println("v.getValue() == v2.getValue() correct: " + (v.getValue() == v2.getValue())); 86 } 87 if (v2.add(-123) != 0) { 88 System.err.println("v2.add(-123) failed!"); 89 } else { 90 System.out.println("v2.add(-123) correct: " + v2.add(-123)); 91 } 92 v2.setValue(255); 93 if (v2.getValue() != 255) { 94 System.err.println("v2.setValue(255) or v2.getValue() failed!"); 95 } else { 96 System.out.println("v2.getValue() correct: " + v2.getValue()); 97 } 98 } 99 } 100 101 // vim: tabstop=4 expandtab shiftwidth=4