[英]Is there a way I can reuse part of my code that does the same thing but access different attributes of a class in Java?
So I was doing a program there's supposed to verify if when cut, a triangle can produce two new triangles, I did all the logic and the code works just as it is supposed to, however as I was doing I noticed that I had to apply the same logic for different attributes over and over again, which seemed highly unproductive.所以我正在做一个程序,应该验证切割时三角形是否可以产生两个新三角形,我完成了所有逻辑并且代码按预期工作,但是当我这样做时我注意到我必须应用一遍又一遍地为不同的属性使用相同的逻辑,这似乎非常没有效率。 Here's the code:
这是代码:
public static class Triangle{
private int[] x = new int[3];
private int[] y = new int[3];
public int getX(int position) {
return x[position];
}
public int getY(int position) {
return y[position];
}
//Get the information regarding the points that make the triangle
public void setDomains(Scanner readVar){
readVar.nextLine();
this.x[0] = readVar.nextInt();
this.y[0] = readVar.nextInt();
this.x[1] = readVar.nextInt();
this.y[1] = readVar.nextInt();
this.x[2] = readVar.nextInt();
this.y[2] = readVar.nextInt();
}
//Verify if one of the other points is lower or higher than the present one
public boolean areDiffX(int i, int j, boolean isLower){
if(isLower){
if(this.getX(i) < this.getX(j)){
return true;
};
}else{
if(this.getX(i) > this.getX(j)){
return true;
};
}
return false;
}
//Verify if one of the other points is lower or higher than the present one
public boolean areDiffY(int i, int j, boolean isLower){
if(isLower){
if(this.getY(i) < this.getY(j)){
return true;
};
}else{
if(this.getY(i) > this.getY(j)){
return true;
};
}
return false;
}
//Verify if from the X axes two new triangles can ensurge
public boolean compareTriangleDomain(int i, int j, int k){
boolean hasDomain = false;
boolean hasCodomain = false;
hasDomain = areDiffX(i, j, true);
hasCodomain = areDiffX(i, k, false);
if(hasDomain == true && hasCodomain == true){
return true;
}
hasDomain = areDiffX(i, j, false);
hasCodomain = areDiffX(i, k, true);
if(hasDomain == true && hasCodomain == true){
return true;
}
return false;
}
//Verify if from the Y axes two new triangles can ensurge
public boolean compareTriangleCodomain(int i, int j, int k){
boolean hasDomain = false;
boolean hasCodomain = false;
hasDomain = areDiffY(i, j, true);
hasCodomain = areDiffY(i, k, false);
if(hasDomain == true && hasCodomain == true){
return true;
}
hasDomain = areDiffY(i, j, false);
hasCodomain = areDiffY(i, k, true);
if(hasDomain == true && hasCodomain == true){
return true;
}
return false;
}
//Verify if from a certain point of a triangle can be produced two new triangles
public boolean cutTriangle(int i, int j, int k){
boolean halveTriangleX = compareTriangleDomain(i, j, k);
boolean halveTriangleY = compareTriangleCodomain(i, j, k);
if(halveTriangleX || halveTriangleY){
return true;
}
return false;
}
//Verify if a triangle can produce two new triangles
public void checkCut(){
boolean canCut = false;
boolean hasntCut = true;
canCut = cutTriangle(0, 1, 2);
if(canCut == true && hasntCut == true){
System.out.println("YES");
hasntCut = false;
}
canCut = cutTriangle(1, 0, 2);
if(canCut == true && hasntCut == true){
System.out.println("YES");
hasntCut = false;
}
canCut = cutTriangle(2, 1, 0);
if(canCut == true && hasntCut == true){
System.out.println("YES");
}
else if(hasntCut == true){
System.out.println("NO");
}
}
}
I thought that maybe I could add a new parameter to the get function so to specify which axes I'm working with, but I don't know if this a dumb way to do it, any recommendations and advices are welcome I'm still a baby in java and coding in general.我想也许我可以向 get 函数添加一个新参数,以便指定我正在使用的轴,但我不知道这是否是一种愚蠢的方法,欢迎任何建议和建议我仍然Java 和一般编码的婴儿。
There are a few ways to do this.有几种方法可以做到这一点。 I would go with simply writing a function which takes the values instead of the indices, and then two new functions which get the values:
我会简单地编写一个函数来获取值而不是索引,然后是两个获取值的新函数:
public boolean areDiff(int a, int b, boolean isLower){
if (isLower) {
if (a < b) {
return true;
}
} else {
if (a > b) {
return true;
}
}
return false;
}
and then:接着:
public boolean areDiffX(int i, int j, boolean isLower){
return areDiff(this.getX(i), this.getX(j), isLower);
}
and a similar areDiffY
function.和一个类似的
areDiffY
函数。
You can rewrite compareTriangleDomain
to take values and use areDiff
and dispense with compareTriangleCodomain
and areDiffX/Y
.您可以重写
compareTriangleDomain
以获取值并使用areDiff
并免除compareTriangleCodomain
和areDiffX/Y
。
Perhaps I dug too deep, but I want to suggest the following approach.也许我挖得太深了,但我想建议以下方法。
The triangle obviously consists of three points on the plane.三角形显然由平面上的三个点组成。 Why not make a class to represent the point?
为什么不做一个类来代表点呢? I will use the
record
syntax that appeared in Java 14. I also add two methods for comparing points and two comparators.我将使用 Java 14 中出现的
record
语法。我还添加了两个比较点的方法和两个比较器。 One allows you to compare points by x
coordinate, the second by y
.一个允许您通过
x
坐标比较点,第二个允许您通过y
比较点。
import java.util.Comparator;
import java.util.function.ToIntFunction;
public record Point2D(int x, int y) {
private static Comparator<Point2D> compareBy(ToIntFunction<Point2D> keyExtractor) {
return (o1, o2) -> Comparator.comparingInt(keyExtractor).compare(o1, o2);
}
public static Comparator<Point2D> byX = compareBy(Point2D::x);
public static Comparator<Point2D> byY = compareBy(Point2D::y);
public boolean less(Point2D other, Comparator<Point2D> comp) {
return comp.compare(this, other) < 0;
}
public boolean higher(Point2D other, Comparator<Point2D> comp) {
return comp.compare(this, other) > 0;
}
}
Now we can greatly simplify the Triangle
class.现在我们可以大大简化
Triangle
类。 The triangle is represented by an array of three points instead of six integers.三角形由三个点而不是六个整数的数组表示。
private final Point2D[] vertices = new Point2D[3];
public void setDomains(Scanner readVar) {
readVar.nextLine();
this.vertices[0] = new Point2D(readVar.nextInt(), readVar.nextInt());
this.vertices[1] = new Point2D(readVar.nextInt(), readVar.nextInt());
this.vertices[2] = new Point2D(readVar.nextInt(), readVar.nextInt());
}
And all other methods look like this所有其他方法看起来像这样
public boolean compareTriangle(int i, int j, int k, Comparator<Point2D> comp) {
boolean hasDomain;
boolean hasCodomain;
hasDomain = vertices[i].less(vertices[j], comp);
hasCodomain = vertices[i].higher(vertices[k], comp);
if (hasDomain && hasCodomain) {
return true;
}
hasDomain = vertices[i].higher(vertices[j], comp);
hasCodomain = vertices[i].less(vertices[k], comp);
return hasDomain && hasCodomain;
}
public boolean cutTriangle(int i, int j, int k) {
boolean halveTriangleX = compareTriangle(i, j, k, Point2D.byX);
boolean halveTriangleY = compareTriangle(i, j, k, Point2D.byY);
return halveTriangleX || halveTriangleY;
}
I have not tested this code (in my opinion it is identical to yours), so you should definitely check it out.我没有测试过这段代码(我认为它与您的代码相同),因此您一定要检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.