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

Python The average monthly low and high temperatures in Pullman are given below

ID: 3605969 • Letter: P

Question

Python

The average monthly low and high temperatures in Pullman are given below in degrees Fahrenheit

For this task, create two lists, one with the low temperatures and one with the high temperatures, and then use a for-loop, sorting, slicing, summing, and anything else necessary to answer the following questions:

1. What are the average temperature differences for all the months of the year? Use a for-loop to create a new list that contains these differences. Which month has the largest temperature difference and which one has the smallest temperature difference?

2. What are the annual averages (not monthly) for the low and high temperatures?

3. What are the annual averages for the low and high temperatures if you remove the lowest and highest temperatures from each?

Month Low High January 26 37 February 27 41 March 32 49 April 36 57 May 42 65 June 47 72 July 50 83 August 50 84 September 44 74 October 30 60 November 31 44 December 24 35

Explanation / Answer

import java.util.*;

public class Temperature{

static ArrayList ar_low=new ArrayList();

static ArrayList ar_high=new ArrayList();

static ArrayList ar_diff=new ArrayList();

public static void init(){

ar_low.add(26);

ar_low.add(27);

ar_low.add(32);

ar_low.add(36);

ar_low.add(42);

ar_low.add(47);

ar_low.add(50);

ar_low.add(50);

ar_low.add(44);

ar_low.add(30);

ar_low.add(31);

ar_low.add(24);

ar_high.add(37);

ar_high.add(41);

ar_high.add(49);

ar_high.add(57);

ar_high.add(65);

ar_high.add(72);

ar_high.add(83);

ar_high.add(84);

ar_high.add(74);

ar_high.add(60);

ar_high.add(44);

ar_high.add(35);

}

static int lowest=100;

static int highest=0;

public static void main(String []args){

init();

for (int i = 0; i < ar_high.size(); i++) {

ar_diff.add(i,((Integer)ar_high.get(i)).intValue()-((Integer)ar_low.get(i)).intValue());

}

int highest_diff=0;

int lowest_diff=100;

int highest_month=0;

int lowest_month=0;

for (int i = 0; i < ar_diff.size(); i++) {

if(((Integer)ar_diff.get(i)).intValue()>highest_diff){

highest_diff=((Integer)ar_diff.get(i)).intValue();

highest_month=i;

}

if(((Integer)ar_diff.get(i)).intValue()<lowest_diff){

lowest_diff=((Integer)ar_diff.get(i)).intValue();

lowest_month=i;

}

}

System.out.println("Average temperature difference: " + average(ar_diff));

System.out.println("Highest temperature difference: " +highest_diff + " in month: " + (highest_month+1));

System.out.println("Lowest temperature difference: " +lowest_diff + " in month: " + (lowest_month+1));

System.out.println("Average low temperature : " + average(ar_low));

System.out.println("Average high temperature : " + average(ar_high));

Collections.sort(ar_low);

ar_low.remove(11);

ar_low.remove(0);

Collections.sort(ar_high);

ar_high.remove(11);

ar_high.remove(0);

System.out.println("After removing highest and lowest temperatures from each:");

System.out.println("Average low temperature : " + average(ar_low));

System.out.println("Average high temperature : " + average(ar_high));

}

public static double average(ArrayList ar){

int sum=0;

for (int i = 0; i < ar.size(); i++) {

sum=sum+((Integer)ar.get(i)).intValue();

}

return (double)sum/ar.size();

}

}

Output:

----------