Recently several cities in north Carolina experienced the joy of a snowfall. The
ID: 3662836 • Letter: R
Question
Recently several cities in north Carolina experienced the joy of a snowfall. The following information was recorded by WERC TV
Your task is to write a java program to compute the total amount of snowfall and the average snowfall for the cities listed above. Your program should display each city and snowfall amount and the average amount of snowfall, found and display the highest as well as the lowest also.. Solve using array system.
city snowfall Boone 2 feet Ashville 6.5 feet Winston-Salem 5.5inches Raleigh 4inches Fayetville .7inchesExplanation / Answer
/**The java program Snowfall that finds the total snow fall,
* avaerage snowfall, minimum and maximum snow fall of given
* cities and snowfalls of cities using arrays*/
//Snowfall.java
public class Snowfall
{
public static void main(String[] args)
{
//an array of string type
String cities[]={"Booe","Ashville",
"Winston-Salem","Raleigh",
"Fayetville"};
//an array of type double to store snowfall
double snow[]={2.0,6.5,5.5,4,0.7};
//print cities and snowfall
print(cities,snow);
//call totalSnow
double total=totalSnow(snow);
System.out.printf(" Total snowfall : %.2f ",total);
System.out.printf("Average snowfall: %.2f ",total/snow.length);
//call minSnowfall
double minsnow=minSnowfall(snow);
System.out.printf("Minimum Snowfall :%.2f ",minsnow);
//call maxSnowfall
double maxsnow=maxSnowfall(snow);
System.out.printf("Maximum Snowfall :%.2f ",maxsnow);
}
/**The method print that takes cities and snow as input arguments
* and print the cities and correspoding snows*/
private static void print(String[] cities, double[] snow)
{
System.out.printf("%-20s%-20s ","City","Snowfall");
System.out.println("-----------------------------------");
for (int i = 0; i < snow.length; i++)
{
System.out.printf("%-20s%-20.2f ",cities[i],snow[i]);
}
}
/**The method minSnowfall that takes an array of type double
* and returns the minimum snowfall*/
private static double minSnowfall(double[] snow) {
//assume minimum snow is at index=0
double min=snow[0];
//Run throught for loop to find the minimum snowfall
for (int i = 1; i < snow.length; i++)
{
if(snow[i]<min)
min=snow[i];
}
//return minimum snowfall
return min;
}
/**The method maxSnowfall that takes an array of type double
* and returns the maximum snowfall*/
private static double maxSnowfall(double[] snow)
{
//assume maximum snow is at index=0
double max=snow[0];
//Run throught for loop to find the maximum snowfall
for (int i = 1; i < snow.length; i++)
{
if(snow[i]>max)
max=snow[i];
}
//return maximum snowfall
return max;
}
/**The method totalSnow that takes array of type double
and returns the total snow fall */
private static double totalSnow(double[] snow) {
//set total=0
double total=0;
for (int i = 0; i < snow.length; i++)
{
total+=snow[i];
}
//return total
return total;
}//end of the totalSnow method
}//end of the class
------------------------------------------------------------------------------------------------------------------------------
Sample output:
City Snowfall
-----------------------------------
Booe 2.00
Ashville 6.50
Winston-Salem 5.50
Raleigh 4.00
Fayetville 0.70
Total snowfall : 18.70
Average snowfall: 3.74
Minimum Snowfall :0.70
Maximum Snowfall :6.50
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.