// purpose: demonstrate Java class for objects with three nonnegative values // version: basic implementation -- practices safe programming 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.set( 0, 0 ); this.set( 1, 0 ); this.set( 2, 0 ); } // Triple(): specific constructor public Triple( int a, int b, int c ) { // set the three values that making up the object's attributes this.set( 0, a ); this.set( 1, b ); this.set( 2, 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 } } } // indexOf(): returns the index of the indicated component public int indexOf( int value ) { return -1; } // rotate(): circularly shift values (i.e., [ a, b, c ] becomes [ c, a, b ]) public void rotate() { } }