Question

In: Computer Science

C++ Assignment. Design the Weather class that contains the following members: Data members to store: -...

C++ Assignment.

Design the Weather class that contains the following members:

Data members to store:

- a day (an integer)

- a month (an integer)

- a year (an integer)

- a temperature (a float)

- a static data member that stores the total of all temperatures (a float)

Member functions:

- a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly entered temperature to the total).

- a static function that computes and displays the day which has the lowest temperature. Note: An array of Weather objects and the size of the array will be passed to this function.

- a static function that computes and displays the average temperature. This function should use a parameter, if necessary.

Design the main( ) function, which instantiates/creates any number of objects of the Weather class as requested by the user (i.e., creates a dynamic array of Weather objects). main( ) should also call appropriate functions to compute and display the day that has the lowest temperature, as well as the average temperature. Your program should include all necessary error checking.

A sample run of this program could be as follows:

How many days => 4

Enter day, month, year, temperature => 29 11 2018 15.6

Enter day, month, year, temperature => 30 11 2018 8.7

Enter day, month, year, temperature => 1 12 2018 3.1

Enter day, month, year, temperature => 2 12 2018 3.5

Lowest temperature = 3.1 C, Day 1/12/2018

Average temperature = 7.7 C

Solutions

Expert Solution

#include <iostream>

#include <iomanip>

#include <vector>

#include <sstream>

using namespace std;

class Weather

{

private:

int day, month, year;

float temperature;

static float totalTemperature;

public:

Weather()

{

int d, m, y;

float temp;

cout << "Enter day, month, year, temperature => ";

cin >> d >> m >> y >> temp;

this->day = d;

this->month = m;

this->year = y;

this->temperature = temp;

totalTemperature += this->temperature;

}

int getDay(){ return this->day; }

int getMonth(){ return this->month; }

int getYear(){ return this->year; }

float getTemperature(){ return this->temperature; }

float getTotalTemperature(){ return totalTemperature; }

string toString()

{

stringstream ss;

ss << this->temperature << " C, Day " << this->day << "/" << this->month << "/" << this->year;

return ss.str();

}

};

float Weather::totalTemperature = 0;

// function prototype

static void findDayWithLowestTemperature(vector<Weather> weathers, int nDays);

static float getAverageTemperature(vector<Weather> weathers, int size);

int main()

{

vector<Weather> weathers;

int nDays;

cout << endl << "How many days => ";

cin >> nDays;

for(int i = 0; i < nDays; i++)

{

Weather w;

weathers.push_back(w);

}

findDayWithLowestTemperature(weathers, nDays);

cout << setprecision(1) << fixed << "Average temperature = " << getAverageTemperature(weathers, nDays) << " C" << endl;

return 0;

}

static void findDayWithLowestTemperature(vector<Weather> weathers, int size)

{

float min = weathers[0].getTemperature();

int minIndex = -1;

for(int i = 0; i < size; i++)

{

if(weathers[i].getTemperature() < min)

{

min = weathers[i].getTemperature();

minIndex = i;

}

}

cout << "Lowest temperature = " << weathers[minIndex].toString() << endl;

}

static float getAverageTemperature(vector<Weather> weathers, int size)

{

float sum = 0.0;

sum += weathers[0].getTotalTemperature();

return (sum / size);

}

**************************************************************** SCREENSHOT ***********************************************************


Related Solutions

c++ Design the Weather class that contains the following members: Data members to store: -a day...
c++ Design the Weather class that contains the following members: Data members to store: -a day (an integer) -a month (an integer) -a year (an integer) -a temperature (a float) -a static data member that stores the total of all temperatures (a float) Member functions: -a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly entered temperature to the total). -a static...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
11. Payroll in C++ Design a PayRoll class that has data members for an employee’s hourly...
11. Payroll in C++ Design a PayRoll class that has data members for an employee’s hourly pay rate, number of hours worked of type double. The default constructor will set the hours worked and pay rate to zero. The class must have a mutator function to set the pay rate for each employee and hours worked. The class should include accessors for both the hours worked and the rate of pay. The class should lastly have a getGross function that...
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
C++ program: Create a Date class that contains three members: the month, the day of the...
C++ program: Create a Date class that contains three members: the month, the day of the month, and the year, all of type int. The user should enter a date in the format 12/31/2001, store it in an object of type Date, then retrieve the object and print it out in the same format. Next create an employee class. The member data should comprise an employee number (type int) and the employee’s compensation (in dollars; type float). Extend the employee...
Design a Ship class that the following members: A field for the name of the ship...
Design a Ship class that the following members: A field for the name of the ship (a string). A field for the year that the ship was built (a string). A constructor and appropriate accessors and mutators. A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers (an int). A constructor...
Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT