Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an Location class that has 3 private attributes: name: String x: int y: i

ID: 3840216 • Letter: C

Question

Create an Location class that has 3 private attributes: name: String x: int y: int And the following public methods GetDistance(Location a): double ReadCoordinates(): void Plus getters and setters for the attributes. ReadCoordinates() reads x and y coordinates from keyboard and assigns them to class attributes using setters. Check sample output for details. GetDistance(Location a) gets another Location as parameter, and returns the euclidean distance between itself and the given location. In WhereToGo, main(), you should first initialize home location using setters to X and Y (0, 0) and set its name to "Home". Then, from keyboard, read how many locations you need to test for. Then, read that many locations find the closest in terms of GetDistance() metric and print the closest given location. You should initialize the temporary locations using RoadCoordinates(). A sample output is provided.

Explanation / Answer


import java.util.*;
public class HelloWorld{
private String name;
private int x;
private int y;

public int readCoordinates(){
int a;

Scanner cin =new Scanner(System.in);
a=cin.nextInt();
return a;
}
public double getLocation(HelloWorld a){
double distance;
  
distance=Math.sqrt((a.x-x)*(a.x-x)+(a.y-y)*(a.y-y));

return distance;
}
public void setName(String name){
  
this.name=name;
  
}
public String getName(){
  
return name;
  
}
public void setX(int x){
this.x=x;
  
}
public void setY(int y){
this.y=y;
  
}
public int getX(){
  
return x;
}
public int getY(){
  
return y;
}

public static void main(String []args){
int noOfPlaces;
String closestloc = null;
String nameofloc[];
String name;
double small;
int x,y;
double dis[];
HelloWorld a=new HelloWorld();
a.setName("home");
a.setX(0);
a.setY(0);
System.out.println("how many places you want to try");
Scanner cin =new Scanner(System.in);
noOfPlaces=cin.nextInt();
dis=new double[noOfPlaces];
nameofloc=new String[noOfPlaces];
for(int i=0;i<noOfPlaces;i++ ){
HelloWorld c=new HelloWorld();
System.out.println("enter location name");
name=cin.next();
  
System.out.println("enter x coord");
x= c.readCoordinates();
System.out.println("enter y coord");
y=c.readCoordinates();
c.setName(name);
  
c.setX(x);
c.setY(y);

dis[i]=a.getLocation(c);
nameofloc[i]=c.getName();
System.out.println(nameofloc[i]);   
System.out.println(a.getLocation(c));   
}
  
for(int j=0;j<noOfPlaces-1;j++){
   closestloc=nameofloc[0];
if(dis[j]<dis[j+1]){
     
closestloc=nameofloc[j];
}
}
System.out.println("nearest loc is"+closestloc);
}
}

output:

how many places you want to try
2
enter location name
infy
enter x coord
4
enter y coord
2
infy
4.47213595499958
enter location name
sector
enter x coord
7
enter y coord
8
sector
10.63014581273465

nearest loc isinfy

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote