How can I move a point and print the new position?
I'm new to Java and I received an assignment that asks me to write a class called PointerTester
that has two Points as instance variables. I need to initialize one of these at coordinate (0.0,0.0) and one at (10.0,12.0). Then I need to move each point by +2.0 in x and -3.0 in Y, query the coordinates of the points, and print out the values.
So far I have this:
public class PointerTester{
/*instance variables*/
private double pointOneX;
private double pointOneY;
private double pointTwoX;
private double pointTwoY;
private double deltaX;
private double deltaY;
/*constructor to initialize*/
public PointerTester (){
pointOneX = 0.0;
pointOneY = 0.0;
pointTwoX = 10.0;
pointTwoY = 12.0;
d开发者_Go百科eltaX = 2.0;
deltaY = -3.0;
}
/*constructor to initialize to specific value*/
PointerTester(double pointOneX, double pointOneY, double pointTwoX, double pointTwoY){
this.pointOneX = pointOneX;
this.pointOneY = pointOneY;
this.pointTwoX = pointTwoX;
this.pointTwoY = pointTwoY;
}
/*command to change value*/
public void moveBy(double deltaX, double deltaY){
this.pointOneX = this.pointOneX + deltaX;
this.pointOneY = this.pointOneY + deltaY;
this.pointTwoX = this.pointTwoX + deltaX;
this.pointTwoY = this.pointTwoY + deltaY;
}
/*Queries*/
public double getOneX(){
return pointOneX;
}
public double getOneY(){
return pointOneY;
}
public double getTwoX(){
return pointTwoX;
}
public double getTwoY(){
return pointTwoY;
}
/*print values*/
public static void main(String[] args){
PointerTester pointOne = new PointerTester();
PointerTester pointTwo = new PointerTester();
System.out.println("Point One after move (" + pointOne + ")");
System.out.println("Point Two after move (" + pointTwo + ")");
}
}
I cannot figure out how to correctly output the values or if I am completely wrong in working on this problem.
Edit It seems I needed to use this code that I at first thought was supposed to be separate
public class Point{
public double x;
public double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}
How do I incorporate this into my code?
I think to get started you should have a separate class called "Point" that encapsulates an X and Y value, and includes a "moveBy" method. It could also implement "toString()" such that "System.out.println" will print something nice for it. [Edit: Or just use java.awt.Point.]
Beyond that, I'll leave that for you to do as your homework.
You might want to use some Point objects.
Point p1 = new Point(); Point p2 = new Point(10,12);
you can then use the setLocation or translate methods found in the point class. Maybe the whole thing would look something like this? Hope this helps.
main(){
//make some points
Point p1 = new Point();
Point p2 = new Point(10,12);
//move our points
p1.translate(2,-3);
p2.translate(2,-3);
//print our points
System.out.println(p1);
System.out.println(p2);
}
You can implement the toString method. There are examples here: http://www.javapractices.com/topic/TopicAction.do?Id=55
You're trying to print the PointerTester object instead of the values contained within. System.out.println(pointOne.getOneX()) should at least stop throwing exceptions and allow you to print correctly.
精彩评论