// purpose: demonstrate Java class for objects with three nonnegative values // version: basic implementation -- just to get something out there. // constructor constructors still need work. import java.util.*; public class Triple { // description has different elements defining the various parts of the // of describing, building, and interacting with our object // naming of the attributes // object configurers // accessors for getting and setting attribute values // missing // specializing standard and providing expected behaviors // attributes int value1; int value2; int value3; // Triple(): default constructor public Triple() { // explicitly set the three values making up the object's attributes this.value1 = 0; this.value2 = 0; this.value3 = 0; } // Triple(): specific constructor public Triple( int a, int b, int c ) { // set the three values that making up the object's attributes this.value1 = a; this.value2 = b; this.value3 = c; } // get(): returns value of interest public int get( int index ) { // index specifies which value to return if ( index == 0 ) { // want first value return this.value1; } else if ( index == 1 ) { // want second value return this.value2; } else if ( index == 2 ) { // want third value return this.value3; } else { // illegal index return -1; } } // toString(): returns a String representation of our object public String toString() { // get the attribute values that make up the object int a = this.get( 0 ); int b = this.get( 1 ); int c = this.get( 2 ); // concatenate values to build up the string representation String result = "[ " + a + ", " + b + ", " + c + " ]"; // hand back the representation return result; } // set(): sets the indicated component public void set( int index, int update ) { // check to make sure update value is good if ( update < 0 ) { // illegal update value, do not update return; } else { // legal update value, attempt to update if ( index == 0 ) { // update first value this.value1 = update; } else if ( index == 1 ) { // update second value this.value2 = update; } else if ( index == 2 ) { // update third value this.value3 = update; } else { // illegal index, do not update } } } }