Repository navigation
Implement new exercise rational-numbers - #1274
Conversation
sjwarner-bp
left a comment
There was a problem hiding this comment.
This is in really good shape so far!
I've left a few comments 🙂
|
|
||
| // Tests | ||
|
|
||
| @Test |
There was a problem hiding this comment.
As per our POLICIES, only the first test should be used by default. All other tests should be @Ignore, in order to allow users to build up a solution.
This occurs a few times here, but I won't leave a comment on all of the exercises.
| @@ -0,0 +1,27 @@ | |||
| A rational number is defined as the quotient of two integers `a` and `b`, called the numerator and denominator, respectively, where `b != 0`. | |||
There was a problem hiding this comment.
That's great; I didn't know about that. I'll check it out.
| return (mask ^ x) - mask; | ||
| } | ||
|
|
||
| public Rational add(Rational that) { |
There was a problem hiding this comment.
There's a good few methods here that have a higher wider access modifier than they need to.
On the track here, we try to adhere to best practise as noted in our POLICIES.
This (and a few more) can be changed from public to package-private.
| "topics": [ | ||
| "floating_point_numbers", | ||
| "mathematics", | ||
| "classes" |
There was a problem hiding this comment.
What is your reasoning for classes?
If you couldn't find it (I always have trouble locating it) the list of topics is super useful here!
There was a problem hiding this comment.
@sjwarner-bp I saw that the topics of complex-numbers, which is similar to this problem, has classes. That's all. I'll take a look at that list.
There was a problem hiding this comment.
It's not the easiest list to find! 😆 Your suggestions seem sensible so far, but feel free to add anything else you think might be suitable!
|
|
||
| // Helper methods | ||
|
|
||
| private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15; |
There was a problem hiding this comment.
This seems like a sensible value for a tolerance.
I haven't run this locally yet - is the tolerance necessary? (I can see why it might be) 🙂
There was a problem hiding this comment.
It may be, depending on the algorithm used to compute double pow(double x) or double exp(double base). I included it because the complex-numbers project has it, and I thought it might be needed for these methods.
There was a problem hiding this comment.
That makes sense to me - thanks 🙂
There was a problem hiding this comment.
This looks great @clementi! I only have a couple of small comments 🙂
Also please add a version file as described in the section on adding a new exercise in our contributing guide 🙂
| private int numerator; | ||
| private int denominator; | ||
|
|
||
| Rational(final int numerator, final int denominator) throws IllegalArgumentException { |
There was a problem hiding this comment.
IllegalArgumentException is an unchecked exception therefore you don't have to declare it in the method signature 🙂
| } | ||
|
|
||
| @Test | ||
| @Ignore |
There was a problem hiding this comment.
As described in our POLICIES doc this should be @Ignore("Remove to run test") and it should be above the @Test annotation 🙂
|
|
||
| private void assertRationalsEqual(Rational r1, Rational r2) { | ||
| assertEquals(r1.getNumerator(), r2.getNumerator()); | ||
| assertEquals(r1.getDenominator(), r2.getDenominator()); |
There was a problem hiding this comment.
Will this give a good error message if the test fails? That is will it clearly tell the user that it's the denominator or the numerator that's wrong or will it just say something like Expected 5 but got 2?
There was a problem hiding this comment.
@FridaTveit It won't, no. It looks like this:
RationalTest > testAddTwoPositiveRationalNumbers FAILED
java.lang.AssertionError: expected:<9> but was:<6>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at RationalTest.assertRationalsEqual(RationalTest.java:18)
at RationalTest.testAddTwoPositiveRationalNumbers(RationalTest.java:27)
I cribbed the assertRationalsEqual helper method from the assertComplexNumbersEqual method in ComplexNumberTest.java in complex-numbers. Would you like it to be more specific? I suppose we could implement equals and maybe toString on Rational, or require the exercise-taker to do it. That way, we could get better error messages.
There was a problem hiding this comment.
You could implement equals, that would help. In which case I would add some starter implementation with equals already implemented since that's not really part of the exercise. Or you could just add a message to your equals check saying something like Incorrect numerator. I think either would be fine 🙂
You're right, it is the same in complex-numbers, thanks for pointing it out! I'll open an issue to improve the error message for complex-numbers 🙂
| @@ -0,0 +1,102 @@ | |||
| public class Rational { | |||
FridaTveit
left a comment
There was a problem hiding this comment.
This is looking great! I just have one question about the topics and I think you still have to add a version file 🙂
| "topics": [ | ||
| "floating_point_numbers", | ||
| "mathematics", | ||
| "classes", |
There was a problem hiding this comment.
These look like good topics 🙂 But I'm not sure about floating_point_numbers and classes, how are they relevant to this exercise?
There was a problem hiding this comment.
@FridaTveit Well, this exercise is similar to complex-numbers, and that exercise has those topics. Floating-point numbers (doubles) are involved in some of the methods of this exercise, and this exercise does require that one create a class with some data hiding. But if you don't think this exercise warrants those topics, I'll remove them.
There was a problem hiding this comment.
I think classes has recently been removed as a topic of complex-numbers 🙂
|
New commits:
|
FridaTveit
left a comment
There was a problem hiding this comment.
This looks great @clementi! Thanks for taking the time to do this! 🙂
I only have one minor comment and it can be pulled out into a separate issue if you would rather get this merged straight away 🙂
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { |
There was a problem hiding this comment.
One small thing: if you're overriding equals then you should also override hashCode (see this stackoverflow question for an explanation of why 🙂 ). But you've done a lot of great work on this PR already so if you'd rather just get it merged then I can open a new issue for overriding hashCode 🙂
There was a problem hiding this comment.
I think we can make an issue of it.
|
Opened #1296 |
* Initial * Extract certain logic to methods * Implement and add tests * Use 'One' and 'Zero' in test method names instead of '1' and '0' * Change spacing * Add remaining tests * Fix indentation * Use better method for calculating rth root of x * Move reference implementation to .meta * Include exercise in settings.gradle * Add README * Add reference to config.js * Add UUID and unlocked-by * Make methods package-private; Ignore all but first test * Add 'algorithms' to topics because of gcd * Give @ignores the proper message and put in the proper order * Generate README using configlet * Add version file * Remove 'classes' from topics * Make class package private * Make class package private * Use equals method and assertEquals (and toString) for comparing Rationals * Stub out Rational with equals and toString * Stub out methods * Remove comment
Reviewer Resources:
Track Policies