$25
In this activity you will create two classes. Temperatures will hold a set of integer values representing daily temperatures. TemperatureInfo will allow users to interact with the Temperatures class.
Directions:
Don’t forget to add your Javadoc comments for your classes, constructor, and methods in this activity.
Part 1: Temperatures: instance variable, method stubs
• Create a class called Temperatures, which will hold a set of integer values representing daily temperatures.
• Add an instance variable with the name temperatures to your class that is of type ArrayList with generic type Integer.
• Add method stubs for the following methods.
o The constructor takes an ArrayList of integer values
!!! o getLowTemp: takes no parameters; returns an integer value o getHighTemp: takes no parameters; returns an integer value o lowerMinimum: takes an int parameter; returns an integer value o higherMaximum: takes an int parameter; returns an integer value o toString: no parameters; returns a String
Part 2: Temperatures: constructor, getLowTemp
• In your constructor, set temperatures equal to temperaturesIn.
• In getLowTemp, first return 0 if the ArrayList is empty:
• Now iterate through the entire list and find the lowest temperature:
Finally, after the loop, return the lowest temperature:
Part 3: getHighTemp
• In getHighTemp, again return 0 if there are no temperatures in the ArrayList:
• This time, use a for each loop to iterate through the list of temperatures to find the highest temperature.
• Add code to the toString method to return a string containing the low and high temperatures (hint: make a method call to getLowTemp and getHighTemp):
Part 4: lowerMinimum & higherMaximum
• The lowerMinimum method takes an int value and returns the parameter if it is lower than the value returned by getLowTemp. Otherwise, it returns the return of getLowTemp.
• The higherMaximum method takes an int value and returns the parameter if it is greater than than the value returned by getHighTemp. Otherwise, it returns the return of getHighTemp.
• Test your methods in the interactions pane:
Note that you should not use the generic <Integer when declaring the ArrayList in interactions.
ϼÏÏimport java.util.ArrayList;
ϼÏÏArrayList tempList = new ArrayList();
ϼÏÏtempList.add(34);
ϼÏÏtempList.add(52);
ϼÏÏtempList.add(36);
ϼÏÏtempList.add(65);
ϼÏÏTemperatures temps = new Temperatures(tempList);
ϼÏÏtemps.getLowTemp() ÏÏÏÏ34
ϼÏÏtemps.getHighTemp() ÏÏÏÏ65
ϼÏÏtemps.lowerMinimum(33) ÏÏÏÏ33
ϼÏÏtemps.lowerMinimum(35) ÏÏÏÏ34
ϼÏÏtemps.higherMaximum(64) ÏÏÏÏ65
ϼÏÏtemps.higherMaximum(67)
ÏÏÏÏ67
Part 5: TemperatureInfo
Don’t forget to add your Javadoc comments; the class and main method will need one.
• Download the canvas file for this activity and save it in same folder as the classes for this activity. After you have created and compiled part or all the main method described below, use the canvas file in conjunction with debugger and/or run the program in canvas mode.
• Create a class called TemperatureInfo with a main method. Declare and instantiate a Scanner object called userInput that reads from System.in. Declare and instantiate an ArrayList with generic type Integer called tempsList. Remember to import the Scanner and ArrayList classes.
• Create the following do while loop that will read in temperature values, one per line, and add each to tempsList until the user presses enter with no value to indicate that there are no more temperatures to be input. After all of the temperatures have been read in and added to tempsList, create a Temperatures object with the tempsList:
• Create a menu by using a do while loop that contains a switch statement to select among user choices for the following: [L]ow temp, [H]igh temp, [P]rint, [E]nd
where ‘L’ prints the low temperature, ‘H’ prints the high temperature, ‘P’ prints the Temperatures object temps, ‘E’ ends the the program (i.e., ends the do while loop).
Run your program as below to test its output:
MM«M ----jGRASP exec: java TemperatureInfo
¼¼§MEnter a temperature (or nothing to end list): 34
¼¼§MEnter a temperature (or nothing to end list): 56
¼¼§MEnter a temperature (or nothing to end list): 78
¼¼§MEnter a temperature (or nothing to end list): -10 ¼¼§MEnter a temperature (or nothing to end list): 95
¼¼§MEnter a temperature (or nothing to end list):
¼¼§MEnter choice - [L]ow temp, [H]igh temp, [P]rint, [E]nd: L MM§M Low is -10
¼¼§MEnter choice - [L]ow temp, [H]igh temp, [P]rint, [E]nd: H MM§M High is 95
¼¼§MEnter choice - [L]ow temp, [H]igh temp, [P]rint, [E]nd: P
MM§M Temperatures: [34, 56, 78, -10, 95]
MM§M Low: -10
MM§M High: 95
¼¼§MEnter choice - [L]ow temp, [H]igh temp, [P]rint, [E]nd: E MM§M Done MM§M MM©M ----jGRASP: operation complete.
• Run your program in Canvas mode using the canvas file you downloaded. Be sure to wait for the program to ask for input before keying in the temperature and pressing ENTER. Remember that you can control the speed with the delay slider in canvas window or debug tab.