import javax.swing.JOptionPane;
public class BMRyanClassTest {
public static void main(String[] args) {
/** You are to write a program to record data from weather stations.
* Your program should allow the following data to be entered for each weather station:
* - weather station code (a 3 digit number 100 - 999)
* - maximum temperature recorded
* - minimum temperature recorded
*
* The user should be able to enter data from an unspecified number of weather stations.
*
* When the user has finished entering data the program should print
* - the highest maximum temperature and the code for the weather station where it was recorded
* - the lowest minimum temperature and the code for the weather station where it was recorded */
/** How to set about this?
* - need arrays for each of
* - weather station number (needs to be validated!)
* - max temp
* - min temp
*
* - user should be able to enter number of arrays, then arrays can be declared
*
* - then loop from zero to numberOfStations to allow data to be entered.
*
* - when data entry finished, simple tests for element numbers of array, then print data
*
* AS MUCH AS POSSIBLE IN METHODS!!!
*/
/** main method */
/** declarations */
int numberOfStations;
float potentialMaxTemp;
float potentialMinTemp;
int potentialMaxTempStationNumber;
int potentialMinTempStationNumber;
String maxTempSentence;
String minTempSentence;
String totalOutput;
String headline = "Weather station news";
/** actual code */ /** ask luser for number of stations */
/** get data */
numberOfStations = getNumberOfStations();
/** set up arrays for stationNumber, maxTemp, minTemp */
int [] stationNumberArray = new int[numberOfStations];
float [] maxTempArray = new float[numberOfStations];
float [] minTempArray = new float[numberOfStations];
/** for loop (0 to number of stations) to ask for: */
for (int counter = 0; counter < numberOfStations; counter++) {
/** stationNumber */
/** validate - if invalid, ask for valid datum DONE IN METHOD
* it would be nice to validate that all station numbers are unique
* by testing each new one against previous entries.*/
/** convert input (String) to integer DONE IN METHOD*/
/** enter into stationNumber array */
stationNumberArray[counter] = getStationNumber(counter);
} /** end for loop */
/** data analysis */
/** find max temp */
/** set potentialMaxTemp = maxTemp[0] */
/** for loop (0 to numberOfStations) to : */
/** check next element: if it's greater than current potentialMaxTemp, */
/** potentialMaxTemp takes on this value */
/** potentialMaxTempStationNumber value becomes the number of this element */
for (int counter = 1; counter < numberOfStations; counter++) {
if (maxTempArray[counter] > potentialMaxTemp) {
potentialMaxTemp = maxTempArray[counter];
potentialMaxTempStationNumber = counter;
} //end if
} //end for
/** find min temp */
/** set potentialMaxTemp = maxTemp[0] */
potentialMinTemp = minTempArray[0];
/** set potentialMaxTempStationNumber = 0 */
potentialMinTempStationNumber = 0;
/** for loop (0 to numberOfStations) to : */
/** check next element: if it's greater than current potentialMaxTemp, */
/** potentialMaxTemp takes on this value */
/** potentialMaxTempStationNumber value becomes the number of this element */
for (int counter = 1; counter < numberOfStations; counter++) {
if (minTempArray[counter] < potentialMinTemp) {
potentialMinTemp = minTempArray[counter];
potentialMinTempStationNumber = counter;
} //end if
} //end for
/** IS IT MORE EFFICIENT TO DO THESE TESTS IN THE SAME LOOP? */
/** data output */
/** It would be neater to use string fragments, AND TO USE METHODS. */
/** construct maxtemp sentence */
maxTempSentence = "The maximum temperature recorded was " + potentialMaxTemp + " degrees celcius. \n" +
"This occurred at weather station " + stationNumberArray[potentialMaxTempStationNumber] + ".";
/** construct mintemp sentence */
minTempSentence = "The minimum temperature recorded was " + potentialMinTemp + " degrees celcius. \n" +
This occurred at weather station " + stationNumberArray[potentialMinTempStationNumber] + ".";
/** concatenate the sentences, line-breaks between the two */
totalOutput = maxTempSentence + "\n\n" + minTempSentence;
/** display concatenated output */
JOptionPane.showMessageDialog(null, totalOutput, headline, JOptionPane.INFORMATION_MESSAGE);
} //end main
/** user-defined methods */
public static int getNumberOfStations() {
/** declarations */
String inputNumberOfStations;
int numberOfStations;
/** actual code */
inputNumberOfStations = JOptionPane.showInputDialog("How many weather stations are we comparing today? " +
"Please note that a set of (station number, its maximum temperature and its minimum temperature) " +
"will be called a DATA_SET.");
numberOfStations = Integer.parseInt(inputNumberOfStations);
/** validate */
while (numberOfStations < 1) {
inputNumberOfStations = JOptionPane.showInputDialog("There's no point in working with zero or negative numbers of stations. " +
"Please try again.");
numberOfStations = Integer.parseInt(inputNumberOfStations);
/** How can I validate against non-numeric input? */
} //end while
/** return value */
return numberOfStations;
} //end getNumberOfPeople
public static int getStationNumber(int counter) {
/** declarations */
int inputCounter = counter + 1;
String stationNumberMessage;
String stationNumberInput;
int stationNumber;
/** validate */
while (stationNumber < 100 || stationNumber > 999) {
stationNumberMessage = "Invalid entry. The station number should be a three digit number "+
"(100 to 999 inclusive). \n\n " +
"Please re-enter the station number for data-unit " + inputCounter + ".";
stationNumberInput = JOptionPane.showInputDialog(stationNumberMessage);
stationNumber = Integer.parseInt(stationNumberInput);
} //end while
return stationNumber;
} //end getStationNumber
public static float getMaxTemp(int counter) {
/** declarations */
String maxTempMessage;
String maxTempInput;
float maxTemp;
/** get user input and return it */
maxTempMessage = "Please enter the maximum temperature (in degrees C) for the current data-unit.";
maxTempInput = JOptionPane.showInputDialog(maxTempMessage);
maxTemp = Float.parseFloat(maxTempInput);
public static float getMinTemp(int counter) {
/** declarations */
String minTempMessage;
String minTempInput;
float minTemp;
}
29
© (except the blatantly ripped-off bits) Random Bozo 2011