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

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!!!

Write a program called Temperatures that will read in a user specified number of temperatures into a double array. Then print the highest, lowest and average temperature. Write five separate methods in addition to the main method one each for readTemps, highestTemp, lowestTemp, averageTemp and printTemps. The printTemps method should bea void method (it returns nothing) and should accept a doublel ] as input. The readTemps method should create and return a double[ ] and does not take any arguments. The other three should accept a doublel ] as input and return a double value. Make sure you test your program using temperatures bpth above and below zero. Temperatures that you output should be formatted to display 2 places past the decimal point. Sample run: Enter the number of temperatures: 7 Please enter 7 temperatures... Enter temperature #1 of 7: 77 Enter temperature #2 of 7: 65 Enter temperature #3 of 7: 69 Enter temperature #4 of 7: 82 Enter temperature #5 of 7: 70 Enter temperature #6 of 7: 57 Enter temperature #7 of 7: 76 The average temperature is 70.86 The highest temperature is 82.00 The lowest temperature is 57.00 The above statistics are based on the following temperatures: Temperature #1: 77.00 Temperature #2: 65.00 Temperature #3: 69.00 Temperature #4 : 82.00 Temperature #5: 7e.ee Temperature #6: 57.00 Temperature #7: 76.00

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

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