Waseem Saeed
University of the People
CS 1103-01 Programming 2 - AY2025-T2
Programming Assignment Unit 8
08/Jan/2025
A. [Link] Code Submission:
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class WeatherApp extends Application {
private TextField locationField;
private Label weatherInfoLabel;
private ImageView weatherIcon;
private TextArea historyArea;
private final String API_KEY = "da0ef9adc929d73816e404f839de72b7"; //
Replace with your API key
private final String API_URL =
"[Link] + API_KEY +
"&units=metric";
@Override
public void start(Stage primaryStage) {
[Link]("Weather Information App");
// Input section
Label locationLabel = new Label("Enter Location:");
locationField = new TextField();
Button fetchButton = new Button("Get Weather");
[Link](e -> fetchWeatherData());
HBox inputSection = new HBox(10, locationLabel, locationField,
fetchButton);
[Link](new Insets(10));
// Weather information display section
weatherInfoLabel = new Label("Weather details will appear here.");
weatherIcon = new ImageView();
[Link](100);
[Link](true);
VBox weatherSection = new VBox(10, weatherIcon, weatherInfoLabel);
[Link](new Insets(10));
// History section
Label historyLabel = new Label("Search History:");
historyArea = new TextArea();
[Link](false);
VBox historySection = new VBox(10, historyLabel, historyArea);
[Link](new Insets(10));
// Layout
VBox root = new VBox(20, inputSection, weatherSection,
historySection);
[Link](new Insets(10));
Scene scene = new Scene(root, 400, 500);
[Link](scene);
[Link]();
}
private void fetchWeatherData() {
String location = [Link]().trim();
if ([Link]()) {
[Link]("Please enter a location.");
return;
}
try {
String apiUrl = [Link](API_URL, location);
URL url = new URL(apiUrl);
HttpURLConnection conn = (HttpURLConnection)
[Link]();
[Link]("GET");
[Link]();
int responseCode = [Link]();
if (responseCode != 200) {
[Link]("Failed to fetch weather data.
Please try again.");
return;
}
Scanner scanner = new Scanner([Link]());
StringBuilder inline = new StringBuilder();
while ([Link]()) {
[Link]([Link]());
}
[Link]();
JSONObject data = new JSONObject([Link]());
updateWeatherUI(data);
} catch (IOException e) {
[Link]("Error fetching weather data.");
}
}
private void updateWeatherUI(JSONObject data) {
String location = [Link]("name");
JSONObject main = [Link]("main");
double temp = [Link]("temp");
double humidity = [Link]("humidity");
JSONObject weather = [Link]("weather").getJSONObject(0);
String description = [Link]("description");
String iconCode = [Link]("icon");
[Link]([Link]("Location: %s\nTemperature:
%.1f °C\nHumidity: %.1f%%\nCondition: %s",
location, temp, humidity, description));
String iconUrl = "[Link] + iconCode +
"@[Link]";
[Link](new Image(iconUrl));
updateHistory(location, temp, description);
}
private void updateHistory(String location, double temp, String
description) {
String entry = [Link]("%s - %.1f °C, %s\n", location, temp,
description);
[Link](entry);
}
public static void main(String[] args) {
launch(args);
}
}
B. Screenshot of the Output:
C. Weather Information App - README
Introduction
The Weather Information App provides real-time weather updates for a specified location.
This application fetches data from the OpenWeatherMap API and displays weather details
such as temperature, humidity, and weather conditions. The app features a user-friendly
graphical interface built using JavaFX and allows users to search for weather information by
entering a location (city name).
How to Use the App
1. Launch the Application:
o Run the [Link] file to start the app. The main window will appear.
2. Enter a Location:
o Type the name of the city or location into the Location Text Field (for example,
"London").
3. Get Weather:
o Click the "Get Weather" button to fetch weather data for the entered location.
4. View Weather Information:
o The weather details will appear below, including:
Location: The city name.
Temperature: The current temperature in Celsius.
Humidity: The current humidity level.
Weather Condition: A brief description of the weather (e.g., sunny,
cloudy).
5. View Weather Icon:
o An icon representing the current weather condition will be displayed (e.g., a sun for
sunny weather).
6. Search History:
o Each time you search for a new location, the weather details will be added to the
Search History section with the timestamp, allowing you to view recent searches.
Technical Implementation
API Integration:
o The app integrates with the OpenWeatherMap API to fetch real-time weather data
for the specified location. The weather information is retrieved in metric units
(Celsius for temperature).
Graphical User Interface (GUI):
o The app is built using JavaFX and includes the following components:
A TextField for entering the location (city name).
A Button to trigger the weather data retrieval.
A Label to display the weather details (temperature, humidity, and condition).
An ImageView to display the corresponding weather icon.
A TextArea to show the search history.
Error Handling:
o The app handles invalid input by displaying a message when the location is empty or
the weather data cannot be fetched.
Weather Icon Representation:
o The app uses weather condition icons provided by OpenWeatherMap to visually
represent the weather (e.g., clear sky, rain, clouds).
Example Walkthrough
Example Search:
City Entered: London
Weather Information:
o Location: London
o Temperature: 1.3 °C
o Humidity: 93%
o Condition: Overcast Clouds
Features & Enhancements
1. Unit Conversion:
o The current version of the app displays the temperature in Celsius. A future
enhancement could include an option to switch between Celsius and Fahrenheit.
2. Dynamic Background:
o The background could change based on the time of day (e.g., a sunset background in
the evening) to enhance the user experience.
3. Weather Forecast:
o A short-term weather forecast feature could be added, showing the weather for the
next few days.
Conclusion
This app demonstrates how to integrate a weather API, retrieve real-time data, and display it
in a clear, user-friendly format using JavaFX. With additional features like unit conversion,
dynamic backgrounds, and a weather forecast, the app can be further enhanced to provide a
more complete weather experience.
References
1. Oracle JavaFX Documentation
o This official Oracle documentation provides detailed information about using
JavaFX to build graphical user interfaces (GUIs) in Java.
o Link: JavaFX Documentation
2. OpenWeatherMap API Documentation
o The OpenWeatherMap API documentation offers all the necessary details on
how to access and use their API to retrieve weather data, along with examples
and guidelines for various API endpoints.
o Link: OpenWeatherMap API Documentation