Rework Exercise 2 so that it will ask the user to enter in the temperatures of t
ID: 3776958 • Letter: R
Question
Rework Exercise 2 so that it will ask the user to enter in the temperatures of the first week (7 days) of January 2015 which will be stored in an array called jan15. Then ask the user to enter the temperatures of the first week (7 days) of January 2016 which will be stored in an array called jan16. The program will then print out the temperatures for each day for both January 2015 and 2016 and give the difference in temperature per day. You can NOT assume that all temperatures are positive. See how the program will work below. Test the program with two sets of data.Explanation / Answer
Exercise2.java
import java.util.Scanner;
public class Exercise2 {
public static void main(String[] args) {
double jan15[] = new double[7];
double jan16[] = new double[7];
Scanner kb = new Scanner(System.in);
for(int i=0; i<jan15.length; i++){
System.out.print("Enter the temperature for Jan. "+(i+1)+" 2015: ");
jan15[i] = kb.nextDouble();
}
System.out.println();
for(int i=0; i<jan16.length; i++){
System.out.print("Enter the temperature for Jan. "+(i+1)+" 2016: ");
jan16[i] = kb.nextDouble();
}
for(int i=0; i<jan16.length; i++){
System.out.println("Day "+(i+1)+" 2015: "+jan15[i]+" "+"Day "+(i+1)+" 2016: "+jan16[i]+" "+"Difference: "+Math.abs(jan15[i]-jan16[i]));
}
}
}
Output:
Enter the temperature for Jan. 1 2015: 2
Enter the temperature for Jan. 2 2015: 4
Enter the temperature for Jan. 3 2015: -5
Enter the temperature for Jan. 4 2015: 1
Enter the temperature for Jan. 5 2015: 0
Enter the temperature for Jan. 6 2015: -2
Enter the temperature for Jan. 7 2015: -3
Enter the temperature for Jan. 1 2016: 5
Enter the temperature for Jan. 2 2016: 2
Enter the temperature for Jan. 3 2016: 1
Enter the temperature for Jan. 4 2016: -10
Enter the temperature for Jan. 5 2016: -4
Enter the temperature for Jan. 6 2016: -5
Enter the temperature for Jan. 7 2016: 2
Day 1 2015: 2.0 Day 1 2016: 5.0 Difference: 3.0
Day 2 2015: 4.0 Day 2 2016: 2.0 Difference: 2.0
Day 3 2015: -5.0 Day 3 2016: 1.0 Difference: 6.0
Day 4 2015: 1.0 Day 4 2016: -10.0 Difference: 11.0
Day 5 2015: 0.0 Day 5 2016: -4.0 Difference: 4.0
Day 6 2015: -2.0 Day 6 2016: -5.0 Difference: 3.0
Day 7 2015: -3.0 Day 7 2016: 2.0 Difference: 5.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.