Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
// https://mvnrepository.com/artifact/org.json/json
implementation group: 'org.json', name: 'json', version: '20230227'

}

Expand Down
9 changes: 9 additions & 0 deletions report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Weather app assignment
## Introduction
In this assignment, we built a simple Java application that connects to a weather API and retrieves information about the current weather conditions of a given city.
## Design and Implemention
At first the city's name is passed to the getWeatherData method and it returns a string.Then the string is passed to getTemperature(or getHumidity, etc) method and we can get the information we want.
## Testing and Evaluation
To make sure the program works properly, I checked the results with the correct weather information of that city.
## Conclusion
This assignment helped me to learn how to use JSON objects.
28 changes: 27 additions & 1 deletion src/main/java/WeatherApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

public class WeatherApp {
// Copy your API-KEY here
public final static String apiKey = "API-KEY";
public final static String apiKey = "67a6c2c3591e4e10839152337232502";
// TODO: Write main function
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String city = input.nextLine();

System.out.println("Temperature = " + getTemperature(getWeatherData(city)));
System.out.println("Humidity = " + getHumidity(getWeatherData(city)));
System.out.println("Wind speed = " + getWindSpeed(getWeatherData(city)));
System.out.println("Wind direction = " + getWindDir(getWeatherData(city)));
}

/**
Expand Down Expand Up @@ -41,12 +47,32 @@ public static String getWeatherData(String city) {
// TODO: Write getTemperature function returns celsius temperature of city by given json string
public static double getTemperature(String weatherJson){
double answer = 0.0;
JSONObject data = new JSONObject(weatherJson);
answer = data.getJSONObject("current").getDouble("temp_c");
return answer;
}

// TODO: Write getHumidity function returns humidity percentage of city by given json string
public static int getHumidity(String weatherJson){
int answer = 0;
JSONObject data = new JSONObject(weatherJson);
answer = data.getJSONObject("current").getInt("humidity");
return answer;
}

public static double getWindSpeed(String weatherJson){
double answer = 0.0;
JSONObject data = new JSONObject(weatherJson);
answer = data.getJSONObject("current").getDouble("wind_mph");
return answer;
}

public static String getWindDir(String weatherJson){
String answer;
JSONObject data = new JSONObject(weatherJson);
answer = data.getJSONObject("current").getString("wind_dir");
return answer;
}


}