42.1k views
5 votes
Write a class encapsulating the concept of daily temperatures for a week with a single dimensional array of temperatures. 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 (use one of the sorting algorithms in the textbook. Write a client class to test all the methods in your class.

User Burg
by
7.9k points

1 Answer

3 votes

Final answer:

A class called Temperature is created to store daily temperatures for a week, with methods to handle temperature data, including finding below freezing and above 100-degree temperatures. A client class tests these functionalities.

Step-by-step explanation:

Implementing a Temperature Class

To encapsulate daily temperatures for a week, we create a class with a constructor that takes an array of seven temperatures. This class includes accessor and mutator methods for manipulating individual temperatures, a toString() method for a string representation of the temperatures, and an equals() method to compare two temperature objects. Additionally, we implement a method to count temperatures below freezing, a method to retrieve temperatures above 100 degrees, one to find the largest change in temperature between consecutive days, and a method to sort the temperatures in descending order. Finally, a separate client class is used to test all the methods.

Client Class for Testing

The client class includes a main method where instances of the temperature class are created using sample data. It then calls the methods within the temperature class to demonstrate functionality, such as retrieving sorted temperatures or finding changes in temperature.

User Richie Bendall
by
8.2k points