In java please, and follow instructions carefully. Thanks so much!!! Write a pro
ID: 3709288 • Letter: I
Question
In java please, and follow instructions carefully. Thanks so much!!!
Explanation / Answer
The below code will satisfy your needs. It has all the required methods to input a list of temperatures, find the highest,lowest and average temperature among them and to print the list. Defined a main method and tested the methods, verified the output. Thanks
// Temperatures.java
import java.util.Scanner;
public class Temperatures {
/**
* method to read temperatures from user and return it
*
* @return - a double array of temperatures
*/
static double[] readTemps() {
// defining a double array
double[] temps;
Scanner scanner = new Scanner(System.in);
// prompting user to enter the array size
System.out.print("Enter the number of temperatures: ");
int n = scanner.nextInt();
temps = new double[n];// initializing the array
System.out.println("Please enter " + n + " temperatures...");
// setting array values
for (int i = 0; i < n; i++) {
System.out.printf("Enter temperature #%d of %d: ", (i + 1), n);
temps[i] = scanner.nextDouble();
}
return temps;
}
/**
* method to find and return the highest temperature in the given list of
* temperatures
*/
static double highestTemp(double[] temps) {
double highest = 0;
for (int i = 0; i < temps.length; i++) {
if (i == 0) {
/**
* first element, at this point we assume that the highest
* temperature is this element
*/
highest = temps[i];
} else if (temps[i] > highest) {
/**
* the current temperature is higher than what we have assumed,
* so making it as the highest temp
*/
highest = temps[i];
}
}
return highest;
}
/**
* method to find and return the lowest temperature in the given list of
* temperatures
*/
static double lowestTemp(double[] temps) {
double lowest = 0;
for (int i = 0; i < temps.length; i++) {
if (i == 0) {
/**
* first element, at this point we assume that the lowest
* temperature is this element
*/
lowest = temps[i];
} else if (temps[i] < lowest) {
/**
* the current temperature is smaller than what we have assumed,
* so making it as the lowest temp
*/
lowest = temps[i];
}
}
return lowest;
}
/**
* method to find and return the average temperature in the given list of
* temperatures
*/
static double averageTemp(double[] temps) {
double total = 0;
for (int i = 0; i < temps.length; i++) {
//adding to the total
total += temps[i];
}
/**
* average= total/count
*/
return total / temps.length;
}
/**
* method to print all temperature in the given list of
* temperatures
*/
static void printTemps(double[] temps) {
for (int i = 0; i < temps.length; i++) {
System.out.printf("Temperature #%d: %.2f ", (i + 1), temps[i]);
}
}
public static void main(String[] args) {
/**
* reading input and displaying the stats
*/
double temps[] = readTemps();
System.out.printf(" The average temperature is %.2f ",
averageTemp(temps));
System.out.printf("The highest temperature is %.2f ",
highestTemp(temps));
System.out
.printf("The lowest temperature is %.2f ", lowestTemp(temps));
System.out
.println(" The above statistics are based on the following temperatures ");
printTemps(temps);
}
}
/*OUTPUT*/
Enter the number of temperatures: 6
Please enter 6 temperatures...
Enter temperature #1 of 6: 22
Enter temperature #2 of 6: 89
Enter temperature #3 of 6: -25.5
Enter temperature #4 of 6: 76.05
Enter temperature #5 of 6: 5
Enter temperature #6 of 6: 32
The average temperature is 33.09
The highest temperature is 89.00
The lowest temperature is -25.50
The above statistics are based on the following temperatures
Temperature #1: 22.00
Temperature #2: 89.00
Temperature #3: -25.50
Temperature #4: 76.05
Temperature #5: 5.00
Temperature #6: 32.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.