/* * Class definition which can be used to * create instances of RomanNumeral objects. * * CS112 * * Christine Papadakis-Kanaris */ public class RomanNumeral { // declare the fields (data members) // of the class private int numericalValue; private String stringValue; // Constructor public RomanNumeral(String stringValue) { this.stringValue = stringValue; // Note: // // Since we had already written a method to // convert a roman numeral string into // its decimal equivalent in our static // class, we can use it here and // do not need to duplicate the code // in this class. // this.numericalValue = RomanNumeralStatic.convert(stringValue); } // // Accessor method for the numerical value // // Note that we don't really need one for the string value // since we can obtain it using the toString method. // public int getNumericalValue() { return this.numericalValue; } // // Other methods of the class // public String toString() { return this.stringValue; } public boolean equals(RomanNumeral other) { return (this.numericalValue == other.numericalValue); } public int add(RomanNumeral other) { return this.numericalValue + other.numericalValue; } /* * Local main method to test * the methods of our class. */ public static void main(String[] args) { RomanNumeral x = new RomanNumeral("X"); RomanNumeral y = new RomanNumeral("IX"); System.out.println(x + " + " + y + " = " + x.add(y)); System.out.print( "Testing equality, the objects are " ); if ( !x.equals(y) ) System.out.print( "not " ); System.out.println( "equal!" ); } } // end of class definition