【发布时间】: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