[JAVA] Let's try the implementation to find the area of the triangle that we did in the training for newcomers

Two years have passed since I started programming

It's been almost two years since I first programmed in Java training at a company I joined as a new graduate. Recently, I have more opportunities to talk with information students, and I often realize that there are many amazing people. Looking back on what I was doing in the training for newcomers, I had doubts ...

What I was doing at that time was object-oriented?

One of the training tasks was to find the area of the triangle. The idea was to implement it using classes rather than writing it procedurally. So, I think the answer I came up with was ⬇️ like this.

class Triangle {
  int bottom;
  int height;
  
  public Triangle(int arg1, int arg2) {
    this.bottom = arg1;
    this.height = arg2;
  }

  public int calc() {
    return this.bottom * this.height / 2;
  }
}

I think the model answer was like this. Maybe it's fine. (Satisfactory)





Wait a minute: thinking: Is the triangle represented by this class like this? スクリーンショット 2020-03-27 16.52.11.png

Is it like this? スクリーンショット 2020-03-27 16.48.18.png

Hmmm, it's not a triangle ...





But it's okay! !! !! It would be nice if we at that time defined the combination of base and height as a triangle`! maybe

However, since the text at that time had a solid triangular figure, the creator must be thinking about this. By the way

Try to re-challenge the problem at that time in an object-oriented manner

Object-oriented is in the process of being gnawing. Please point out any shortages or mistakes.

Define a triangle

Since the only information given as an assignment is ** base ** and ** height **, let's assume that the triangle is a right triangle with these two sides as the sides. (Please let me)

Try to implement

public class RightTriangle {
    private Set<Side> sideSet;

    public RightTriangle(Side bottom, Side height) {
        assert !bottom.isHypotenuse();
        assert !height.isHypotenuse();

        sideSet = Set.of(bottom, height, calcHypotenuse(bottom, height));
    }

    private Side calcHypotenuse(Side bottom, Side height) {
        return Side.createHypotenuse(Math.hypot(bottom.length(), height.length()));
    }

    public double area() {
        return sideSet.stream()
          .filter(s -> !s.isHypotenuse())
          .map(Side::length)
          .reduce(1.0, (s1, s2) -> s1 * s2) / 2;
    }
}

public class Side {
    private boolean isHypotenuse;
    private double value;

    private Side(double value, boolean isHypotenuse) {
        assert value > 0;
        this.value = value;
        this.isHypotenuse = isHypotenuse;
    }

    public static Side createSide(double value) {
        return new Side(value, false);
    }

    public static Side createHypotenuse(double value) {
        return new Side(value, true);
    }

    public boolean isHypotenuse() { return this.isHypotenuse; }

    public double length() { return this.value; }
}

I tried to find the hypotenuse from two sides when initializing the triangle class. I think this will be the triangle you imagine. I expressed the side like a value object and gave it the state of a hypotenuse. When you ask the triangle class for the area, it will calculate and return from the two sides that are not the hypotenuse.

The length of the side may have been cut out as a Length class. The initialization process of the triangle class and edge class may not be cool. It may not be cool in the first place. This is the best I can do now ... Let's try again two years later: rice_ball:

If you have any suggestions: bow_tone1: I want to understand object orientation

Recommended Posts

Let's try the implementation to find the area of the triangle that we did in the training for newcomers
The story of Collectors.groupingBy that I want to keep for posterity
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
This and that of the implementation of date judgment within the period in Java
How to find the total number of pages when paging in Java
[Introduction to Computer Science Part 1: Let's try machine learning] Let's implement k-means clustering in Java-About the concept of coordinates-
The story of intentionally using try catch for the first time in my life
[Java] Where is the implementation class of annotation that exists in Bean Validation?
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
[Swift, a must-see for fledgling! ] Let's understand the implementation of communication processing of WebAPI
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android