ITI 1121 Midterm: ITI 1121 University of Ottawa 2004 Midterm-en

22 views15 pages

Document Summary

Introduction to computer science ii (csi 1101 a,b) You may not hand in additional pages: appendix a lists some of the methods of the classes string and character. This question is about a class to represent rational (fractional) numbers. // compares this object with the specified object for order. // returns a negative integer, zero, or a positive integer. // as this object is less than, equal to, or greater than the. If the denominator is 1 then the method returns a string consisting of the numerator only. Page 4 of 15 public class rational { // instance variables private int numerator; private int denominator; // instance method(s) public rational plus(rational other) { int newdenominator = denominator * other. denominator; int newnumerator = numerator * other. denominator; int newothernumerator = other. numerator * denominator; int sum = newnumerator + newothernumerator; return new rational(sum, newdenominator);