2021. 4. 7. 15:08ㆍJava programming
You have to represent a point in 2D space. Write a class with the name Point. The class needs two fields with name x and y of type int.
The class needs to have two constructors. The first constructor does not have any parameters. The second constructor has parameters x and y of type int and it needs to initialise the fields.
Write the following methods :
* Method named getX without any parameters, it needs to return the value of x field.
* Method named getY without any parameters, it needs to return the value of y field.
* Method named setX with one parameter of type int, it needs to set the value of the x field.
* Method named setY with one parameter of type int, it needs to set the value of the y field.
* Method named distance without any parameter, it needs to return the distance between this Point and Point 0,0 as double.
* Method named distance with two parameters x, y both of type int, it needs to return the distance between this Point and Point x, y as double.
* Method named distance with parameter another of type Point, it needs to return the distance between this Point and another Point as double.
How to find the distance between two points?
To find a distance between points A(xA,yA) and B(xB,yB), we use the formula:
d(A,B)=√ (xB - xA) * (xB - xA) + (yB - yA) * (yB - yA)
Where √ represents square root.
public class Point {
private int x;
private int y;
public Point(){
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public double distance(){
return Math.sqrt((getX() - 0.0) * (getX() - 0.0) + (getY() - 0.0) * (getY() - 0.0));
}
public double distance(int x, int y){
return Math.sqrt((getX() - x) * (getX() - x) + (getY() - y) * (getY() - y));
}
public double distance(Point another){
return Math.sqrt((getX() - another.getX()) * (getX() - another.getX()) + (getY() - another.getY()) * (getY() - another.getY()));
}
}
Through this coding exercise, a method called get, and another one called set are used quite often.
To talk more about get and set, private variables are used for data access restriction, and one of the easiest ways to protect the data that is to be edited within a certain level or range. Because private variables can only be accessed within the same class(an outside class has no access to it). So we need a special access to them by providing public get and set methods.
The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case.
Providing the getter and setter methods is same as generating a constructor. Just go to the 'Source' tab and click on 'Generate Getters and Setters'.
Coming back to the coding exercise above, the getX method on the line 13 and the getY method after the former return the value of the variable x and and y.
The setX and setY methods, next, take parameters int x and y respectively and assign them to the this.x and this.y variables. Like I mentioned in the previous post, the this. keyword is used to refer to the current object.
However, as the instance variable x and y of the class Point are declared as private, we can't access it from outside of this class.
This whole process to set the variable access restriction with using the access modifer(private), and getter, setter is called Encapsulation. The concept is to make sure that "sensitive" data is hidden from users.
'Java programming' 카테고리의 다른 글
Coding Exercise 34: Complex Operations (0) | 2021.04.09 |
---|---|
Coding Exercise 33: Carpet Cost Calculator (0) | 2021.04.08 |
Coding Exercise 31: Wall Area (0) | 2021.04.06 |
Coding Exercise 30: Person (0) | 2021.04.05 |
Coding Exercise 29: Sum Calculator (0) | 2021.04.04 |