Question

In: Computer Science

In Java please, question is: Find a website that publishes the current temperature in your area,...

In Java please, question is:
Find a website that publishes the current temperature in your area, and write a screen-scraper program Weather so that typing java weather followed by your zip code will give you a weather forecast

Solutions

Expert Solution

Iam Sharing Both the codes in python and java it might be helpful you

package realtimeweb.weatherservice.domain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Forecast {
  
private String longDescription;
private String description;
private String imageUrl;
private String temperatureLabel;
private String periodName;
private Integer probabilityOfPrecipitation;
private String periodTime;
private Integer temperature

public String getLongDescription() {
return this.longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
public String getDescription() {
return this.description;
}
  
public void setDescription(String description) {
this.description = description;
}
public String getImageUrl() {
return this.imageUrl;
}
  
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getTemperatureLabel() {
return this.temperatureLabel;
}
public void setTemperatureLabel(String temperatureLabel) {
this.temperatureLabel = temperatureLabel;
}
public String getPeriodName() {
return this.periodName;
}
public void setPeriodName(String periodName) {
this.periodName = periodName;
}
public Integer getProbabilityOfPrecipitation() {
return this.probabilityOfPrecipitation;
}
public void setProbabilityOfPrecipitation(Integer probabilityOfPrecipitation) {
this.probabilityOfPrecipitation = probabilityOfPrecipitation;
}
  
public String getPeriodTime() {
return this.periodTime;
}
public void setPeriodTime(String periodTime) {
this.periodTime = periodTime;
}
  
public Integer getTemperature() {
return this.temperature;
}
public void setTemperature(Integer temperature) {
this.temperature = temperature;
}
   public String toString() {
       return "Forecast[" +longDescription+", "+description+", "+imageUrl+", "+temperatureLabel+", "+periodName+", "+probabilityOfPrecipitation+", "+periodTime+", "+temperature+"]";
   }
   @SuppressWarnings("unchecked")
   public Forecast(Map<String, Object> raw) {
try {
this.longDescription = ((Map<String, Object>) raw.get("data")).get("text").toString();
this.description = ((Map<String, Object>) raw.get("data")).get("weather").toString();
this.imageUrl = ((Map<String, Object>) raw.get("data")).get("iconLink").toString();
this.temperatureLabel = ((Map<String, Object>) raw.get("time")).get("tempLabel").toString();
this.periodName = ((Map<String, Object>) raw.get("time")).get("startPeriodName").toString();
this.probabilityOfPrecipitation = Integer.parseInt(((Map<String, Object>) raw.get("data")).get("pop").toString());
this.periodTime = ((Map<String, Object>) raw.get("time")).get("startValidTime").toString();
this.temperature = Integer.parseInt(((Map<String, Object>) raw.get("data")).get("temperature").toString());
} catch (NullPointerException e) {
       System.err.println("Could not convert the response to a Forecast; a field was missing.");
       e.printStackTrace();
   } catch (ClassCastException e) {
       System.err.println("Could not convert the response to a Forecast; a field had the wrong structure.");
       e.printStackTrace();
}
  
   }
  
public Forecast(String periodName, String periodTime, String temperatureLabel, int temperature, int probabilityOfPrecipitation, String description, String imageUrl, String longDescription) {
       this.periodName = periodName;
       this.periodTime = periodTime;
       this.temperatureLabel = temperatureLabel;
       this.temperature = temperature;
       this.probabilityOfPrecipitation = probabilityOfPrecipitation;
       this.description = description;
       this.imageUrl = imageUrl;
       this.longDescription = longDescription;
   }

   @SuppressWarnings("unchecked")
   public static ArrayList<Forecast> parse(Map<String, Object> raw) {
       HashMap<String, Object> rawTime = (HashMap<String, Object>) raw.get("time");
       HashMap<String, Object> rawData = (HashMap<String, Object>) raw.get("data");
      
       ArrayList<String> periodNames = ((ArrayList<String>)rawTime.get("startPeriodName"));
       ArrayList<String> periodTimes = ((ArrayList<String>)rawTime.get("startValidTime"));
       ArrayList<String> tempLabels = ((ArrayList<String>)rawTime.get("tempLabel"));
       ArrayList<String> temperatures = ((ArrayList<String>)rawData.get("temperature"));
       ArrayList<String> pops = ((ArrayList<String>)rawData.get("pop"));
       for (int i = 0; i < pops.size(); i+= 1) {
           if (pops.get(i) == null) {
               pops.set(i, "0");
           }
       }
       ArrayList<String> weather = ((ArrayList<String>)rawData.get("weather"));
       ArrayList<String> icons = ((ArrayList<String>)rawData.get("iconLink"));
       ArrayList<String> text = ((ArrayList<String>)rawData.get("text"));

       ArrayList<Forecast> forecasts = new ArrayList<Forecast>();

python

import requests
from pprint import pprint
def weather_data(query):
   res=requests.get('http://api.openweathermap.org/data/2.5/weather?'+query+'&APPID=****************************8&units=metric');
   return res.json();
def print_weather(result,city
   print("{}'s temperature: {}°C ".format(city,result['main']['temp']))
   print("Wind speed: {} m/s".format(result['wind']['speed']))
   print("Description: {}".format(result['weather'][0]['description']))
   print("Weather: {}".format(result['weather'][0]['main']))
def main():
   city=input('Enter the city:'
   print()
   try:
   query='q='+city
   w_data=weather_data(query);
   print_weather(w_data, city)
   print()
   except:
   print('City name not found...')
if __name__=='__main__':
   main()


Related Solutions

Question 2​ Part (a)​​ Please state your current major (or the area of business, e.g., finance,...
Question 2​ Part (a)​​ Please state your current major (or the area of business, e.g., finance, marketing, human resources, you plan to go into if it is different from your current major). major - Accounting Part (b)​ From the area of business you stated above, identify a relationship between variables that can be estimated with a linear regression. Specifically, you need to: - state the dependent variable - state one independent variable that could explain why the dependent variable changes...
In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Find a company website that in your opinion violates one or more of the principles of...
Find a company website that in your opinion violates one or more of the principles of good design. Include a link to the company website you are using for this discussion. What was your first impression of this company when you viewed their website? Which principle(s) of design is(are) being violated on the company website you found? What would you do to improve the website design?
Question: Go to the website of Statistics Canada and find data on the consumer price index...
Question: Go to the website of Statistics Canada and find data on the consumer price index for Ontario. By how much has the index for each product group and also including all items risen over the years from 2015 to 2017? For which categories of spending have prices risen the most/the least from 2015 to 2017? How much is the percent price change for each category of spending from 2015 to 2017? Have any categories experienced price declines? Explain any...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Whether current employment laws are sufficient to protect employees workplace? PLEASE PROVIDE WEBSITE
Whether current employment laws are sufficient to protect employees workplace? PLEASE PROVIDE WEBSITE
ch 15. This is your last posting!!! Find an article or website that discusses a cultural...
ch 15. This is your last posting!!! Find an article or website that discusses a cultural or social approach to death. Relate the information to what you know about death and what the text mentions about death. Remember that your article or link should be unique and reputable (e.g a society or organization's website rather than a personal webpage). Mention the link or article in your post. If you have any questions, please let me know.
----Modern Physics question: Please be thorough with your explanation. Using the Internet, find the location of...
----Modern Physics question: Please be thorough with your explanation. Using the Internet, find the location of a particle accelerator. In several paragraphs describe its size, what particles are being accelerated, how they are being accelerated, what their target is, how the particles are detected and what sub-atomic particles have been discovered at the site. Be sure to cite your sources.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT