Reputation: 43
I've just started writing my own classes and methods in Java, so pardon some of my perhaps overcrowded and unnecessary code. I am supposed to write a class called OrderedPair utilizing the following methods: reflectX, reflectY, translate, rotate90, getQuadrant, getOrigPt, dilate, and toString.
The reflectX and reflectY methods are purposely overloaded because my teacher wanted us to implement the technique in the program.
My problem is that my rotate90 method isn't working. When I rotate, for example, the point (3,-8) 4 times (which should return the original point), I instead get (-8,-8). Also, when I print the toString method, the new quadrant for the new point is incorrect. Here's my code:
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;
}
}
If anyone could help, that would be great!
Upvotes: 3
Views: 80
Reputation: 221
public void rotate90 (int numOfRotations){
for (int rotate = 1; rotate <= numOfRotations; rotate++){
x_value = -y_value;
y_value = x_value;
}
}
You're overriding the x_value with -y_value and after that you set y_value = x_value. So your result is x_value=-y_value and y_value = -y_value
use a local variable to cache x_value:
double x_value_cache = x_value;
x_value = -y_value;
y_value = x_value_cache;
Upvotes: 2
Reputation: 178263
In rotate90
, the first line of the for
loop body
x_value = -y_value;
overwrites the x_value
, so it's no longer available in the second line.
y_value = x_value; // x_value has already been changed
This effectively sets y_value
to the opposite of itself. Use a temporary variable so you don't lose the old value.
double old_x = x_value;
x_value = -y_value;
y_value = old_x;
The quadrant determination looks correct, but it relies on correct x and y values. Correcting the rotate90
method should also correct the output from the getQuadrant
method.
Upvotes: 2