48.5k views
3 votes
Write a class encapsulating the concept of daily temperatures for a week. Write the following methods:

A constructor accepting an array of seven temperatures as a parameter
Accessor, mutator, tostring, and equals methods
A method returning how many temperatures were below freezing
A method returning an array of temperatures above 100 degrees
A method returning the largest change in temperature between any two consecutive days
A method returning an array of daily temperatures, sorted in descending order
Write a client class to test all the methods in your class.
Option A: Java
Option B: Python
Option C: C++
Option D: JavaScript

User Adeel Turk
by
8.1k points

1 Answer

5 votes

Final answer:

To create a class encapsulating the concept of daily temperatures for a week, define a class called 'Temperatures' with various methods such as a constructor, accessor, mutator, tostring, and equals methods. Additionally, include methods to count temperatures below freezing, identify temperatures above 100 degrees, calculate the largest change in temperature between consecutive days, and sort the temperatures in descending order. Finally, create a client class to test all the methods.

Step-by-step explanation:

To create a class encapsulating the concept of daily temperatures for a week, you can define a class called 'Temperatures' with the following methods:

  1. Constructor: Accepts an array of seven temperatures as a parameter.
  2. Accessor method: Retrieves the temperatures.
  3. Mutator method: Modifies the temperatures.
  4. toString method: Converts the temperatures to a string representation.
  5. equals method: Compares two instances of the class for equality.
  6. Method returning the count of temperatures below freezing: Iterates over the temperatures and counts the number below freezing.
  7. Method returning an array of temperatures above 100 degrees: Iterates over the temperatures and adds those above 100 degrees to a new array.
  8. Method returning the largest change in temperature between consecutive days: Compares the temperature differences between consecutive days and finds the largest change.
  9. Method returning an array of daily temperatures, sorted in descending order: Sorts the temperatures in descending order.

A client class can then be created to test all the methods in the 'Temperatures' class.

User Alexglue
by
7.9k points