Using Java: 1.Create an array that will store 7 temperatures. 2.Populate the arr
ID: 3764133 • Letter: U
Question
Using Java:
1.Create an array that will store 7 temperatures.
2.Populate the array with 7 random integer temperatures from 1 to 100 degrees. (hint use a for loop and a Random number Generator object)
3.After the temperatures are in the array, calculate the average of the temperatures in the array.
4.Print out the average.
5.Print out each temperature in a statement comparing it to the average such as: The average temperature is 48.94 Temperature 1 is 5.0 and is below average. Temperature 2 is 67.8 and is above average. etc.....
6.this program can be done in one file called ArrayOfTemperatures.java.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class ArrayOfTemperatures
{
public static double randomInRange(double min, double max) {
Random random = new Random();
double range = max - min;
double scaled = random.nextDouble() * range;
double shifted = scaled + min;
return shifted; // == (rand.nextDouble() * (max-min)) + min;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
double[] temp = new double[7];
double sum = 0;
for(int i=0;i<7;i++)
{
temp[i] = randomInRange(1,100);
sum = sum+temp[i];
}
double avg = sum/7;
System.out.println("Average is : "+avg);
for(int i=0;i<7;i++)
{
if(temp[i]==avg)
System.out.println("Average temperature is : "+avg+" and temperature "+(i+1)+" "+temp[i]+" is equal to the average.");
else if(temp[i]>avg)
System.out.println("Average temperature is : "+avg+" and temperature "+(i+1)+" "+temp[i]+" is above than average.");
else
System.out.println("Average temperature is : "+avg+" and temperature "+(i+1)+" "+temp[i]+" is below than average.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.