Algorithms and OOD (CSC 207 2013F) : EBoards

CSC207.01 2013F, Class 14: Inheritance, Continued



Questions on HW4

Can I store the numerator and denominator as BigInteger values?


Why doesn't my equals method work?

public boolean equals(Fraction other) {
    return this.numerator.equals(other.numerator) &&
} // equals(Fraction)

If you write an equals method with a signature of equals(Fraction other), the Java often doesen't know to call it. In general, it thinks it should call equals(Object other). So, you have to cast.

public boolean equals(Object other) {
    return this.equals((Fraction) other);
} // equals(Object)


public boolean equals(Object other) {
    return (other instanceof Fraction) && 
            (this.equals((Fraction) other));
} // equals(Object)

Why does the constructor for fractions fail?

I think I wrote something like if (this.denominator.equals(...)) without first asssigning to this.denominator. (Only some of you seem to have had this problem.)

We compare two fractions by subtracting one from the other and then return the numerator as converted to an integer. What do you think about this awesome approach?

Okay in theory, but ... overflow issues. Try using .signum() or use BigInteger.compareTo.

Can we assume that our procedures work?

No. But simple tests can test multiple procedures (e.g., constructor and toString().

What should evaluate return when given an assignment?

The value assigned.

Is this like C in which you can have beautiful stuff like r0 = r2 = r0 + 1?

No, you do not have to support that.

Q&A on Other Issues

How would you have solved the "separate strings" problem from HW3?

// Count how many times the separator appears
    // Repeatedly use indexOf
// Build the array of the correct size
// Step through the string (using indexOf), with substring


Continue Friday's lab.

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.