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

Hi, I need help with this please provide correct code by testing and getting the

ID: 3587365 • Letter: H

Question

Hi, I need help with this please provide correct code by testing and getting the expected output. Thanks a lot!

"Model.java 3 1 package code; 3 import java.util.HashMap; 8 This assignment is the first step to a geography-based application. It gives you practice with the following 9 concepts: 11loops 12 13HashMaps Conditionals 5 The methods you write will use: 16 17 HashMap - this is a mapping of place names Ce.g "Tnawonda City") to their locations, 18 expressed in term of latitude and longitude points (this is what the Point2D is used for) 19 20 HashMap String, Integer> - this is a mapping of place names to their populations 21 22 New classes you will work with this homework include: 23 24 Point2D -a pair of double values which we use to represent points on a map, in latitude and longitude 25 A very useful method defined on a Point2D object is called 'distance'. If a and b are Point2D objects 26a.distance(b) returns the distance between the two points (as a double) The methods a.getXO and a.getYO 27return the x and y coordinates of the point a, respectively 29Integer - an object which holds a primitive int value. We've seen this idea before: a Character object 30 holds a primitive char value. Integer and Character are called wrapper classes-their objects hold values 31 of the corresponding primitive type. Every primitive type has a corresponding wrapper class The compiler 32 can generally convert between an Integer and int transparently as needed Cand similarly for other wrapper 33class primitive type pairings) 34 35 Autograding will be set up in Autolab no later than Tuesday, October 3 37 36 38 public class Model l 39 40 DESCRIBING A PLACE 42 43 * Given: 45 46 47 a String, the name of place, a HashMap

Explanation / Answer

/*
* Used the Given Class
*/
public class Point2D {

private double x;
private double y;
  public final double getX() {
      return x;
  }
  public final double getY() {
      return y;
  }
  public Point2D(double x, double y){
      this.x=x;
      this.y=y;
  }
  public double distance(Point2D b){
      double x = getX()-b.getX();
      double y = getY()-b.getY();
      return Math.sqrt(x*x + y*y);
    }
}


//Implemented all the methods
import java.util.HashMap;
public class Model{

//Check for existence. If requested city exists, get the values from both maps and return it
  public String Description(String name, HashMap<String, Point2D> location, HashMap<String, Integer> population){
      String answer = "";
      if(location.containsKey(name)){
//As mentioned in the question, if one map contains the key, other map will also have it
          Point2D loc = location.get(name);
          answer = name+" has latitude "+loc.getX()+", longitude "+loc.getY()+", and has a population of "+population.get(name)+" persons.";
      }else{
          answer = "Requested place is unknown: "+name;
      }
  return answer;
}

//Find the distance and update the min value.
  public String closestToPoint(Point2D p, HashMap<String, Point2D> location){
      String closest = "";
      double min = Double.MAX_VALUE;
      for(String loc : location.keySet()){
          double dist = p.distance(location.get(loc));
          if(min > dist){
              min = dist;
              closest = loc;
          }
    }
      return closest+" is the closest place with the distance "+ min;
  }

//First find all the four corners of the area. You have a point and radius. You can figure out top, left, right, bottom points
//If a location has x value which lies within x values of left and right, then whose y value lies within y values of top and bottom, then city is in the limit
// So now you figured out a city in the radius. Then look for maximum population within the range
  public String largestToPoint(Point2D p, double radius, HashMap<String, Point2D> location, HashMap<String, Integer> population){
      String largest = "";
//Find all the surrounding points
      Point2D left = new Point2D(p.getX()-radius, p.getY());
      Point2D top = new Point2D(p.getX(), p.getY()+radius);
      Point2D right = new Point2D(p.getX()+radius, p.getY());
      Point2D bottom = new Point2D(p.getX(), p.getY()-radius);
      int maxPopulation = Integer.MIN_VALUE;;
      for(String loc : location.keySet()){
          Point2D locPoint = location.get(loc);
          double x = locPoint.getX();
          double y = locPoint.getY();
          if((x>=left.getX() && x<=right.getX()) && (y>=bottom.getY() && y<=top.getY())){
              //City is inside the radius
              if(maxPopulation < population.get(loc)){
                  maxPopulation = population.get(loc);
                  largest = loc;
          }
      }
  }
  if(maxPopulation == Integer.MIN_VALUE){
      return largest;
  } else{
      return largest + " has the most population with in the radius of "+radius+" from the point ("+p.getX()+","+p.getY()+") and the population is "+maxPopulation;
  }
}

//Calculate all the distances. If you have three cities a,b,c. Then find out the distance of ab, ac, bc. Return the maximum distance value
public double longestDistance(HashMap<String, Point2D> location){
  double longest = 0.0;
  double max = Double.MIN_VALUE;
  for(String loc : location.keySet()){
      for( String nextLoc : location.keySet()){
          double dist = location.get(loc).distance(location.get(nextLoc));
          if(max < dist){
              max = dist;
          }
      }
  }
  return longest;
}
}

import java.util.HashMap;
public class Testing{
  public static void main(String[] a){
      Model m = new Model();
      HashMap<String, Integer> population = new HashMap<String, Integer>();
      population.put("Tanawanda City", 15130);

      HashMap<String, Point2D> location = new HashMap<String, Point2D>();
      location.put("Tanawanda City", new Point2D(43.020, -78.880));

      String s = m.Description("Tanawanda City", location, population);
      System.out.println(s);

      Point2D t = new Point2D(30.020, -90.980);
      System.out.println(m.closestToPoint(t, location));

      System.out.println(m.largestToPoint(t, 2.50, location, population));

      System.out.println(m.longestDistance(location));
  }
}

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