Write an application that provides statistics about temperatures for a given wee
ID: 3673914 • Letter: W
Question
Write an application that provides statistics about temperatures for a given week. Your solution should be a two-class application that has a one-dimensional array as a data member. The array stores temperatures for any given week. Provide constructors for instantiating the class and methods to return the highest temperature, lowest temperature, average temperature, and the average temperature excluding the lowest temperature. Provide a method that accepts as an argument a temperature and returns the number of days the temperatures were below that value. Override the ToString( ) method to return a listing of all the temperatures in three-column format and the temperature range for the given week. Write a second class to test your class.
Explanation / Answer
import java.util.Arrays;
public class WeekTemperature {
int tempList[] = new int[7];
/**
* @param tempList
*/
public WeekTemperature(int[] tempList) {
this.tempList = tempList;
}
// compute and return the average temperature
public double averageTemp() {
double sum = 0;
for (int i = 0; i < tempList.length; i++) {
sum += tempList[i];
}
return sum / tempList.length;
}
/**
* method to return average temperature excluding the lowest temperature
*
* @return
*/
public double averageExcludeLowTemp() {
double sum = 0;
for (int i = 0; i < tempList.length; i++) {
sum += tempList[i];
}
sum = sum - lowestTemp();
return sum / (tempList.length - 1);
}
// determine and return the highest temperature
public int highestTemp() {
int max = tempList[0];
for (int i = 0; i < tempList.length; i++) {
if (tempList[i] > max)
max = tempList[i];
}
return max;
}
// determine and return the lowest temperature
public int lowestTemp() {
int min = tempList[0];
for (int i = 0; i < tempList.length; i++) {
if (tempList[i] < min)
min = tempList[i];
}
return min;
}
/**
* number of days the temperatures were below the value temp
*
* @param temp
* @return
*/
public int noOfDaysLessTemp(int temp) {
int noOfDays = 0;
for (int i = 0; i < tempList.length; i++) {
if (tempList[i] < temp)
noOfDays++;
}
return noOfDays;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "WeekTemperature [tempList=" + Arrays.toString(tempList) + "]";
}
}
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
/**
* @author srinu
*
*/
public class TestWeekTemperature {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] tempList = new int[7];
System.out
.print("Please enter the highest temperature of each day in a week (start with Sunday): ");
for (int i = 0; i < tempList.length; i++) {
tempList[i] = input.nextInt();
}
WeekTemperature weekTemperature = new WeekTemperature(tempList);
// display the statistics
System.out.println("The average temperature of the week is: "
+ weekTemperature.averageTemp() + " degrees");
System.out.println("The hottest temperature is: "
+ weekTemperature.highestTemp() + " degrees");
System.out.println("The hottest temperature is: "
+ weekTemperature.lowestTemp() + " degrees");
System.out.println("The hottest days are: "
+ (7 - weekTemperature.noOfDaysLessTemp(23)));
System.out.println("The coldest days are: "
+ weekTemperature.noOfDaysLessTemp(23));
}
}
OUTPUT:
Please enter the highest temperature of each day in a week (start with Sunday): 43
56
34
33
19
32
31
The average temperature of the week is: 35.42857142857143 degrees
The hottest temperature is: 56 degrees
The hottest temperature is: 19 degrees
The hottest days are: 6
The coldest days are: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.