Answer to Question #253035 in C# for Israel

Question #253035

Write an application that provides statistics about temperature for given week. Your solution should be a two class application that has 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 as well as 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 temperatures in three column format and the temperature range for the given week. Write a second class to test your class.

 



1
Expert's answer
2021-10-20T15:58:36-0400
using System;
using System.Collections.Generic;


namespace App
{
    class TemperatureStatistics
    {
        // one-dimensional array as a data member. 
        //The array stores temperatures for any given week.
        private double[] temperatures;
        /// <summary>
        /// Provide constructors for instantiating the class
        /// </summary>
        /// <param name="temperatures"></param>
        public TemperatureStatistics(double[] temperatures)
        {
            this.temperatures = temperatures;   
        }


        public double getHighestTemperature()
        {
            double highestTemperature = this.temperatures[0];
            for (int i = 0; i < this.temperatures.Length; i++) {
                if (highestTemperature < this.temperatures[i]) {
                    highestTemperature = this.temperatures[i];
                }
            }
            return highestTemperature;
        }
        public double getLowestTemperature()
        {
            double lowestTemperature = this.temperatures[0];
            for (int i = 0; i < this.temperatures.Length; i++) {
                if (lowestTemperature > this.temperatures[i])
                {
                    lowestTemperature = this.temperatures[i];
                }
            }
            return lowestTemperature;
        }
        public double getAverageTemperature()
        {
            double lowestTemperature = getLowestTemperature();
            double sum = 0;
            double counter = 0;
            for (int i = 0; i < this.temperatures.Length; i++) {
                if (lowestTemperature != this.temperatures[i])
                {
                    sum += this.temperatures[i];
                    counter++;
                }
            }
            return sum / counter;
        }


        public int numberTemperaturesBelow(double temperature)
        {
            int counter = 0;
            for (int i = 0; i < this.temperatures.Length; i++)
            {
                if (temperature > this.temperatures[i])
                {   
                    counter++;
                }
            }
            return counter;
        }
        public override string ToString()
        {
            string table="";
            int counter = 0;
            for (int j = 0; j < 2; j++)
            {
                for (int i = 0; i < 3; i++)
                {
                    table += this.temperatures[counter] + "\t";
                    counter++;
                }
                table +="\n";
            }
            table += this.temperatures[counter-1] + "\n";


            return table;
        }


    }




    class Program
    {
        static void Main(string[] args)
        {
            double[] temperatures={87,98,78,88,79,89,92};


            TemperatureStatistics temperatureStatistics = new TemperatureStatistics(temperatures);


            Console.WriteLine("The lowest temperature: {0}", temperatureStatistics.getLowestTemperature());
            Console.WriteLine("The highest temperature: {0}", temperatureStatistics.getHighestTemperature());
            Console.WriteLine("The average temperature excluding the lowest temperature.: {0:N2}", temperatureStatistics.getAverageTemperature());
            Console.WriteLine("The number of days the temperatures were below 80: {0}", temperatureStatistics.numberTemperaturesBelow(80));
            Console.WriteLine("All temperatures\n"+temperatureStatistics.ToString());




            Console.ReadLine();
        }


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS