Skip to content

Implement new exercise rational-numbers - #1274

Merged
FridaTveit merged 25 commits into
exercism:masterfrom
clementi:master
Feb 7, 2018
Merged

FridaTveit merged 25 commits into
exercism:masterfrom
clementi:master

Conversation

@clementi

Copy link
Copy Markdown

Reviewer Resources:

Track Policies

@sjwarner-bp sjwarner-bp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in really good shape so far!

I've left a few comments 🙂


// Tests

@Test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This README looks a little incomplete.

If you use the configlet tool, it will generate the README for you. Normally for our track, we have a couple of added bits that configlet will automatically pull in.

The script should pull down configlet for you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great; I didn't know about that. I'll check it out.

return (mask ^ x) - mask;
}

public Rational add(Rational that) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread config.json Outdated
"topics": [
"floating_point_numbers",
"mathematics",
"classes"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) 🙂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me - thanks 🙂

@FridaTveit FridaTveit left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IllegalArgumentException is an unchecked exception therefore you don't have to declare it in the method signature 🙂

}

@Test
@Ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be package private 🙂

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I just have one question about the topics and I think you still have to add a version file 🙂

Comment thread config.json Outdated
"topics": [
"floating_point_numbers",
"mathematics",
"classes",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look like good topics 🙂 But I'm not sure about floating_point_numbers and classes, how are they relevant to this exercise?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think classes has recently been removed as a topic of complex-numbers 🙂

@clementi

clementi commented Feb 6, 2018 •

Copy link
Copy Markdown
Author

New commits:

  • Add version file
  • Remove "classes" from topics
  • Stub out Rational with equals and toString
  • Use assertEquals in tests

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make an issue of it.

@sjwarner-bp

Copy link
Copy Markdown
Contributor

Opened #1296

@FridaTveit
FridaTveit merged commit 71a1fcb into exercism:master Feb 7, 2018
hgvanpariya pushed a commit to hgvanpariya/java that referenced this pull request May 13, 2018
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants