Question
I need help getting the correct output using the provided code listen below! Temperature Format Whenever printing the temperature, it should only display one decimal
I need help getting the correct output using the provided code listen below!
Temperature Format
Whenever printing the temperature, it should only display one decimal place. Meaning, 72.2342343 should print as 72.2.
Use printf() as discussed in class to accomplish this.
Table Format
The format of your table for displayTemperatures() should appear as follows:
Day Temp ---------------- Sunday 93.6 Monday 61.1 Tuesday 145.1 Wednesday 90.3 Thursday 139.1 Friday 140.0 Saturday -20.1
The day column should be 10 spaces wide, such that the following temperatures all begin at the same place.
Note that the index of the days array and the index of the temperatures array is related. days[0], being Sunday, is associated with the temperature temperatures[0]. So it goes.
Use printf() as discussed in class to accomplish this.
#### Magic Numbers
Do not use any magic numbers or strings, consider using the length of days to set the length of temperatures.
WeatherForWeek class
public class WeatherForWeek { private final String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; private float[] temperatures; /** * Constructor method: Initializes temperatures[] to a new float array that is * the same length as the number of days. * The initial values for each temperature should be 0.0. */ public WeatherForWeek() { // TODO implement me } /** * Overloaded constructor method: initializes temperatures[] to a new float * array that is the same length as the number of days. * Then, call the setTemperatures method to set the temperatures. * * @param temperatures * an array containing the temperatures to be set. */ public WeatherForWeek(float[] temperatures) { // TODO implement me } /** * getter method: Returns the temperatures array. * * @return */ public float[] getTemperatures() { // TODO implement me return null; } /** * setter method: Sets the temperatures IF the length is the same as the length * of the days array and the temperature falls within the valid range. * * @param temperatures * an array of temperatures. */ public void setTemperatures(float[] temperatures) { // TODO implement me } /** * Displays the temperatures in a table format. See lab instructions for format * of the table. */ public void displayTemperatures() { // TODO implement me } /** * Displays the coldest temperature and day of week. */ public void displayLowestTemperature() { // TODO implement me } /** * Displays the hottest temperature and day of week. */ public void displayHighestTemperature() { // TODO implement me } /** * Displays the average temperature of the week. */ public void displayAverageTemperature() { // TODO implement me } }
Client Class
import java.util.Random; public class WeatherForWeekClient { private final static int WEATHERS = 3; private final static int DAYS = 7; public static void main(String[] args) { WeatherForWeek[] w4ws = new WeatherForWeek[WEATHERS]; Random rand = new Random(1234567890); for (int i = 0; i < w4ws.length; ++i) { float[] temps = new float[DAYS]; for (int j = 0; j < temps.length; ++j) { float temp = rand.nextFloat() * 200.0f - 50.0f; temps[j] = temp; } System.out.printf("----- Week %d -------------------------------------------------- ", i + 1); w4ws[i] = new WeatherForWeek(temps); w4ws[i].displayTemperatures(); w4ws[i].displayLowestTemperature(); w4ws[i].displayHighestTemperature(); w4ws[i].displayAverageTemperature(); } } }
Expected Output
----- Week 1 -------------------------------------------------- Day Temp ---------------- Sunday 93.6 Monday 61.1 Tuesday 145.1 Wednesday 90.3 Thursday 139.1 Friday 140.0 Saturday -20.1 The coldest day was Saturday with a temperature of -20.1. The hottest day was Tuesday with a temperature of 145.1. The average temperature was 92.7 ----- Week 2 -------------------------------------------------- Day Temp ---------------- Sunday 112.6 Monday 130.2 Tuesday 146.8 Wednesday -1.0 Thursday 38.5 Friday 52.8 Saturday 46.4 The coldest day was Wednesday with a temperature of -1.0. The hottest day was Tuesday with a temperature of 146.8. The average temperature was 75.2 ----- Week 3 -------------------------------------------------- Day Temp ---------------- Sunday -17.3 Monday 49.1 Tuesday 66.1 Wednesday 65.2 Thursday 36.8 Friday 120.1 Saturday 33.3 The coldest day was Sunday with a temperature of -17.3. The hottest day was Friday with a temperature of 120.1. The average temperature was 50.5 Error: temperature -51.0 out of range. Error: temperature 151.0 out of range.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started