我创建的类不在主程序中调用(Java)

时间:2015-04-07 04:47:00

标签: java configuration

好的,为了简短起见,我有3个java文件。第一个是主要的,另外两个是我试图在我的主要内部使用的类。出于某种原因,当我通过右键单击包并创建一个新类来创建netbeans中的其他两个类时,这些类似乎不是主项目或"包的一部分。 #34;换句话说,我在其他课程中创建的方法并没有链接到我的主要课程。 " 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);
}
 }

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我在NetBeans中遇到了类似的错误。确保将源文件添加到项目中。然后,在<{1}}中停用 保存时编译

来自here

答案 2 :(得分:0)

如果您对多个类有疑问,可以考虑在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游戏。

答案 3 :(得分:-1)

  1. 确保您的所有文件都在同一个包中。
  2. 2.我认为这是错误的

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

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