For this assignment, you are required to develop a Windowed GUI Java Program to demonstrate you can use Java constructs including input/output via a GUI interface, Java primitive and built-in types, Java defined objects, arrays, selection and looping statements and various other Java commands. Your program must produce the correct results.
The code for the GUI interface is supplied and is available on the course website, you must write the underlying code to implement the program. The command buttons are linked to appropriate methods in the given code. Please spend a bit of time looking at the given code to familiarise yourself with it and where you have to complete the code. You will need to write comments in the supplied code as well as your own additions.
In assignment one we read in multiple locations and temperatures using both Scanner and GUI dialogs. In this assignment we are going to read in the data and output information via a GUI interface, the code for the GUI interface WeatherGUI.java is supplied (via the Moodle web site) which supplies the basic functionality of the interface. We are also going to store the information in an array of Weather objects. see image.
Look at the code supplied and trace the execution and you will see the buttons are linked to blank methods (stubs) which you will implement the various choices via the buttons.
The GUI interface contains three JLabels for the heading and the prompts.
There are two JTextFields in which the location and temperature are read.
There is also a JTextArea for displaying the output.
Four JButtons are at the bottom which link to blank methods for implementing the functionality of the application.
First step is to create a class called Weather (Weather.java) it will not contain a main.
The Weather class will be very simple it will contain two private instance variables:
The following public methods will have to be implemented:
*You will also create a public value returning method to return the temperature rating freezing, cold, cool etc (see assignment one). The rating can be derived from the temperature and typically we do not store calculated values in a database so the rating will be retrieved via your getRating method. Use constants in the if statements for the rating boundaries e.g.
final int FREEZING = 0;
final int COLD = 10;
Once the Weather class is implemented and fully tested we can now start to implement the functionality of the GUI interface.
For this assignment we are going to store the locations and temperatures in an array of Weather objects. Do not use the ArrayList data structure.
Declare an array of Weather objects as an instance variable of WeatherGUI class, the array should hold ten entries. Use a constant for the maximum entries allowed.
private Weather  weatherArray = new Weather[MAX_LOCATIONS];
We need another instance variable (integer) to keep track of the number of the location being entered and use this for the index into the array of Weather objects.
private int currentLocation = 0;
1. Enter button: enterWeatherLocationAndTemperature() For assignment two we are going to use the JTextFields for our input. see image.
When the enter button is pushed the program will transfer to the method: enterWeatherLocationAndTemperature() this is where we read the location and temperature and add them to the Weather array.
The text in the JTextFields is retrieved using the getText() method:
String location = nameField.getText();
When we read the temperature input we are reading a string from the GUI, we will need to convert this to an integer using the Integer wrapper class as per assignment one.
int num = Integer.parseInt(temperatureField.getText());
We need to add these values location and temperature to the array of weather objects, when we declared our array using the new keyword, only an array of references were created, we need to create an instance of each of the Weather objects. When we create the Weather object we can pass the location and temperature to the object via the parameterised constructor.
weatherArray[currentLocation] = new Weather(location, num);
Remember to increment currentLocation at the end of the enter method.
Next we will output the entry including the rating in the JTextArea see image.
The supplied code contains the methods for printing the heading and the line underneath. The font in the text area is fixed width so can be aligned using column widths.
displayTextArea.setText(String.format("%-30s%-16s%9sn", "Location Name", "Temperature", "Rating"));
Just like the JTextFields the JTextArea has a method setText() to set the text in the text area, note this overwrites the contents of the text area.
Note the column widths in the above format string "%-30s%-16s%9sn" to print the temperature use %d (decimal) in the correct spot in your format string.
You can append text to the text area by using the append() method.
When the data has been entered and displayed, the location and temperature JTextFields should be cleared. We can clear the contents by using: textField.setText("");
The focus should then return to the location JTextField.
Data validation (you can implement this after you have got the basic functionality implemented)
When the maximum number of locations is reached do not attempt to add any more locations and give the following error message: see image.
Use an if statement at the beginning of the method and after displaying the error dialog use the return statement to exit the method.
if (nameField.getText().compareTo("") == 0) // true when blank
Use the above code to check the name field for text if there is no text display the following error dialog and use the return statement to exit the method, the focus should return to the location field. see image.
The temperature field should also be checked for text. see image.
We will not worry about checking data types or numeric ranges in this assignment.
2. Display all locations, temperatures and ratings: displayAll()
When this option is selected, display all of the locations and temperatures plus the ratings which have been entered so far. At the end of the list display the average temperature. see image.
Use a loop structure to iterate through the array, you should be sharing the printing functionality with the enter button, hint: create a method which prints one location, temperature and rating entry and use this throughout the program.
Only print the entries which have been entered so far and not the whole array, use your instance variable currentLocation as the termination value in your loop.
Sum the temperatures in the loop for the average calculation at the end.
If no entries have been added clear the text area and display the following error dialog, repeat this for your search. see image.
3. Search for a location: search()
You can just use a simple linear search which will be case insensitive. Use the JOptionPane.showInputDialog() method to input the location. see image.
If the search is successful display the details about the location. see image.
If the search is unsuccessful display an appropriate message and clear the text area. see image.
4. Exit the application: exit()
The exit method is called when the user pushes the exit button or when they select the system close (X at top right hand corner), try and find the code which does this.
During the exiting process we may want to ask the user if they want to save any files or if they really want to exit, in this assignment we will just print an exit message. see image.