【问题标题】:Classes I created aren't be called in main program (Java)我创建的类不在主程序(Java)中调用
【发布时间】:2015-06-11 14:21:57
【问题描述】:

好的,简而言之,我有 3 个 java 文件。第一个是主要的,另外两个是我试图在我的主要中使用的类。出于某种原因,当我通过右键单击包在 netbeans 中创建其他两个类并创建一个新类时,这些类似乎不属于主项目或“包”。换句话说,我在其他类中创建的方法并没有链接到我的主类。 “Point3d、translate、scale、translate、rotate、line3d 是我的主要错误的方法。任何帮助将不胜感激,谢谢。

头等舱

package ddunlap_3dcoordinates;
import java.util.*;
/**
*
* @author Aiden
*/
 public class DDunlap_3dCoordinates {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

int x, y, z;
    x = 2;
    y = 3;
    z = 4;
    Point3d pointA = new Point3d(x, y, z);

    //Let verify our starting conditions
    System.out.println("Initial Points");
    System.out.println(pointA.asString());
    System.out.println("");

    //Translate Test
    System.out.println("Translate all -1");
    pointA.translate(-1, -1, -1);
    System.out.println(pointA.asString());
    System.out.println("");

    //Scale Test
    System.out.println("Scale by 2");
    pointA.scale(2, 2, 2);
    System.out.println(pointA.asString());
    System.out.println("");

    //Rotate
    System.out.println("Rotate 180 with respect to x-axis");
    pointA.rotate(180, "x");
    System.out.println(pointA.asString());
    System.out.println("Rotate 180 with respect to y-axis");
    pointA.rotate(180, "y");
    System.out.println(pointA.asString());
    System.out.println("Rotate 180 with respect to z-axis");
    pointA.rotate(180, "z");
    System.out.println(pointA.asString());
    System.out.println("");

    //Resetting points
    pointA = null;
    pointA = new Point3d(x, y, z);
    Point3d pointB = new Point3d(0, 0, 0);

    Line3d lineAB = new Line3d(pointA, pointB);

    System.out.println("Line AB created");
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");

    //Scale line points
    System.out.println("Scale by 2");
    lineAB.scale(2, 2, 2);
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");

    //Translate by 1,1,1
    System.out.println("Translate by 1,1,1");
    lineAB.translate(1, 1, 1);
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");

    //Translate by -1.-1,-1
    System.out.println("Translate by -1,-1,-1");
    lineAB.translate(-1, -1, -1);
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");

    //Rotate
    System.out.println("Rotate 180 with respect to x-axis");
    lineAB.rotate(180, "x");
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");
    System.out.println("Rotate 180 with respect to y-axis");
    lineAB.rotate(180, "y");
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");
    System.out.println("Rotate 180 with respect to z-axis");
    lineAB.rotate(180, "z");
    System.out.println("Point A: " + lineAB.getA().asString());
    System.out.println("Point B: " + lineAB.getB().asString());
    System.out.println("");
    System.out.println("");

  }

 }

二等

  public class Point3d {
 private double x, y, z;

public Point3d(double x, double y, double z) {
    //Using this.variable will assign the value to the class.variable
    //This can be avoided by renaming the arguments above
    this.x = x;
    this.y = y;
    this.z = z;
}
//Mutator

void setX(double X) {
    x = X;

}

void sety(double Y) {
    y = Y;

}

void setZ(double X) {
    z = X;

}

void setPoints(double X, double Y, double Z) {
    x = X;
    y = Y;
    z = Z;
}
//Accessor

double getX() {
    return x;
}

double getY() {
    return y;
}

double getZ() {
    return z;
}

//Scale
void scale(double xFactor, double yFactor, double zFactor){
    x = x*xFactor;
    y = y*yFactor;
    z = z*zFactor;
}
//Translate
void translate(double xFactor, double yFactor, double zFactor){
    x += xFactor;
    y += yFactor;
    z += zFactor;

}
//Rotate
void rotate(double theta, String axis){
    //If invalid values are passed reject an notify user

    if(axis.isEmpty() || theta == 0 || axis.length() != 1){
        System.out.println("Error: Invalid args");
        return;
    }

    //Math.trigs expects radians! Convert degrees to radians
    theta = theta * (Math.PI/180);

    //Convert axis to lower
    axis = axis.toLowerCase();

    //x rotation
    if(axis.equals("x")){
        //x = x;
        y = (y * Math.cos(theta) - (z * Math.sin(theta)));
        z = (y * Math.sin(theta)) + (z * Math.cos(theta));
        return;
    }
    //y rotation
    if(axis.equals("y")){
        x = x * Math.cos(theta) + z * Math.sin(theta);
        //y = y;
        //0 minus x is -x
        z = (0-x) * Math.sin(theta) + z * Math.cos(theta);
        return;    
    }
    if(axis.equals("z")){
        x = x * Math.cos(theta) - y * Math.sin(theta);
        y = x * Math.sin(theta) + y * Math.cos(theta);
        //z = z;

    }


}


//Value retrieval for debugging
String asString(){
    return "X: " + x + " Y: " + y + " Z: " + z;

}
}

三等

  public class Line3d {
   Point3d lineA;
   Point3d lineB;

Line3d(Point3d a, Point3d b) {
    lineA = new Point3d(a.getX(), a.getY(), a.getZ());
    lineB = new Point3d(b.getX(), b.getY(), b.getZ());

}

void setA(Point3d a) {
    //Java has a garbage collector and you do not need to do this
    //but setting an object to null will close all references to the object
    //and allow Java GC to clean it up
    lineA = null;
    lineA = new Point3d(a.getX(), a.getY(), a.getZ());

}

void setB(Point3d b) {
    lineB = null;
    lineB = new Point3d(b.getX(), b.getY(), b.getZ());
}

//We already have an object to handle points, using the object will allow us to reuse code
Point3d getA() {

    return lineA;

}

Point3d getB() {

    return lineB;
}

void scale(double xFactor, double yFactor, double zFactor) {
    lineA.scale(xFactor, yFactor, zFactor);
    lineB.scale(xFactor, yFactor, zFactor);

}

void translate(double xFactor, double yFactor, double zFactor) {
    lineA.translate(xFactor, yFactor, zFactor);
    lineB.translate(xFactor, yFactor, zFactor);

}

void rotate(double degrees, String axis){
    lineA.rotate(degrees, axis);
    lineB.rotate(degrees, axis);
}
 }

【问题讨论】:

  • 您遇到的错误是秘密错误吗?或者编译器是否允许您与我们分享它们以便我们能够帮助您?
  • 它告诉我创建上面提到的方法。当我尝试编译时,我得到这个:线程“main”java.lang.RuntimeException中的异常:无法编译的源代码 - 类ddunlap_3dcoordinates.Point3d中的构造函数Point3d不能应用于给定类型;必需:未找到参数:int、int、int 原因:实际参数列表和正式参数列表在 ddunlap_3dcoordinates.DDunlap_3dCoordinates.main(DDunlap_3dCoordinates.java:23) 处的长度不同

标签: java configuration


【解决方案1】:

如果您对多个类有疑问,您可能需要考虑在github.com 上创建一个存储库,以便合作者可以看到您创建的文件结构。

使用 ide 时,您创建的代码将存在于您计算机上的 3 个位置。 1) src 文件夹的各种包中的纯文本。 2) 作为 bin 文件夹中的低级指令。 3) 在 ide 的对象堆栈上显示和操作代码。

现在您真正需要担心的只有一个,那就是 src。由于对象堆栈在您没有考虑的情况下就存在,并且在您构建/运行代码时构建了 bin。在您的 源代码(src) 中,您应该有子文件夹,这些是您的包。这些包中的类能够访问具有类名的同一包中的类以及具有更长地址的其他包中的类,例如:

.
├── pack1
│   ├── class1
│   └── class2
└── pack2
    └── class3

class1 的顶部可能有类似的东西

package pack1;
import class2;
import pack2.class3;

或者导入整个pack2

import pack2.*; 

看起来你所有的类都在一个文件中......这不是要走的路。面向对象设计的理念是代码自然地委托给类对象。让事情井井有条。一个中间的例子是我为一个班级准备的这个tetris 游戏。

【讨论】:

  • 所以你正在创建多个包并将其他 java 文件放在那里?该死的,我的教授当时真的把我弄糊涂了。他一直在教我做这件事的错误方法。
  • 将包视为章节,将类视为页面。 (在一本逐行阅读并贯穿章节的书中)您的代码中缺少导入语句引发了一些警钟......
【解决方案2】:
  1. 确保所有文件都在同一个包中。

2.我认为这是错误的

int x, y, z;
    x = 2;
    y = 3;
    z = 4;
    Point3d pointA = new Point3d(x, y, z);

你不能传递int类型的参数,它应该是double类型的参数。

【讨论】:

  • 好的,我会改变它。谢谢。
  • Java 自动为您将int 转换为double。这不是问题。
【解决方案3】:

我在 NetBeans 中遇到了类似的错误。确保将源文件添加到您的项目中。然后,在Project Properties -> Build -> Compiling 中禁用 Compile On Save。

来自here。

【讨论】:

  • 我禁用了 Compile On Save 选项,但是如何导入类?
【解决方案4】:

尝试将文件放在同一个文件夹中,并在驱动程序 java 代码(包含 main 函数)中导入类,然后尝试再次运行代码。

【讨论】:

  • 我以为你只能导入包。你如何导入一个类?我还没有被教过。抱歉,我是 Java 新手。
  • 天哪。我将所有文件移动到同一个文件夹,现在它可以工作了。哈哈。这太愚蠢了,但谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-06-28
  • 2023-02-26
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多