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...
This is an intro to Java Question. My current solution is giving me bad outputs. Please...
This is an intro to Java Question. My current solution is giving me bad outputs. Please show me your way of solving this. Problem 4: Min/Max Search by Value Develop a program that, given a sequence S of integers as input, produces as two output values, the first is the minimum value that appears in the sequence and the second is the maximum value that appears in the sequence. Facts ● Scanner has a method that returns a boolean indicating...
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...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding. We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :) Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst...
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
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...
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT