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

Hi everyone, I can\'t seem to wrap my head around this. At least to me, classes

ID: 3621565 • Letter: H

Question

Hi everyone,

I can't seem to wrap my head around this. At least to me, classes are very confusing and the textbook we use in class offers little to no help.

Here's the question:

Create a class called TemperatureArray. This class will hold up to 30 temperature values. The temperatures values may be stored as either Fahrenheit or Celsius. The temperatures may be stored in any position in the array. It should support the following methods:

setTemperatureFahrenheit(int temp, int index) Sets the Fahrenheit value for the temperature at the index location of the array.

getTemperatureFahrenheit( int index) Gets the Fahrenheit value for the temperature at the index location of the array.

setTemperatureCelsius(int temp, int index) Sets the Celsius value for the temperature at the index location of the array.

getTemperatureCelsius( int index) Gets the Celsius value for the temperature at the index location of the array.

getAverageTempFahrenheit() Return the average temperature of all entered values.

getAverageTempCelsius() Return the average temperature of all entered values.

getHighestTempFahrenheit() Return the highest temperature of all entered values as Fahrenheit.

getHighestTempCelsius() Return the highest temperature of all entered values as Celsius.

getLowestTempFahrenheit() Return the lowest temperature of all entered values as Fahrenheit.

getLowestTempCelsius() Return the lowest temperature of all entered values as Celsius.

printTempFahrenheit() Prints the temperature of all entered values as Fahrenheit.

printTempCelsius() Prints the temperature of all entered values as Celsius.

So far, I know that in the public section of the class I need to declare
int TemperatureArray[29];
I've also set up these in the class definition. I think using kelvin values for each would make the most sense, as I'd just need to convert from kelvin to whichever system is needed when the functions call upon the values in the array.
class TemperatureArray{
public:
voidsetTempCelsius(double);
voidsetTempKelvin(double);
voidsetTempFarenheit(double);
double getTempKelvin();
double getTempCelsius();
double getTempFarenheit();
int tempArray[29];
private:
double temperature;
};

From here I'm stuck though, I can't seem to get the array to store my values correctly. And I'm having issues converting my kelvin and such.

Thank you for any help.

Explanation / Answer

Let me know if you have any further questions. Hope this helps. Please rate. :) The first problem I see is that your temperature array is public. This means that using the "get..." and "set..." methods would be pointless, since the user could just directly insert values into the public array. For this reason, you would want the temperature array to be private. And since you're storing real-valued numbers, it should be a double array. Since you want to store 30 values, it should be declared with a size of 30, not 29. And the "double temperature" shouldn't be a class variable but instead should just be used within the methods that need a local variable. You had the right idea with storing it in Kelvin, but since you never actually use Kelvin, why not just store it in Celsius and convert to Fahrenheit whenever needed. That way you won't have to convert to Celsius when you need Celsius. Code: class TemperatureArray{ public: void setTempCelsius(double temp, int index); void setTempFahrenheit(double temp, int index); double getTempCelsius(int index); double getTempFahrenheit(int index); double getAverageTempFahrenheit(); double getAverageTempCelsius(); double getHighestTempFahrenheit(); double getHighestTempCelsius(); double getLowestTempFahrenheit(); double getLowestTempCelsius(); double ERROR; // constructor to initialize ERROR and tempArray values TemperatureArray(); private: double tempArray[30]; double fahrenheitToCelsius(double temp); double celsiusToFahrenheit(double temp); }; TemperatureArray::TemperatureArray() { ERROR = -10000; // Some impossible temperature to indicate that this value does not exist for (int i = 0; i < 30; i++) tempArray[i] = ERROR; } void TemperatureArray::setTempCelsius(double temp, int index) { if (index >= 30) // can't set outside the array bounds return; // no conversion needed tempArray[index] = temp; } double TemperatureArray::fahrenheitToCelsius(double temp) { return (5.0 / 9.0 * temp - 32); } double TemperatureArray::celsiusToFahrenheit(double temp) { return (9.0 / 5.0 * (temp + 32)); } void TemperatureArray::setTempFahrenheit(double temp, int index) { if (index >= 30) return; double t = fahrenheitToCelsius(temp); tempArray[index] = t; } double TemperatureArray::getTempCelsius(int index) { if (index >= 30) return ERROR; return tempArray[index]; } double TemperatureArray::getTempFahrenheit(int index) { if (index >= 30) return ERROR; return celsiusToFahrenheit(tempArray[index]); } double TemperatureArray::getAverageTempFahrenheit() { double sum = 0; int count = 0; for (int i = 0; i < 30; i++) { if (tempArray[i] != ERROR) { sum += celsiusToFahrenheit(tempArray[i]); count++; } } return sum / count; } double TemperatureArray::getAverageTempCelsius() { double sum = 0; int count = 0; for (int i = 0; i < 30; i++) { if (tempArray[i] != ERROR) { sum += tempArray[i]; count++; } } return sum / count; } double TemperatureArray::getHighestTempFahrenheit() { double highest = 0; for (int i = 0; i < 30; i++) { if (tempArray[i] > highest) { highest = tempArray[i]; } } return celsiusToFahrenheit(highest); } double TemperatureArray::getHighestTempCelsius() { double highest = 0; for (int i = 0; i < 30; i++) { if (tempArray[i] > highest) { highest = tempArray[i]; } } return highest; } double TemperatureArray::getLowestTempFahrenheit() { double lowest = tempArray[0]; for (int i = 0; i < 30; i++) { if (tempArray[i]
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