lots of exercises in java... from https://github.com/exercism/java

PythagoreanTriplet.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Objects;
  4. import java.util.stream.Collectors;
  5. public class PythagoreanTriplet {
  6. private int a;
  7. private int b;
  8. private int c;
  9. public PythagoreanTriplet(final int a, final int b, final int c) {
  10. this.a = a;
  11. this.b = b;
  12. this.c = c;
  13. }
  14. public static TripletListBuilder makeTripletsList() {
  15. return new TripletListBuilder();
  16. }
  17. public int calculateSum() {
  18. return this.a + this.b + this.c;
  19. }
  20. public long calculateProduct() {
  21. return this.a * this.b * this.c;
  22. }
  23. public boolean isPythagorean() {
  24. return this.a * this.a + this.b * this.b == this.c * this.c;
  25. }
  26. @Override
  27. public int hashCode() {
  28. return Objects.hash(a, b, c);
  29. }
  30. @Override
  31. public boolean equals(Object obj) {
  32. if (this == obj) {
  33. return true;
  34. }
  35. if (obj == null) {
  36. return false;
  37. }
  38. if (getClass() != obj.getClass()) {
  39. return false;
  40. }
  41. final PythagoreanTriplet other = (PythagoreanTriplet) obj;
  42. if (this.a != other.a) {
  43. return false;
  44. }
  45. if (this.b != other.b) {
  46. return false;
  47. }
  48. if (this.c != other.c) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. public static class TripletListBuilder {
  54. private static final int MAX_FACTOR_DEFAULT_VALUE = 0;
  55. private static final int SUM_DEFAULT_VALUE = -1;
  56. private int maxFactor = MAX_FACTOR_DEFAULT_VALUE;
  57. private int minFactor = 1;
  58. private int sum = SUM_DEFAULT_VALUE;
  59. public TripletListBuilder() {
  60. }
  61. public TripletListBuilder withFactorsLessThanOrEqualTo(
  62. final int maxFactor) {
  63. this.maxFactor = maxFactor;
  64. return this;
  65. }
  66. public TripletListBuilder withFactorsGreaterThanOrEqualTo(
  67. final int minFactor) {
  68. this.minFactor = minFactor;
  69. return this;
  70. }
  71. public TripletListBuilder thatSumTo(final int sum) {
  72. this.sum = sum;
  73. return this;
  74. }
  75. public List<PythagoreanTriplet> build() {
  76. List<PythagoreanTriplet> triplets = new ArrayList<>();
  77. if (this.maxFactor == MAX_FACTOR_DEFAULT_VALUE) {
  78. return triplets;
  79. }
  80. double sqrt;
  81. int floor;
  82. for (int n = minFactor; n < maxFactor; n++) {
  83. for (int m = n + 1; m < maxFactor; m++) {
  84. sqrt = Math.sqrt(n * n + m * m);
  85. floor = (int) Math.floor(sqrt);
  86. if (sqrt == floor) {
  87. triplets.add(new PythagoreanTriplet(m, n, floor));
  88. }
  89. }
  90. }
  91. if (sum != SUM_DEFAULT_VALUE) {
  92. return triplets.stream()
  93. .filter(t -> t.calculateSum() == sum)
  94. .collect(Collectors.toList());
  95. }
  96. return triplets;
  97. }
  98. }
  99. }