Method Details
We have one ReadSnowfallData.java which comprises of various methods.
List of methods in this file:-
- main: –
This is the main method for this program. The program executions start from this method. This method reads the data from the snowfall.dat file and make an array from this data. It also calls the startPrg to start further functionality.
- startPrg: –
This is the method that is being called from main method. It contains an infinite loop which calls the menu function to take user choice and then call various methods to perform the functionality selected by the user.
- saveFile: –
This method takes atores the current array into snowfall.dat file.
- getTotal: –
This method finds and returns the total of an array received as argument.
5.getAverage: –
This method finds the average of the array passed as argument and returns it to the calling function.
- sortByAverage: –
This method makes the use of getAverage method to performs the sorting of the array according to the average snowfall of a site.
- swap: –
This method takes two array indexes as arguments and then sorts the array elements present at these two indexes.
- sortByTotal: –
This method makes the use of getTotal method to performs the sorting of the array according to the total snowfall of a site.
- viewData: –
This method prints the snowfall of all the sites for various years.
- addNewSiteData: –
This method resizes the array and adds a new row to the current 2D array. After resizing, it takes the snowfall data from the user for all the years and stores it into array.
- addNewYearData: –
This method resizes the array and adds a new column to the current 2D array. After resizing, it takes the snowfall data from the user for the New Year for all the sites and stores it into array.
- exit: –
This method prints an exit message and terminates the program.
- menu: –
This method prints the menu choices available and makes the use of getInt method to return one choice to the user.
- getInt: –
This method takes two argument min and max. This method performs the exception handling and returns an integer in the range between min and max.
- getDouble: –
This method gets a double value from the user, and returns this value to the calling function. This method makes sure that a valid double value is entered by the user.
Pseudocode for the various methods: –
- main: –
- set dis as new datastream to input file
- set rows as int value read from dis
- set cols as int value read from dis
- repeat steps 5 to 8
- set data as read double value from dis
- set array[i][j] as data
- set j as j+1
- if j is equal to columns, set i as i+1 and j as 0
- close dis
- call strtPrg
- startPrg: –
- repeat steps 2 to 10
- get choice as result from calling method menu
- if choice is equal to 1 call addNewYearData and then break
- if choice is equal to 2 call addNewSiteData and then break
- if choice is equal to 3 call viewData and then break
- if choice is equal to 4 call sortByTotal and then break
- if choice is equal to 5 call sortByAverage and then break
- if choice is equal to 6 call saveFile and then break
- if choice is equal to 5 call exit and then break
- saveFile: –
- set dis as DataOutputStream to snowfallData1.dat
- write array.length as int to dis
- wite array[0].length as int to dis
- for i in range 0 to length of array repeat step 5
- for j in range 0 to array[i].length repeat step 6
- write array[i][j] as double to dis
- close dis
- getTotal: –
- set tot as 0.0
- for i in range 0 to length of array repeat step 3
- for j in range 0 to array[i].length repeat step 4
- set tot as tot+ array[i][j]
- return tot to the calling function
E.getAverage: –
- set tot as 0.0
- for i in range 0 to length of array repeat step 3
- for j in range 0 to array[i].length repeat step 4
- set tot as tot+ array[i][j]
- return tot/length of array to the calling function
- sortByAverage: –
- if length of array is 1 return
- for i in range 0 to length of array repeat step 3
- for j in range 0 to array[i].length repeat step 4
- if getAverage(array[j]) is less than getAverage(array[i]) call swap(i,j)
- swap: –
in1 and in2 are arguments
- for i in range 0 to length of array repeat step 2
- set t as array[in1][i]
- set array[in1][i] as array[in2][i]
- set array[in1][2] as t
- sortByTotal:
- if length of array is 1 return
- for i in range 0 to length of array repeat step 3
- for j in range 0 to array[i].length repeat step 4
- if getTotal(array[j]) is less than getTotal(array[i]) call swap(i,j)
- viewData: –
- for i in range 0 to length of array repeat step 2 and 3
- Display “Site” +(i+1)
- for j in range 0 to array[i].length repeat step 4
- Display array[i][j]+”t”;
- addNewSiteData: –
- set new array as new double array[array.length+1][array[0].length
- for i in range 0 to length of array repeat step 3
- for j in range 0 to array[i].length repeat step 4
- set newArr[i][j] as array[i][j]
- setarray as new array
- for i in range 0 to length of array repeat step 7
- set array[array.length-1][i] as double value from callin getDouble
- addNewYearData: –
1 . set new array as new double array[array.length][array[0].length+1]
- for i in range 0 to length of array repeat step 3
- for j in range 0 to array[i].length repeat step 4
- set newArr[i][j] as array[i][j]
- setarray as new array
- for i in range 0 to length of array repeat step 3
- set array[i][array[i].length-1] as double value from callin getDouble
- exit: –
- Display “Exitting”
- terminate the application
- menu: –
- Display “nnSouth Pacific Hotel (snowfall) Menu options: “
- Display “1. Add new Year data.”
- Display “2. Add new site data.”
- Display “3. View data”
- Display “4. Sort by Total”
- Display “5. Sort by Average”
- Display “6. Save to File”
- Display “7. Exit”
- return getInt(1,7)
- getInt: –
- set value as 0
- set value as input from user
- if value < min or value greater than max throw exception
- else return value
- if exception call getInt
- getDouble: –
- set value as 0
- set value as double input from user
- return value
- if exception call getDouble
IPO Table: –
Method Name |
Input |
Process |
Output |
main |
NA |
Read file add the read data to array call startPrg |
NA |
startPrg |
NA |
Print menu Get user choice perform functyion accordingly |
NA |
saveFile |
NA |
Save the array to file |
NA |
getTotal |
Double [] arr |
Get total of the passed array |
total of the array |
getAverage |
Double [] arr |
Get average of the passed array |
Average of the array |
sortByAverage |
NA |
Sort the array by site average |
NA |
swap |
Int in1, int in2 |
Swap elements at index in1 and in2 |
NA |
sortByTotal |
NA |
Sort the array by site total |
NA |
viewData |
NA |
Print the data of the array |
NA |
addNewSiteData |
NA |
Resize the array Add data for all the years for a new site |
NA |
addNewYearData |
NA |
Resize the array Add data for all the sites for a new year |
NA |
Exit |
NA |
Print exit message terminate the application |
NA |
menu |
NA |
Print menu return a choice from user |
Int value as user choice |
getInt |
Int min, int max, int prompt |
Get an int value perform validation return the read value to calling function |
Int value read from user |
getDouble |
String prompt |
Get an double value perform validation return the read value to calling function |
Double value read from user |
Test Data: –
# |
Test Input |
Expected Output |
Result |
1 |
Add Year data: Enter year 6 snowfall data for site 1 : 1.1 Enter year 6 snowfall data for site 2 : 2.3 Enter year 6 snowfall data for site 3 : 4.5 Enter year 6 snowfall data for site 4 : 6.67 |
Site 1 : 42.7 45.8 44.9 40.3 52.0 1.1 Site 2 : 50.8 52.4 49.6 54.7 50.0 2.3 Site 3 : 48.3 40.7 42.0 57.0 52.5 4.5 Site 4 : 51.3 43.8 46.7 48.3 54.7 6.67 |
Pass |
2 |
Add Site data :- Enter year 1 snowfall data for site 5 : 2.3 Enter year 2 snowfall data for site 5 : 4.5 Enter year 3 snowfall data for site 5 : 6.7 Enter year 4 snowfall data for site 5 : 5.5 Enter year 5 snowfall data for site 5 : 4.4 Enter year 6 snowfall data for site 5 : 2.3 |
Site 1 : 42.7 45.8 44.9 40.3 52.0 1.1 Site 2 : 50.8 52.4 49.6 54.7 50.0 2.3 Site 3 : 48.3 40.7 42.0 57.0 52.5 4.5 Site 4 : 51.3 43.8 46.7 48.3 54.7 6.67 Site 5 : 2.3 4.5 6.7 5.5 4.4 2.3 |
Pass |
3 |
After sorting by total |
Site 1 : 2.3 4.5 6.7 5.5 4.4 1.1 Site 2 : 42.7 45.8 44.9 40.3 52.0 2.3 Site 3 : 48.3 40.7 42.0 57.0 52.5 4.5 Site 4 : 51.3 43.8 46.7 48.3 54.7 6.67 Site 5 : 50.8 52.4 49.6 54.7 50.0 2.3 |
Pass |
4. |
After sorting by average |
Site 1 : 2.3 4.5 6.7 5.5 4.4 1.1 Site 2 : 42.7 45.8 44.9 40.3 52.0 2.3 Site 3 : 48.3 40.7 42.0 57.0 52.5 4.5 Site 4 : 51.3 43.8 46.7 48.3 54.7 6.67 Site 5 : 50.8 52.4 49.6 54.7 50.0 2.3 |
Pass |
5. |
Save file |
Filr successfully saved to snowfall1.dat and can be read from the program |
Pass |
6. |
View Data |
Site 1 : 2.3 4.5 6.7 5.5 4.4 1.1 Site 2 : 42.7 45.8 44.9 40.3 52.0 2.3 Site 3 : 48.3 40.7 42.0 57.0 52.5 4.5 Site 4 : 51.3 43.8 46.7 48.3 54.7 6.67 Site 5 : 50.8 52.4 49.6 54.7 50.0 2.3 |
Pass |
7. |
Exit |
Program terminates after printing exit message |
Pass |
User guide:
- Please make sure that you have snowfall.dat in the same directory as program.
- No errors/bugs in the program