R]?{?.dll 45%-12:18 PM You are writing a small object that computes various temp
ID: 3738905 • Letter: R
Question
R]?{?.dll 45%-12:18 PM You are writing a small object that computes various temperature stats. After the object has been created (instantiated) you can add any number of temperatures that you'd like to it. At any time you can ask for the minimum of t temperatures, the maximum of the temperatures, and an average e of the temperatures The object to implement this is referred to as Temperature Stats and consists of only the following public methods public TemperatureStats0) public void addTemperature(double t) - Add a temperature to the statistics. This can be called as much as we'd like public void clearTemperatures()-Removes all temperatures. public double getMaximumTemperature0 - Returns the maximum temperature for the inputs given public double getMinimumTemperature) Returns the minimum temperature for the inputs given public double getAverageTemperature - Returns the average temperature for the inputs given Your Main.java should contain code to test your TemperatureSta object. Load multiple values and check to make sure that the values match the expected values Getting Started We are going to do this exercise by writing the obiect that solves the problem first (in a source file called TemperatureStats.java) and then testing it using code we write i Main.java. Using the techniques shown on the web page titleExplanation / Answer
Here is the code for you:
import java.util.*;
/*
You are writing a small object that computes various temperature stats. After the
object has been created (instantiated), you can add any number of temperatures that
you'd like to it. At any time, you can ask for the minimum of the temperatures, the
maximum of the temperatures, and an average of the temperatures.
*/
class TemperatureStats
{
ArrayList<Double> temperatures;
//Constructor which instantiates the temperatures arraylist.
public TemperatureStats()
{
temperatures = new ArrayList();
}
//Add a temperature to the statistics. This can be called as much as we'd like.
public void addTemperature(double t)
{
temperatures.add(t);
}
//Removes all temperatures.
public void clearTemperatures()
{
temperatures.clear();
}
//Returns the maximum temperature for the inputs given.
public double getMaximumTemperature()
{
double max = temperatures.get(0);
for(int i = 1; i < temperatures.size(); i++)
if(temperatures.get(i) > max)
max = temperatures.get(i);
return max;
}
//Returns the minimum temperature for the inputs given.
public double getMinimumTemperature()
{
double min = temperatures.get(0);
for(int i = 1; i < temperatures.size(); i++)
if(temperatures.get(i) < min)
min = temperatures.get(i);
return min;
}
//Returns the average temperature for the inputs given.
public double getAverageTemperature()
{
double avg = 0;
for(int i = 0; i < temperatures.size(); i++)
avg += temperatures.get(i);
return avg / temperatures.size();
}
}?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.