Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program, no user defined class required, that asks the user how many day

ID: 3624112 • Letter: W

Question

Write a program, no user defined class required, that asks the user how many days worth of data for high temperature for each day there are, verifying the user enters a positive integer. Then prompt the user for each real-valued high temperature,
one by one as shown below. Calculate and output the maximum change (in absolute value) in the day to day high temperature and output on which two days this maximum change occured.
Sample output looks like this: (and user passes in the values desired)
How many days worth of high temperatures do you have to enter? 5
Enter high temperature for day #1: 95.0
Enter high temperature for day #2: 92.0
Enter high temperature for day #3: 68.0
Enter high temperature for day #4: 72.0
Enter high temperature for day #5: 70.0
The maximum day to day high temperature change is -24.0 and occured between days 2 and 3

My code looks like this I have problems with the for loop that goes through the temperature array. I cannot get it to pause and then wait for my to input a value. Also it produces one less. Find my code attached. Pls correct the main method and the find maxChange method accordingly:
public class Temperatures PL
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many days worth of high temperature do you have to enter? ");
int days = input.nextInt();
double[] temperatures = new double[days];
for (days = 1; days < temperatures.length; days++)
{
System.out.println("Enter high temperature for day #" + days + ": "+ input.nextDouble() + " ");
}
maxChange(temperatures);
}

public static void maxChange(double[] temperatures)
{
double maxDifference = temperatures[0] - temperatures[1];
int i;
for (i = 0; i < temperatures.length - 1; i++)
{
if(temperatures[i] - temperatures[i+1] > maxDifference)
maxDifference = temperatures[i] - temperatures[i+1];
}
System.out.println("The maximum day to day high temperature change is " +maxDifference+ " and occured between days "+ i +" and " +i + 1);
}
}

Explanation / Answer


import java.util.Scanner;
public class Temperatures
{
public static void main (String[] args)
{

Scanner input = new Scanner(System.in);
System.out.print("How many days worth of high temperature do you have to enter? ");
int days = input.nextInt();
while(days<=0)
{
    System.out.println("Negative Numbers are not allowed Plz try again:");
    days = input.nextInt();
}
double[] temperatures = new double[days];
for (days = 0; days < temperatures.length; days++)
{
System.out.print("Enter high temperature for day #" + (days+1) + ": ");
temperatures[days]= input.nextDouble();

}
maxChange(temperatures);
}

public static void maxChange(double[] temperatures)
{
double maxDifference = temperatures[0] - temperatures[1];
int k=0;
int i;
for (i = 1; i < temperatures.length - 1; i++)
{
if(temperatures[i] - temperatures[i+1] > maxDifference)
{
    k = i;
maxDifference = temperatures[i] - temperatures[i+1];
}
}
System.out.println("The maximum day to day high temperature change is " +maxDifference+ " and occured between days "+ (k+1) +" and " +(k+2));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote