系统要求我传递更多参数以匹配我的构造函数,但我不知道该将什么传递给它们。
我有多个实例变量,但是只有少数几个实例变量将由用户(顶点)定义,其他实例变量将使用各自的方法进行定义。如果我将除顶点以外的所有内容都用在构造函数之外,以解决该错误,那么大多数报告的最终输出将保留为0。
构造函数是问题还是对象中的参数有问题?
import java.lang.Math;
public class Triangle {
//instance variables
private double VertAx, VertAy, VertBx, VertBy, VertCx, VertCy;
private double lengthAB, lengthBC, lengthCA;
private double Perimeter, Area;
private double H = Perimeter/2;
//Triangle Constructor
public Triangle(double userVertAx, double userVertAy, double userVertBx, double userVertBy, double userVertCx, double userVertCy, double userlengthAB, double userlengthBC, double userlengthCA, double userPerimeter, double userArea, double userH) {
userVertAx = this.VertAx;
userVertAy = this.VertAy;
userVertBx = this.VertBx;
userVertBy = this.VertBy;
userVertCx = this.VertCx;
userVertCy = this.VertCy;
userlengthAB = this.lengthAB;
userlengthBC = this.lengthBC;
userlengthCA = this.lengthCA;
userPerimeter = this.Perimeter;
userArea = this.Area;
userH = this.H;
}
public double lengthAB(double userVertAx, double userVertAy, double userVertBx, double userVertBy) {
return lengthAB = Math.sqrt( (Math.pow((userVertBx - userVertAx), 2)) + (Math.pow((userVertBy - userVertAy), 2)));
}
public double lengthBC(double userVertBx, double userVertBy, double userVertCx, double userVertCy) {
return lengthBC = Math.sqrt( (Math.pow((userVertCx - userVertBx), 2)) + (Math.pow((userVertCy - userVertBy), 2)));
}
public double lengthCA(double userVertCx, double userVertCy, double userVertAx, double userVertAy) {
return lengthCA = Math.sqrt( (Math.pow((userVertAx - userVertCx), 2)) + (Math.pow((userVertAy - userVertCy), 2)));
}
public void setPerimeter(double userlengthAB, double userlengthBC, double userlengthCA) {
Perimeter = userlengthAB + userlengthBC + userlengthCA;
}
public double getPerimeter() {
return Perimeter;
}
public void setArea(double userlengthAB, double userlengthBC, double userlengthCA, double userH) {
Area = Math.sqrt(userH*(userH-userlengthAB)*(userH-userlengthBC)*(userH-userlengthCA));
}
public double getArea() {
double Area = getArea();
return Area;
}
public String toString() {
return String.format("Vertices: A(%f, %f) B(%f, %f) C(%f, %f)\nSide Lengths: AB=%f BC=%f CA=%f\nPerimeter: %f\nArea: %f", VertAx, VertAy, VertBx, VertBy, VertCx, VertCy, lengthAB, lengthBC, lengthCA, Perimeter, Area);
}
}
public class TriangleTest {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner Vertices = new Scanner(System.in);
System.out.println("Welcome to the Triangle Test enter each coordinate of your three vertices SEPERATELY");
System.out.println("Enter Vertex A X");
Double VAX = Vertices.nextDouble();
System.out.println("Enter Vertex A Y");
Double VAY = Vertices.nextDouble();
System.out.println("Enter Vertex B X");
Double VBX = Vertices.nextDouble();
System.out.println("Enter Vertex B Y");
Double VBY = Vertices.nextDouble();
System.out.println("Enter Vertex C X");
Double VCX = Vertices.nextDouble();
System.out.println("Enter Vertex C Y");
Double VCY = Vertices.nextDouble();
//ERROR
Triangle UserTriangle = new Triangle(VAX, VAY, VBX, VBY, VCX, VCY);
//ERROR ^
UserTriangle.lengthAB(VAX, VAY, VBX, VBY);
UserTriangle.lengthBC(VBX, VBY, VCX, VCY);
UserTriangle.lengthCA(VCX, VCY, VAX, VAY);
UserTriangle.getPerimeter();
UserTriangle.getArea();
System.out.println(UserTriangle.toString());
}
}
我期望通过某种方式将正确的参数传递到我的UserTriangle
中,但是我对如何使用感到困惑。感谢您提供任何帮助。我对类和对象的理解很好,可以实现用户输入,但是考虑到某些变量是在方法中定义的,而有些是由用户定义的,所以这对我来说似乎很棘手。
答案 0 :(得分:1)
您将构造函数定义为接受 12个参数,但是随后仅使用 6个参数对其进行了调用。这是您所指的错误。为了解决这个问题,您有3种选择
要初始化属性,请写this.VertAx = userVertAx
而不是userVertAx = this.VertAx;
(基本上颠倒该语句)
这也适用于所有其他属性(userlengthAB,userPerimeter等)
最好使用Java naming conventions,这样您就可以说出属性和类之间的区别。属性和变量应以小写开头,而类应以大写开头。
更好的写法是在构造函数中使用更少的参数。过多的参数被认为是code smell,会使您的代码可读性/可维护性降低,等等。
要处理此问题,可以将一些概念封装在类中。例如,您可以拥有
public class Vertex {
private double x;
private double y;
public Vertex(double x, double y) {
this.x = x;
this.y = y;
}
public class TriangleVertices {
private vertexA;
private vertexB;
private vertexC;
public TriangleVertices (Vertex a, Vertex b, Vertex c) {
vertexA = a;
vertexB = b;
vertexC = c;
}
}
public class Triangle {
private TriangleVertices vertices;
// other attributes
// You have now 5 arguments less!
public Triangle(TriangleVertices vertices, // other attributes) {
this.vertices = vertices;
// Initialize other attributes
}
}
答案 1 :(得分:0)
这里的“ this”是一个关键字,它将构造函数指向在创建的类中声明的变量(在您的情况下,创建的类是Triangle)。我们在使用“ this”关键字时 参数的变量与在创建的类中声明的变量的名称相同。例如;
class A {
int NewVar;
A (double NewVar){
this.NewVar = NewVar; //here this.NewVar is pointing to NewVar of type int
}
}
在您的代码中进行更改。这可能会解决您的问题。
public Triangle(double userVertAx, double userVertAy, double userVertBx, double userVertBy, double userVertCx, double userVertCy, double userlengthAB, double userlengthBC, double userlengthCA, double userPerimeter, double userArea, double userH) {
this.VertAx = userVertAx;
this.VertAy = userVertAy;
this.VertBx = userVertBx;
this.VertBy = userVertBy;
this.VertCx = userVertCx;
this.VertCy = userVertCy;
this.lengthAB = userlengthAB;
this.lengthBC = userlengthBC;
this.lengthCA = userlengthCA ;
this.Perimeter = userPerimeter ;
this.Area = userArea;
this.H = userH ;
}