Write a Java program to read 7 temperatures from the user an

Write a Java program to read 7 temperatures from the user and shows are above and which are below the average of the 7 temperatures.

main()-declare the double array named temperature.

-Call the method readTemperature() method

readTemperature()-this method should prompt the user to enter the 7 temperature readings.

Calculate the average temperature.

Display the readings that are above and below the average temperature.

Solution

TempAverage.java

import java.util.Scanner;


public class TempAverage {

  
   public static void main(String[] args) {
       double temp[] = readTemperature();
       double average = averageTemparature(temp);
       String belowAverage = \"\ \";
       String aboveAverage = \"\ \";
       for(int i=0; i<temp.length; i++){
           if(average >= temp[i]){
               aboveAverage = aboveAverage + temp[i]+\"\ \";
           }
           else{
               belowAverage = belowAverage + temp[i]+\"\ \";
           }
       }
       System.out.println(\"Above average temparatures are : \"+aboveAverage);
       System.out.println(\"Below average temparatures are : \"+belowAverage);
   }

   public static double[] readTemperature(){
       Scanner scan = new Scanner(System.in);
       double temp[] = new double[7];
       for(int i=0; i<temp.length; i++){
           System.out.println(\"Enter the temparature: \");
           temp[i] = scan.nextDouble();
       }
       return temp;
   }
   public static double averageTemparature(double d[]){
       double sum = 0;
       double average = 0;
       for(int i=0; i<d.length; i++){
           sum = sum + d[i];
       }
average = sum/(double)d.length;
return average;
   }
}

Output:

Enter the temparature:
40
Enter the temparature:
50
Enter the temparature:
60
Enter the temparature:
70
Enter the temparature:
80
Enter the temparature:
90
Enter the temparature:
30
Above average temparatures are :

40.0
50.0
60.0
30.0

Below average temparatures are :

70.0
80.0
90.0

Write a Java program to read 7 temperatures from the user and shows are above and which are below the average of the 7 temperatures. main()-declare the double a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site