Java I';我的几何程序有问题
我刚刚开始用Java编写自己的类和方法,所以请原谅我的一些可能过于拥挤和不必要的代码。我应该使用以下方法编写一个名为OrderedPair的类:reflectX、reflectY、translate、rotate90、getQuadrant、getOrigPt、Explate和toString reflectX和reflectY方法故意重载,因为我的老师希望我们在程序中实现该技术 我的问题是我的rotate90方法不起作用。例如,当我旋转点(3,-8)4次(应该返回原始点)时,我得到的是(-8,-8)。此外,当我打印toString方法时,新点的新象限不正确。这是我的密码:Java I';我的几何程序有问题,java,geometry,Java,Geometry,我刚刚开始用Java编写自己的类和方法,所以请原谅我的一些可能过于拥挤和不必要的代码。我应该使用以下方法编写一个名为OrderedPair的类:reflectX、reflectY、translate、rotate90、getQuadrant、getOrigPt、Explate和toString reflectX和reflectY方法故意重载,因为我的老师希望我们在程序中实现该技术 我的问题是我的rotate90方法不起作用。例如,当我旋转点(3,-8)4次(应该返回原始点)时,我得到的是(-8,
public class OrderedPair{
private double original_x;
private double original_y;
private double x_value;
private double y_value;
private int original_quadrant;
private int quadrant;
public OrderedPair (double x, double y, int q){
original_x = x;
original_y = y;
x_value = x;
y_value = y;
original_quadrant = q;
quadrant = q;
}
public void reflectX (){
y_value = -y_value;
}
public void reflectX (double value){
double reflect = value - x_value;
if (reflect > 0)
x_value += 2*reflect;
else
x_value -= 2*reflect;
}
public void reflectY (){
x_value = -x_value;
}
public void reflectY (double value){
double reflect = value - y_value;
if (reflect > 0)
y_value += 2*reflect;
else
x_value -= 2*reflect;
}
public void translate (double translateX, double translateY){
x_value += translateX;
y_value += translateY;
}
public void rotate90 (int numOfRotations){
for (int rotate = 1; rotate <= numOfRotations; rotate++){
x_value = -y_value;
y_value = x_value;
}
}
public void dilate (double dilate_value){
x_value *= dilate_value;
y_value *= dilate_value;
}
public int getQuadrant(){
if (x_value>=0)
{
if (y_value >= 0)
{
quadrant = 1;
return quadrant;
}
else
{
quadrant = 4;
return quadrant;
}
}
else
{
if (y_value >= 0)
{
quadrant = 2;
return quadrant;
}
else
{
quadrant = 3;
return quadrant;
}
}
}
public String getOrigPt(){
return "( " + original_x + ", " + original_y + ")";
}
public String toString(){
return "( " + original_x + ", " + original_y + "); " + original_quadrant + "; " + "( " + x_value + ", " + y_value + "); " + quadrant;
}
}
公共类OrderedPair{
私人双原件;
私人双原件;
私人双x_值;
私人双y_值;
私密的int-u象限;
私有整数象限;
公共订单对(双x,双y,整数q){
原始_x=x;
原始_y=y;
x_值=x;
y_值=y;
原始象限=q;
象限=q;
}
公共无效反射X(){
y_值=-y_值;
}
公共无效反射X(双值){
双反射=值-x_值;
如果(反射>0)
x_值+=2*反映;
其他的
x_值-=2*反映;
}
公共无效反射(){
x_值=-x_值;
}
公共无效反射(双值){
双反射=值-y_值;
如果(反射>0)
y_值+=2*反映;
其他的
x_值-=2*反映;
}
public void translate(双translateX,双translateY){
x_值+=translateX;
y_值+=translateY;
}
公共空间旋转90(整数旋转){
对于(int rotate=1;rotate=0)
{
如果(y_值>=0)
{
象限=1;
返回象限;
}
其他的
{
象限=4;
返回象限;
}
}
其他的
{
如果(y_值>=0)
{
象限=2;
返回象限;
}
其他的
{
象限=3;
返回象限;
}
}
}
公共字符串getOrigPt(){
返回“(“+original_x+”,“+original_y+”)”;
}
公共字符串toString(){
返回“(“+original_x+”,“+original_y+”);“+original_quadrant+”;“+”(“+x_value+”,“+y_value+”);“+quadrant;
}
}
如果有人能帮忙,那就太好了 在
旋转90
中,循环体的的第一行
x_value = -y_value;
覆盖x_值
,因此它在第二行中不再可用
y_value = x_value; // x_value has already been changed
这有效地将y\u值设置为与自身相反的值。使用临时变量,以免丢失旧值
double old_x = x_value;
x_value = -y_value;
y_value = old_x;
象限确定看起来是正确的,但它依赖于正确的x和y值。更正rotate90
方法还应更正getQuadrant
方法的输出。在rotate90
中,仔细查看赋值顺序。y\u value
在赋值中有什么值?在public void reflectY(double value)
方法中,else语句中可能有错误。不像一个错误,一个错误。只是一个逻辑错误。谢谢你抓住了我!谢谢,你们说得对。我完全没有意识到这一点。
public void rotate90 (int numOfRotations){
for (int rotate = 1; rotate <= numOfRotations; rotate++){
x_value = -y_value;
y_value = x_value;
}
}
double x_value_cache = x_value;
x_value = -y_value;
y_value = x_value_cache;