Activity: Conditionals and Loops II
Activity: Conditionals and Loops II
Page 1 of 4
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
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.
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
when declaring the ArrayList in interactions.
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:
• 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.