Desiqn an abstract data type called \"Weather\" that keeps track of the temperat
ID: 3927094 • Letter: D
Question
Desiqn an abstract data type called "Weather" that keeps track of the temperature of the last 7 days and thee average. Assume that the days are numbered 0 to 6 (you can declare an array such as day[7]). When an object of Weather is instantiated, the temperature value of the last 6 days should be automatically initialized to 50.75 Once the program starts running, the user should be able to input the temperature values one day at a time After each input adjust your array index and values, and update the average. At any time, the user may want to see the temperature of last 7 days and the average. The user may also want to see the temperature of a specific day in the last 7days. Use constructor and destructor. Be user-friendly. Guide the user by specifyinq what input is expectedExplanation / Answer
import java.util.*;
public class Temperature {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// Input the number of days from the user.
System.out.print("How many days' temperatures? ");
int days = console.nextInt( );
// Declare and create an array, maybe should check if days is positive
int[ ] temps = new int[days];
// Input and store the temperatures in the array
for (int i = 0; i < temps.length; i++) {
System.out.print("Day " + i + "'s high temp: ");
temps[i] = console.nextInt( );
}
// Calculate and print the average
int sum = 0;
for (int i = 0; i < temps.length; i++) {
sum += temps[i];
}
// need a cast to avoid integer division
double average = (double) sum / temps.length;
System.out.println("Average temp = " + average);
// Count the number of values that were above average
int count = 0;
for (int i = 0; i < temps.length; i++) {
if (temps[i] > average) {
count++;
}
}
System.out.println(count + " days were above average");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.