14Write a program in java that allows a user to enter the te
14.Write a program in java that allows a user to enter the temp of seven days of given week, and stores those values in an array of doubles, The program should produce the average temp for the week,
the maximum temp for the week and the minimum temp for the week
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class Temperature {
public static double getMax(double[] temp){
// initializing max with first day temperture
double max = temp[0];
for(int i=1; i<7; i++)
if(max < temp[i])
max = temp[i];
return max;
}
public static double getMin(double[] temp){
// initializing min with first day temperture
double min = temp[0];
for(int i=1; i<7; i++)
if(min > temp[i])
min = temp[i];
return min;
}
public static double getAverage(double[] temp){
// getting sum of all temperatur
double sum = 0;
for(int i=0; i<7; i++)
sum = sum + temp[i];
return sum/7;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// creating temp array
double[] temp = new double[7];
// getting temperature for week
for(int i=0; i<7; i++){
System.out.print(\"Enter temperature for day \"+(i+1)+\" :\");
temp[i] = sc.nextDouble();
}
System.out.println(\"Max temperature: \"+getMax(temp));
System.out.println(\"Min temperature: \"+getMin(temp));
System.out.println(\"Average temperature: \"+getAverage(temp));
}
}
/*
Sampel run:
Enter temperature for day 1 :34.64
Enter temperature for day 2 :40.4
Enter temperature for day 3 :39.5
Enter temperature for day 4 :38
Enter temperature for day 5 :33
Enter temperature for day 6 :36.5
Enter temperature for day 7 :37.7
Max temperature: 40.4
Min temperature: 33.0
Average temperature: 37.105714285714285
*/


