Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help fixing errors in this program: public class TemperatureProblem { pub

ID: 3865237 • Letter: I

Question

I need help fixing errors in this program:

public class TemperatureProblem

{

public static void main(String[] args)

{

// calling methods to execute

// collect average temperature in for each of the 12 months of the year into an array

int [] monthlyTemp = new int [12];

int hottest, total = 0;

double average = 0.0;

Scanner keyboard = new Scanner(System.in);

for (int i=0; i<=monthlyTemp.length-1; i++)

{

System.out.print("Enter temperature for Month "+(i+1)+": ");

monthlyTemp[i] = keyboard.nextInt();

}

// call method averageTemp to compute average of all average monthly temperature collected from array

public static double averageTemp(monthlyTemp)

{

for (int i=0; i<=monthlyTemp.length-1; i++)

total += monthlyTemp[i];

average = total/12.0;

// return average

return (average);

}

// call method getHotMonth to find hottest temperature from array

public public void getHotMonth(monthlyTemp)

{

hottest = monthlyTemp[0];

for (int i=1; i<= monthlyTemp.length-1; i++)

if (monthlyTemp[i] > hottest)

hottest = monthlyTemp[i];

System.out.println("The hottest month is Month "+hottest+".");

}

// call method displayResults to display both the average and hottest month to user

public static void displayResults[]

{

System.out.println("The average temperature of the year was "+average+" degrees F with Month "+hottest+" being the hottest month.");

}

}

}

Here's the problem:

Write a program to calculate the average temperature for the year and determine the hottest month of the year. In your main method, the program should collect the user input of the average Fahrenheit temperatures for each of the 12 months of a year into an array. The average temperature for January will go into the 1st index position of the array, February will go into the 2nd index position, etc.

Your main method should then call a method named averageTemp, passing the entire array of temperatures to this method as the only argument. The averageTemp method should compute the average of the numbers stored in the array passed to it and return the average to the caller.

Your main method should then call a method named getHotMonth, passing the entire array of temperatures to this method as well. The getHotMonth method should use the array to find the index position of the month with the hottest temperature. If there is a tie between two months, the method should return the array index position of first month to reach the hottest temperature.

Once main receives both the average and index position in the array of the hottest month, it should call a method named displayResults to display the average and hottest month to the user. The method displayResults will accept two arguments, the average temperature and the index position in the array of the hottest month. The final output of the program should say something like, “The average temperature for the year was 45.7 degrees F with July being the hottest month.”

Explanation / Answer

// Java program tested on java-8

// Tool - Eclipse

/*********************************************TemperatureProblem.java**********************/

import java.text.DateFormatSymbols;

import java.text.DecimalFormat;

import java.util.Scanner;

/**

* The Class TemperatureProblem.

*/

public class TemperatureProblem {

/**

* The main method.

*

* @param args

* the arguments

*/

public static void main(String[] args) {

// calling methods to execute

// collect average temperature in for each of the 12 months of the year

// into an array

double[] monthlyTemp = new double[12];

int hottest;

double average;

Scanner keyboard = new Scanner(System.in);

for (int i = 0; i <= monthlyTemp.length - 1; i++) {

System.out.print("Enter temperature for Month " + (i + 1) + ": ");

monthlyTemp[i] = keyboard.nextDouble();

}

average = averageTemp(monthlyTemp);

hottest = getHotMonth(monthlyTemp);

displayResults(average, hottest);

keyboard.close();

}

// call method averageTemp to compute average of all average monthly

// temperature collected from array

/**

* Average temp.

*

* @param monthlyTemp

* the monthly temp

* @return the double

*/

public static double averageTemp(double[] monthlyTemp) {

int total = 0;

double average;

for (int i = 0; i <= monthlyTemp.length - 1; i++)

total += monthlyTemp[i];

average = total / 12.0;

// return average

return average;

}

/**

* Gets the hot month.

*

* @param monthlyTemp

* the monthly temp

* @return the hot month

*/

// call method getHotMonth to find hottest temperature from array

public static int getHotMonth(double[] monthlyTemp) {

double hottest = monthlyTemp[0];

int hottestMonthIndex = 1;

for (int i = 1; i <= monthlyTemp.length - 1; i++)

if (monthlyTemp[i] > hottest) {

hottest = monthlyTemp[i];

hottestMonthIndex = i;

}

System.out.println("The hottest month is Month " + hottest + ".");

return hottestMonthIndex;

}

// call method displayResults to display both the average and hottest month

// to user

/**

* Display results.

*

* @param average

* the average

* @param hottestMonthIndex

* the hottest month index

*/

public static void displayResults(double average, int hottestMonthIndex) {

String hottest = new DateFormatSymbols().getMonths()[hottestMonthIndex];

DecimalFormat df = new DecimalFormat("####0.00");

System.out.println("The average temperature of the year was " + df.format(average) + " degrees F with Month "

+ hottest + " being the hottest month.");

}

}

/*****************************Output****************************/

Enter temperature for Month 1: 25.6
Enter temperature for Month 2: 34.7
Enter temperature for Month 3: 33.8
Enter temperature for Month 4: 40.4
Enter temperature for Month 5: 44.8
Enter temperature for Month 6: 48.9
Enter temperature for Month 7: 50
Enter temperature for Month 8: 45.6
Enter temperature for Month 9: 42.1
Enter temperature for Month 10: 35.6
Enter temperature for Month 11: 28.6
Enter temperature for Month 12: 27.12
The hottest month is Month 50.0.
The average temperature of the year was 37.58 degrees F with Month July being the hottest month.

Thanks a lot.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote