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

Use Loops, Conditionals, and HashMaps package code; import java.util.HashMap; pu

ID: 3588966 • Letter: U

Question

Use Loops, Conditionals, and HashMaps

package code;

import java.util.HashMap;

public class Model {

public class Model {

/*

* DESCRIBING A PLACE

*

* Given:

* a String, the name of place,

* a HashMap<String,Point2D> containing a place name to location mapping, and

* a HashMap<String,Integer> containing a place name to population mapping,

* return a String that describes that place as in the example below.

*

* You may assume that if there is an entry for String in either of the two HashMaps, then

* there is an entry for that String in both.

*

* If there is no entry for the String in either HashMap, return the string

* "Requested place is unknown: <placename>."

* where <placename> is replaced by the requested place name.

*

* Suppose we know the following about Tonawanda City:

*

* Tonawanda City is at 43.020335,-78.880315, according to http://www.itouchmap.com/latlong.html

* The population of Tonawanda City is 15,130 (https://en.wikipedia.org/wiki/Tonawanda_(city),_New_York)

*

* Assuming that "Tonawanda City" has an entry in both HashMaps, reflecting this information, the String

* returned must be:

*

* "Tonawanda City has latitude 43.020335, longitude -78.880315, and has a population of 15130 persons."

*

* For example, assuming that "Omicron Persei 8" does not exist in either HashMap, the String

* returned must be:

*

* "Requested place is unknown: Omicron Persei 8."

*/

public String description(String name, HashMap<String,Point2D> location, HashMap<String, Integer> population) {

String answer = "";

return answer;

}

/*

* FIND THE CLOSEST PLACE

*

* Given:

* a Point2D, representing a location, and

* a HashMap<String,Point2D>, containing a place name to location mapping,

* return a String that gives the place name of the closest place to that location from the HashMap.

*

* You may assume that the HashMap has at least one entry - this means that there is

* in fact a closest place!

*

* HINT: Use a figure larger than the Earth's equatorial circumference to seed the closest distance.

* https://www.space.com/17638-how-big-is-earth.html

*

* HINT: Remember that you can obtain the set of keys for which the HashMap has entries by calling

* the keySet() method on the HashMap.

*

* HINT: Keep track of both the shortest distance you've come across so far and the name of that place

* as you iterate through the loop.

*/

public String closestToPoint(Point2D p, HashMap<String, Point2D> location) {

String closest = "";

return closest;

}

Explanation / Answer

1.

public String description(String name, HashMap < String, Point2D > location, HashMap < String, Integer > population) {

String answer = "";

Point2D loc = location.get(name);

if (loc == null) {

answer += "Requested place is unknown :" + name;

return answer;

} else {

answer += name + " has latitude" + loc.getLatitude + ", longitude " + loc.getLongitude + ",";

}

Integer pop = population.get(name);

if (pop == null) {

answer += "The area has no population";

} else {

answer += "and has a population of" + pop + " persons."

}

return answer;

}

2.

public String closestToPoint(Point2D p, HashMap < String, Point2D > location) {

String closest = "";

Float distance = Integer.MAX;

for (String n: location.keySet()) {

Point2D m = location.get(n);

float d = getnearest(p, m);

if(d < distance){

distance =d;

closest = name;

}

}

return closest;

}

public float getnearest(Point2D p, Point2D m) {

float R = radius; //Radius of earth

float 1 = p.latitude.toRadians();

float 1 = p.longitude.toRadians();

float 2 = m.latitude.toRadians();

float 2 = m.longitude.toRadians();

var = 2 - 1;

var = 2 - 1;

var a = Math.sin( / 2) * Math.sin( / 2) + Math.cos(1) * Math.cos(2) * Math.sin( / 2) * Math.sin( / 2);

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

var d = R * c;

return d;

}

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