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:
Dont 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
Dont 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:
MMM -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 MMM Low is -10
MEnter choice [L]ow temp, [H]igh temp, [P]rint, [E]nd: H MMM High is 95
MEnter choice [L]ow temp, [H]igh temp, [P]rint, [E]nd: P
MMM Temperatures: [34, 56, 78, -10, 95]
MMM Low: -10
MMM High: 95
MEnter choice [L]ow temp, [H]igh temp, [P]rint, [E]nd: E MMM Done MMM MMM -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.
Reviews
There are no reviews yet.