I have a java project where I was told to write a program that asks the user to input a number of days and temperatures. From there I have to find the average temp, the days above average temp, as well as sorting the temperatures from highest to lowest. *I have done all of these things successfully, however, my professor is asking me to simplify my code by creating separate methods. For example, having a method for the days over the average, and so on. Although I understand the concept of this, I’m not sure how to approach and go about this way. * Please help?
Here is the code that I have thus far:
import java.util.Scanner;
public class NumberAboveAverage {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter number of days of temperature to calculate:");
Scanner keyboard = new Scanner(System. in);
int day = keyboard.nextInt();
int[] temperature = new int[day];
System.out.println("Enter " + day + " temperatures to calculate.");
for(int i=0; i<day; i++){
temperature[i] = keyboard.nextInt();
}
int sum = 0;
for(int i=0; i<day; i++){
sum = sum + temperature[i];
}
int average = sum/day;
System.out.println("The average temperature is: " + average);
int daysOver=0;
for(int i=0; i<day; i++){
if (temperature[i] > average){
daysOver++;
}
}
System.out.println("The temperature was above average for " + daysOver + " day(s).");
for (int i = 0; i < temperature.length; i++) {
int min = i;
for (int j = i; j < temperature.length; j++) {
if (temperature[j] < temperature[min])
min = j;
}
int temp;
temp = temperature[i];
temperature[i] = temperature[min];
temperature[min] = temp;
}
System.out.print("The temperatures in increasing order are: ");
for(int i=0; i<day; i++){
System.out.print(temperature[i]+" ");
}
}
}
You may want to perform one task in a separate methods as below:
and create methods as below:
Complete the rest of the methods.