Write a C# application that provides statistics about temperatures for a given w
ID: 3921007 • Letter: W
Question
Write a C# 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, aver- age 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
TemperatureApplication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemperatureApplication
{
class TemperatureApplication
{
static void Main()
{
// int[] someTemps = { 88, 87, 85, 80, 87, 90, 82 };
int[] tempArray = new int[7];
tempArray = GetArrayValues();
Temperature tempObject = new Temperature(tempArray);
Console.Write(tempObject);
DisplayObject(tempObject);
Console.ReadKey();
}
public static int[] GetArrayValues()
{
int[] someArray = new int[7];
string inValue;
Console.WriteLine("Please enter seven(7) temperatures!");
for (int i = 0; i < someArray.Length; i++)
{
Console.WriteLine("Temperature" + (i + 1) + ": ");
inValue = Console.ReadLine();
while (int.TryParse(inValue, out someArray[i]) == false)
{
Console.WriteLine("Please enter a number!");
Console.WriteLine("Temperature" + (i + 1) + ": ");
inValue = Console.ReadLine();
}
}
return someArray;
}
public static void DisplayObject(Temperature tempObject)
{
int baseTemp = GetTemp();
while (baseTemp < 50 || baseTemp > 100)
{
Console.WriteLine("Value must be > 49 and < 101");
baseTemp = GetTemp();
}
Console.Clear();
Console.WriteLine("Temp Stats");
Console.Write(" Average: " +
tempObject.ComputeAverage().ToString("F0") +
" Days Below " + baseTemp + ": " +
tempObject.ComputeDaysBelow(baseTemp));
}
public static int GetTemp()
{
string inValue;
int baseTemp;
Console.WriteLine("Enter Base Temperature: ");
inValue = Console.ReadLine();
while (int.TryParse(inValue, out baseTemp) == false )
{
Console.WriteLine("Value must be numberic!");
Console.Write("Enter Base Temperature: ");
inValue = Console.ReadLine();
}
return baseTemp;
}
}
}
Temperature.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemperatureApplication
{
class Temperature
{
private int[] temp;
public Temperature()
{
}
public Temperature(int[] t)
{
temp = new int[t.Length];
temp = t;
}
public int[] Temp
{
get
{
return temp;
}
set
{
temp = value;
}
}
public int ComputeLowest()
{
return temp.Min();
}
public int ComputeMaxTemp()
{
int max = temp[0];
for (int i = 1; i < temp.Length; i++)
{
if (temp[i] > max)
max = temp[i];
}
return max;
}
public double ComputeAverage()
{
return temp.Average();
}
public double CompeAverageWithoutLowest()
{
int total = 0;
total = temp.Sum() - temp.Min();
return total / (temp.Length - 1);
}
public int ComputeDaysBelow(int tempDay)
{
int noOfDays = 0;
foreach (int t in temp)
{
if (t < tempDay)
noOfDays++;
}
return noOfDays;
}
public override string ToString()
{
string result = " Temperatures ";
for (int i = 0; i < temp.Length; i++)
{
if (i % 3 == 0)
{
result += temp[i] + " ";
}
else
{
result += temp[i] + " ";
}
result += " Range: " + ComputeLowest() + " - " +
ComputeMaxTemp();
}
return result;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.