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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ repositories {
}

dependencies {
implementation 'org.jetbrains:annotations:20.1.0'
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
84 changes: 27 additions & 57 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,32 @@
# Weather App Assignment

## Introduction
In this assignment, you will build a simple Java application that connects to a weather API and retrieves information about the current weather conditions of a given city.
-this assignment is an application that give us a brief information(humidity,tempreture) of a given city.

## design and implementation
- i used gradle and json for handling the data that computer recives from the wethear api and show the specefic
- data that we want to us . for this code i actually didnt do that much and it was just filling the blanks
- thank you mr.nesari i guess, so i just used two methods for each data and show them to us.

## testing and evaluation
-as i said you as the user just need to type a city and the application will show you the humidity and tempreture
of the given city, i tested the code and didnt encounterd any bugs because as i said it was just a fil in the blanks
assignment , but at the same time i learned alot about toolboxes and how do they work.

## what i used
this assignment had three major topics that we needed to learn before using the in our application
1.json;or javascript objet notation is a format and as way to storage data that i used to get the data
from api in this application
2.gradle; it is a toolbox that helps you build applications easily and it contains tool to help you compile and
and package your code.
3.api; sorry for this one it is a lot harder to explain but basically you use api when you want to interact with a server
or site and it helps you with delivering and recieving the massage and data from the server.

## functions(methods)
this code contains three important functions first is the function that reads the name of the city from user
and i used the try and catch method if the user typed the right city so it doesnt crash and it recieves data
about the city second i made double variable called answer that we use to get the temprature from the
json format and show it to the user anmd the third one does the same vas second one but it has a integer variable
and it shows us the humidity of the current location

## Objectives
- Learn how to import and use a JSON library by reading its documentation
- Parse and process JSON data in Java
- Understand how the `getWeatherData()` function works and makes HTTP requests to retrieve data from the weather API
- Use Git for version control and collaborate on a codebase
- Use Maven or Gradle as package manager
- Write a report on the assignment

## Requirements
- Java 8 or later
- Git
- Maven or Gradle

## Tasks
1. Get an API key from https://www.weatherapi.com/. You need to sign up and verify your account.
2. Fork this repository and clone the fork to your local machine.
3. Create a new Git branch and switch to it (branch name: develop)
4. Add a the org.json package (or any other JSON library that you plan to use) to Gradle. You must include the necessary code in the dependencies section of the `build.gradle` file to import the JSON library. Be sure to check https://www.mvnrepository.com/ for more information.

5. Write a Java class `WeatherApp` that does the following:
- Prompts the user for a city name
- Parses the JSON response to extract the temperature and humidity
- Prints the temperature and humidity to the console

6. Write a detailed report explaining the steps you took while working on this assignment
7. Commit your changes and push them to your own fork on Github

## Notes
1. This project uses Gradle by default, but you are allowed to create a new project and use Maven as your package manager instead.
2. You can learn more about the Weather API by reading its official documentation at https://www.weatherapi.com/docs/. You can also experiment with the API by visiting the API Explorer tab in the website.
3. Your report should primarily be focused on the research you conducted on the JSON data format and your chosen JSON library.
4. Your report doesn't need to follow a specific report template, but it is recommended that you familiarize yourself with these templates before writing your report.

## Evaluation
- Your code should compile and run without any errors
- Your code should correctly retrieve and display the temperature and humidity for a given city
- Your code should be well-organized, readable, properly commented and follows clean code principles
- You should use Git for version control and include meaningful commit messages

**Attention: Using ChatGPT or any other AI generative model for any section of the assignment will result in a score of 0 without any warnings.**

## Bonus Objectives
1. Extend the WeatherApp class to also display the current wind speed and direction
2. Add error handling to handle cases where the API returns an error or the city name is not found
3. Revise your report to make it as comprehensive as possible with more details about your approach.
4. Implement a simple GUI (Graphical User Interface) for your project. This GUI should be able to display the city name and its temperature and humidity as text. There are several GUI libraries available for Java, But we would recommend using either Swing or JavaFX:

- Swing is an older UI toolkit that has been part of the Java Standard Edition (Java SE) since its inception. Swing is still widely used and remains a popular choice for building desktop applications, especially for applications that require a more lightweight UI toolkit.
- JavaFX, which is a more recent addition to the Java platform, is considered more powerful and provides more features and capabilities for building modern, visually rich desktop applications.

## Submission
- Push your code to your fork on Github
- Upload your report as a PDF file to your fork on GitHub

The deadline for submitting your code is wednesday, March 1 (10th of Esfand). Any commits made after this date will not affect your score.

Good luck and happy coding!
28 changes: 16 additions & 12 deletions src/main/java/WeatherApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
import java.util.Scanner;

public class WeatherApp {
// Copy your API-KEY here
public final static String apiKey = "API-KEY";
// TODO: Write main function
public final static String apiKey = "197c936379844258894151857230103";

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the city name :");
String city = sc.next();

String weatherData = getWeatherData(city);
double temperature = getTemperature(weatherData);
int humidity = getHumidity(weatherData);

System.out.println("The temperature in " + city + " is " + temperature);
System.out.println("The humidity percentage in " + city + " is " + humidity);
}

/**
* Retrieves weather data for the specified city.
*
* @param city the name of the city for which weather data should be retrieved
* @return a string representation of the weather data, or null if an error occurred
*/
public static String getWeatherData(String city) {
try {
URL url = new URL("http://api.weatherapi.com/v1/current.json?key=" + apiKey + "&q=" + city);
Expand All @@ -38,15 +40,17 @@ 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;
}
}
}