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 ...
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?
Is it like this?
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
Object-oriented is in the process of being gnawing. Please point out any shortages or mistakes.
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)
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