How come you can get the x and y values from a java.awt.Point class by using a method and referencing the value?
Point p = new Point(10,20);
int x0 = p.getX();
int y0 = p.getY();
int x1 = p.x;
int y1 = p.y;
System.out.println(x0+"=="+x1+"and"+y0+"=="+y1);
Did the people who made this class forget to make x and y private?
Looking at the javadoc, these seem to return different types. p.x returns an int while p.getX() returns a double.
The source code of Point shows this:
public int x;
//...
public double getX() {
return x;
}
So it looks like that's its only purpose. getX() is a more convenient way to get the coordinates as a double.
Change to
double x0 = p.getX();
// getX returns the X coordinate of this Point2D in double precision
Related
I have a question. For the first time, I am working with multiple classes in JAVA. I have some trouble doing this. I created a class which I will call from another class. I want to make a type Coordinate, which, as the name suggests, holds coordinates. then, I want to shift those coordinates. So far the code looks as follows:
public class Coordinate {
double x;
double y;
Coordinate(){
x=0;
y=0;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);//TO TEST WHETHER IT DOES SOMETHING
}
Coordinate shiftCoordinate(int z, int w){
this.x = x + z;
this.y = y+ w;
return new Coordinate(x,y);//ERROR: The constructor Coordinate(double, double) is undefined
}
}
It throws an error where stated. I do not understand this error. In my 'main' class, I did the following:
void start() {
Coordinate coordinate = new Coordinate();
coordinate.x=3;
coordinate.y=4;
}
I would expect this to print 3, but it does not. Where am I wrong?
First you don't work with mutiples class, only one : Coordinate but you want multiple constructors.
As your attributs are double make a constructor that needs that type, it'll be used when you write new Coordinate(5,6)
public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}
If you want a default constructor (no args) it'll be used when you call new Coordinate()
You want a way to shift from a Coordinate instance you have 2 ways : modify the current instance or create a new one, but don't do both together (like your code) it's not usefull to get at the end 2 objects whith the same values
// modify current instance
void shiftCoordinate(double z, double w) {
this.x = x + z;
this.y = y + w;
}
// return a new object
Coordinate shiftCoordinate(double z, double w) {
return new Coordinate(this.x + z, this.y + w);
}
Also you last code uses the default constructor with no args so that's normal you don't see any printing use new Coordinate(3,4) to see it
A classic constructor is also, the constructor to clone an instance, it takes an instance and create a new one with the same values :
public Coordinate(Coordinate clone) {
this.x = clone.x;
this.y = clone.y;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);
}
This constructor has arguments that are integers, and you're trying to pass in doubles. Change the constructor to this:
public Coordinate(double x, double y){
this.x = x;
this.y = y;
System.out.print(x);
}
I have an assignment explaining like this:
Write a definition of a class named Point that might be used to store and manipulate the location of a point on the plane. The point is stored as two coordinates: x and y. You will need to declare and implement the following methods:
Two constructors:
a. no-argument constructor that sets the point coordinates to (0,0), and
b. a constructor that takes x and y coordinate of the point and sets member
variables.
Method set that sets the private data after an object of this class is created.
A method to move the point by an amount along the vertical and horizontal directions specified by the first and second arguments: move(double dx, double dy)
The method to rotate the point by 90 degrees clockwise around the origin. Hint: when point is getting rotated 90 clockwise around the origin the following changes happen to its coordinates: xrotated = y; yrotated = -x .
two accessor methods to retrieve the coordinates of the point.
It should be 2 different call. these items on second one (not main class)
I will call this on main class. (I do that).
This is my code but I dont understand what I should do next.
private double x;
private double y;
public Point(double initialX, double initialY) {
x = initialX;
y = initialY;
}
public Point() {
x = 0;
y = 0;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void move(double dx, double dy) {
x += dx;
y += dy;
}
You have almost everything, good job. For the rotate by 90 function, you're given a pretty good clue about what to do. Imagine you have a point (1,2), and you rotate it. you'll end up with (2,-1). if you rotate it again, you'll get (-1,-2). once more gives you (-2,1), and a fourth 90 degree rotation gives (1,2), which is what you started with. come up with a function that does this. It shouldn't be longer than 3 lines.
The setter functions (functions used to set, or change, the values) are simply functions you can use to set the values of the point. so, you'd have a function "setX(...) { ... }" and a function "setY(...) { ... }". These should be very straightforward.
Feel free to ask further questions if you're still confused.
private double x;
private double y;
public Point(double dx, double dy) {
x = dx;
y = dy;
}
public Point() {
x = 0;
y = 0;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void SetX(double dx)
{
x = dx;
}
public void SetY(double dy)
{
y = dy;
}
public void move(double dx, double dy)
{
x = x + dx;
y = y + dy;
}
public double rotateX()
{
double temp = x;
x=y;
y=temp;
return x;
}
public double rotateY()
{
y=-y;
return y;
}
main page
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p = new Point();
p.SetX(50);
p.SetY(17);
System.out.println("X and Y coordinates are : \n("+p.getX()+","+p.getY()+")");
System.out.println("after 90 degree clockwise rotate: ");
System.out.println(p.rotateX()+","+p.rotateY());
I have these instance variables in my class:
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private int height = y2 - y1;
private int width = x2 - x1;
In my program I use methods that change the xs and ys, so I expect height and width to change as well. However, they don't. Can someone explain why this is the case, and how I can make their values "update" after a method call that changes the xs and ys?
Your expression private int height = y2 - y1; is evaluated when the instance variable is initialised: i.e. when the class is instantiated. From that point on it is does nothing - it is not "linked" in any way to the source parts of the expression and does not update when they update.
You probably want a method on the class (public if it is used outside the class, private otherwise) which would be something like the below. You can get rid of your height and width fields:
public int getHeight() { return this.y2 - this.y1; }
However if you decide you still need the width and height internally I would change this to a private method named calculateHeight. Methods called getXYZ are usually accessors for fields and not mutation methods. You can then call this (or the equivalent calculateWidth()) whenever you change the field values for y2, y1, x2, x1.
public int getHeight() { return this.height; }
private int calculateHeight() { return this.y2 - this.y1; }
...
this.y2 = this.y2 + 10;
this.height = calculateHeight();
As an aside I would argue that width and height are positive numbers regardless of y2 being more or less than y1. You can use Math.abs to remove the sign on the subtraction result:
public int getHeight() { return Math.abs(y2 - y1); }
My preference would be to use a single method to return the height and width as a dimension. The two values are really a single piece of data at a point in time. You could use java.awt.Dimension for this:
public Dimension getDimension() {
new Dimension(Math.abs(x2 - x1), Math.abs(y2 - y1));
}
Your calculations are only being performed once: at the initialization of the height and width fields. After changing the x's and y's, you need to repeat the calculations for the height and width, to update their values.
Ex:
x2 = 55;
width = x2 - x1;
When performing a math calculation, the variable's values are only used once. Once the calculation is done, (height = y2 - y1), the height variable does not relate to the y2 and y1 variables.
Solution: Manually update the height and width values, by repeating the calculation, when the x's and y's values are changed.
Solution example:
public void setX1(int x1) {
this.x1 = x1;
height = x2 - x1;
}
Or an even better example is not to have the height and width fields, and use this:
public int getHeight() {
return y2 - y1;
}
public int getWidth() {
return x2 - x1;
}
How can I write a double slope method for a segment class?
I have two variable: p1 = x1, y1 and p2 = x2, y2.
I did this code but this is wrong:
public double slope() {
return (double)(p2.y - p1.y)/(p1.x-p2.x);
}
Can someone tell me why is it wrong?
What is the right way to write it?
Thank you!
Depending on the type of p1, it could be a Point which takes both an x and a y coordinate.
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
You'd have to use getX() and getY() to get the X and Y coordinates. You'd also have to be sure you created the point with new Point(1, 2) as well.
Also, be sure that you're getting the right cast behavior by adding parens around it and your numerator:
return ((double)(p2.getY() - p1.getY()))/(p1.getX() - p2.getX());
(Although that above seems to scream for a deltaY and deltaX method alone)
I am trying to establish a Java method to calculate the x and y coordinate based off of three given coordinates and distances. I used the following post:Determining The Coordinates Of A Point Based On Its Known Difference From Three Other Points for guidance. I just can't get this to work properly and don't really understand the math. With my given inputs I should be outputting (1,4), but instead output a bunch of different results depending on what I make d1, d2, d3.
public class Driver {
public static void main (String[] args)
{
double x1 = 1;
double y1 = 1;
double x2= 2;
double y2 = 1;
double x3= 3;
double y3 = 1;
double d1 = 3;
double d2 = 2;
double d3 = 1;
Main control = new Main();
control.GET_POINT(x1,y1,x2,y2,x3,y3,d1,d2,d3);
}
}
Class w/ Method:
public class Main {
public void GET_POINT(double x1, double y1,double x2,double y2,double x3,double y3, double r1, double r2, double r3){
double A = x1 - x2;
double B = y1 - y2;
double D = x1 - x3;
double E = y1 - y3;
double T = (r1*r1 - x1*x1 - y1*y1);
double C = (r2*r2 - x2*x2 - y2*y2) - T;
double F = (r3*r3 - x3*x3 - y3*y3) - T;
// Cramer's Rule
double Mx = (C*E - B*F) /2;
double My = (A*F - D*C) /2;
double M = A*E - D*B;
double x = Mx/M;
double y = My/M;
System.out.println(x);
System.out.println("and ");
System.out.println( y);
}
}
I guess that there is no problem with your program. The problem is that the four points that you chose have the same Y (the distance vectors are colinar). Therefore, M that is the determinant that is used by the cramer method to solve the linear system is always zero. So two divisions by zero arise in your program.
In this case, the solution is much simpler:
(x-xi)^2+(y-yi)^2=di^2
But y-yi=0. Threfore, x-xi=di.
So, I can write
x-x1=d1
x-x2=d2
x-x3=d3
Therefore, using any of these equations you get x=4 and Y is the same of the other points.
[PS: I think it is not a problem, but in the evaluation of Mx and My instead of dividing by 2 I would divide by 2.0 - just to be sure that integer division is not happening]
I hope I helped.
Daniel