Question
City Weather Lookup Create a class named City that has the following private variables: name - a String that holds the name of the city
City Weather Lookup Create a class named City that has the following private variables:
name - a String that holds the name of the city
latitude - a double that holds the latitude of a point in the city in decimal degrees
longitude - a double that holds the longitude of a point in the city in decimal degrees Your class should have the following constructors/methods:
A default constructor that sets the name to Anchorage, the latitude to 61.1912556, and the longitude to -149.8238687
A second constructor that sets the name, latitude, and longitude to values passed in
Getters and setters (mutators and accessors) for each of the private variables
A void method named showWeatherForecast that outputs the weather forecast for the city using the stored lat/long. To get the weather forecast, add the file WeatherQuery.java, to your project and call the static method getForecast. The method returns an array of Strings that you can output for the upcoming weather forecast. See the file for more information. You don't need to understand how this method works, only how to call the static method and process the array that is returned. Write a main method that creates at least 2 city objects representing different cities. You can look up the latitude and longitude of a city on google maps by left-clicking on a point in the city. Output the city name, latitude, longitude, and weather forecast for each city. If you get a network error for the weather forecast, try again. Sometimes the National Weather Service server is a bit finicky.
import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; import java.util.ArrayList; public class WeatherQuery { // getForecast // // This method returns an array of Strings that is the upcoming weather forecast // for the lat/long specified in decimal degrees (e.g. 61.1912556,-149.8238687 for UAA). // It may take a few moments to get the forecast. A query is made to the National Weather // Service to get the forecast zone for the location. // public static String[] getForecast(double latitude, double longitude) { try { // Use the NWS API to get the weather forecast for a lat/long URL aURL = new URL("https://api.weather.gov/points/" + latitude + "," + longitude + "/forecast"); BufferedReader in = new BufferedReader(new InputStreamReader(aURL.openStream())); // Hard-coded parsing of JSON looking for name and then detailedForecast String inputLine = ""; String name = ""; String detailedForecast = ""; ArrayList forecast = new ArrayList<>(); while ((inputLine = in.readLine()) != null) { if (inputLine.contains("name")) { StringTokenizer st = new StringTokenizer(inputLine.trim(), ":"); String fieldName = st.nextToken(); name = st.nextToken().trim(); name = name.substring(1,name.length()-2); } if (inputLine.contains("detailedForecast")) { StringTokenizer st = new StringTokenizer(inputLine.trim(), ":"); String fieldName = st.nextToken(); detailedForecast = st.nextToken().trim(); detailedForecast = detailedForecast.substring(1,detailedForecast.length()-1); forecast.add(name + " : " + detailedForecast); } } in.close(); String[] strArray = new String[forecast.size()]; strArray = forecast.toArray(strArray); return strArray; } catch (Exception e) { String[] error = new String[1]; error[0] = "An error occurred: " + e.toString(); return error; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started