Programming Assignment Unit 8-Submission1
Programming Assignment Unit 8-Submission1
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONObject;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Weather Information App");
// Input section
Label locationLabel = new Label("Enter Location:");
locationField = new TextField();
Button fetchButton = new Button("Get Weather");
fetchButton.setOnAction(e -> fetchWeatherData());
// History section
Label historyLabel = new Label("Search History:");
historyArea = new TextArea();
historyArea.setEditable(false);
// Layout
VBox root = new VBox(20, inputSection, weatherSection,
historySection);
root.setPadding(new Insets(10));
if (location.isEmpty()) {
weatherInfoLabel.setText("Please enter a location.");
return;
}
try {
String apiUrl = String.format(API_URL, location);
URL url = new URL(apiUrl);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
weatherInfoLabel.setText(String.format("Location: %s\nTemperature:
%.1f °C\nHumidity: %.1f%%\nCondition: %s",
location, temp, humidity, description));
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