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: '20220924'

}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/Report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Second-Assignment Report!

## Introduction
- This application is made to give us the temperature and humidity of a city\
by taking the name of the city.
- The purpose of this assignment is probably to learn how to work with Git and GitHub.

## Design and Implementation
- In this program, I did not use any special data structure except
that I got acquainted with json data structure.
- The algorithm I used in this question was to take the city name as a string and then
give it to two functions that return the temperature and humidity of that city.
At the beginning,
I entered the API Key that it can connect with the Weather site.
- The challenge I faced was working with Git because it was my first time using it
and I didn't know its commands.

## Testing and Evaluation
- To check if the program works properly and gives the right output,
I gave it the city of Tehran as input
and it returned its temperature and humidity as output.
## Conclusion
- I learned to work with Markdown
and it was beautiful and simple and of course practical.
- Actually , I don't see any strong point because I didn't do anything special,
but there was a lot of way to improve the solution and increase its strengths.
next time Inshallah!
22 changes: 17 additions & 5 deletions src/main/java/WeatherApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@

public class WeatherApp {
// Copy your API-KEY here
public final static String apiKey = "API-KEY";
// TODO: Write main function
public static void main(String[] args) {
public final static String apiKey = "67282eb9cedb48bf9dc72207230103";
// TODO: Write main function

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String city;
city = in.next();
String WeatherData = getWeatherData(city);
double Temp = getTemperature(WeatherData);
System.out.println(Temp);
int Hum = getHumidity(WeatherData);
System.out.println(Hum);
}

/**
* Retrieves weather data for the specified city.
*
Expand All @@ -32,7 +39,8 @@ public static String getWeatherData(String city) {
}
reader.close();
return stringBuilder.toString();
} catch (Exception e) {
}
catch (Exception e) {
e.printStackTrace();
return null;
}
Expand All @@ -41,12 +49,16 @@ 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;
}
}