Activity: Conditionals and Loops II Solution

$35.00 $29.05

You'll get a: . zip file solution, download link after Payment

Description

Goals:

By the end of this activity you should be able to do the following:

  • Understand the basics of the ternery conditional operator

  • Understand the basics of the for loop, for each loop, and the do-while loop

  • Understand the basics of the switch statement

Description:

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.

  1. The constructor takes an ArrayList of integer values

lu6292cur8h tmp 84f8ed9a021fd520

!!!

  1. 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:

lu6292cur8h tmp bca7d3f7762cb062 lu6292cur8h tmp df1104be5e45fc6f

Finally, after the loop, return the lowest temperature:

lu6292cur8h tmp e1802aaa307d1ccd lu6292cur8h tmp c6dc708a7c315217

Page 1 of 4

Activity: Conditionals and Loops II Page 2 of 4

lu6292cur8h tmp cdc2f9164483b6f9

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.

lu6292cur8h tmp 7d4ffb40bf59e2f3 lu6292cur8h tmp 8133cabfce7698af

  • 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):

lu6292cur8h tmp a313991b94051ae6

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);

lu6292cur8h tmp e1802aaa307d1ccd lu6292cur8h tmp 1c4cb9199cc7519d lu6292cur8h tmp 8e3e198668f15238 lu6292cur8h tmp 8688d9468cdf6041

Page 2 of 4

Activity: Conditionals and Loops II Page 3 of 4

lu6292cur8h tmp cdc2f9164483b6f9

ϼÏÏtemps.getLowTemp()

lu6292cur8h tmp 4cf74281aec2a53c

ÏÏÏÏ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:

lu6292cur8h tmp e52f3da8c0fa2489

  • 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).

lu6292cur8h tmp e1802aaa307d1ccd

Page 3 of 4

Activity: Conditionals and Loops II Page 4 of 4

lu6292cur8h tmp a9bc0a2fa400d1c7

Run your program as below to test its output:

MM«M —-jGRASP exec: java TemperatureInfo

lu6292cur8h tmp 4ba163cdd2f011d8

¼¼§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.

  • Finally, submit your .java files to Web-CAT.

lu6292cur8h tmp e1802aaa307d1ccd

Page 4 of 4