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

Write a Java program that asks the user for several days\' temperatures and calc

ID: 655256 • Letter: W

Question

Write a Java program that asks the user for several days' temperatures and calculates the average temperature for that period and how many days were above average.

First, ask the user how many days he wants to calculate the average for. Second, create an array with that number and ask for the temperature of each day. Store the temperatures in the array. Finally, find the average temperature and the number of days above that average. Print the results to the screen.

The array size will be inputted by the user (use Scanner). Please make sure output starts printing at Day 1 and not Day 0.

Output:

Explanation / Answer

import java.util.Scanner;

import java.io.*;

public class Temperatures {

    public static void main(String[] args)

    {

        temperatureAnalysis temp=new temperatureAnalysis();

        temp.getTemp();

        temp.calcAvg();

        temp.aboveTemp();

    }   

}

class temperatureAnalysis

{

    int temperature[];

    int n;

    int count;

    double avgTemp;

    temperatureAnalysis()

    {

     temperature=new int[50];

     count=0;

    }

    public void getTemp()           // read & store the temperature values into array

    {

        System.out.println("How many days Temperature?:");

        Scanner scan=new Scanner(System.in);

        n=Integer.parseInt(scan.nextLine());

        for(int i=1;i<=n;i++)

        {

        System.out.println("Day "+i+" 's High Temp:");

        temperature[i]=Integer.parseInt(scan.nextLine());

        }

    }

   

    public void calcAvg() // calculates average temperature

    {

       double sum=0;

    for(int i=1;i<=n;i++)

        {

            sum=sum+temperature[i];

        }

    avgTemp=sum/n;

    System.out.println("Average Temperature: "+avgTemp);

    }

    public void aboveTemp() // calculates average temperature

    {

        int count=0;

    for(int i=1;i<=n;i++)

        {

            if(temperature[i] >avgTemp)

            count+=1;

        }

    System.out.println(count+" days above average");

    }

}

----------------------------------------------------------------------------------------------------------------------------------------

Output:

How many days Temperature?:

5

Day 1 's High Temp:

67

Day 2 's High Temp:

78

Day 3 's High Temp:

45

Day 4 's High Temp:

34

Day 5 's High Temp:

90

Average Temperature: 62.8

3 days above average

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