can anyone help me with this..question is too long but answer is not that too lo
ID: 3626824 • Letter: C
Question
can anyone help me with this..question is too long but answer is not that too long.? Create a class called Point in the myclasses package.
? Create two instance variables, x (int) and y (int). Both are private with public getter and private setter methods.
? Create a setPoint( ) method. This will be a public method that has two parameters: newX (int) and newY (int). This method should call the other setter methods.
? The no argument constructor should set the coordinates of the Point to (0,0).
? Create an overloaded constructor with parameters x (int) and y (int). Set the coordinates of the Point accordingly.
? Define the toString( ) method to print the coordinates of the Point within parentheses and comma separated; for example, (2,3).
? Create a new class called TestPoint within the testmyclasses package. Remember the import statement.
? Create an instance of Point called p1 using the no argument constructor.
? Create an instance of Point called p2 with x=2 and y=1.
? Print the value of p1: you should see (0,0).
Print the value of p2: you should see (2,1).
? Create four public methods: up(), down(), left(), and right(). Each method should have a parameter units (int) indicating the distance the point should be moved. Reminder:
? to move up i units, add i to y;
? to move down i units, subtract i from y;
? to move left i units, subtract i from x; and
? to move right i units, add i to x.
? Test your methods by moving and printing p2 as follows:
? move up(2) and print: you should see (2,3)
? move left(5) and print: you should see (-3,3)
? move down(1) and print: you should see (-3,2)
? move down(4) and print: you should see (-3,-2)
? move right(1) and print: you should see (-2,-2)
? move right(4) and print: you should see (2,-2)
? move up(2) and print: you should see (2,0)
? move left(2) and print: you should see (0,0)
Explanation / Answer
please rate - thanks
import java.util.*;
public class TestPoint
{public static void main(String[] args)
{Point p1=new Point();
Point p2=new Point(2,1);
System.out.println("p1="+p1.toString());
System.out.println("p2="+p2.toString());
p2.up(2);
System.out.println("after up(2): p2="+p2.toString());
p2.left(5);
System.out.println("after left(5): p2="+p2.toString());
p2.down(1);
System.out.println("after down(1): p2="+p2.toString());
p2.down(4);
System.out.println("after down(4): p2="+p2.toString());
p2.right(1);
System.out.println("after right(1): p2="+p2.toString());
p2.right(4);
System.out.println("after right(4): p2="+p2.toString());
p2.up(2);
System.out.println("after up(2): p2="+p2.toString());
p2.left(2);
System.out.println("after left(2):p2="+p2.toString());
}
}
---------------------------------------------
public class Point
{
private int x, y;
public Point()
{setPoint( 0, 0 );
}
public Point(int x, int y )
{setPoint(x,y);
}
public void setPoint(int x, int y)
{setX(x);
setY(y);
}
public int getX()
{return x;
}
public int getY()
{return y;
}
public void setX(int newX)
{x=newX;
}
public void setY(int newY)
{y=newY;
}
public String toString()
{return "("+x+","+y+")";
}
public void up(int i)
{y+=i;
}
public void down(int i)
{y-=i;
}
public void left(int i)
{x-=i;
}
public void right(int i)
{x+=i;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.