Write a java class Location which has two data fields x and y (int), get/set met
ID: 3713158 • Letter: W
Question
Write a java class Location which has two data fields x and y (int), get/set methods for each data field, and two constructor methods for each data field and two constructor methods:a default one intializes x and y to 0, and the other initializes the two data fields with the corresponding parameters. The "Location" class also has a static method named calDistance(Location a, Location b) that returns the distance(of type double) between two locations a and b. Hint to find the distance between two coordinates(x1, y1) and (x2,y2) you can use this formula. d=(x1-x2)^2 + (y1-y2)^2 [this formula is under square root]
Explanation / Answer
public class Location { private int x, y; public Location() { x = 0; y = 0; } public Location(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public static double calDistance(Location a, Location b) { return Math.sqrt(Math.pow(b.y - a.y, 2) + Math.pow(b.x - a.x, 2)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.