Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need assistance with a quick coding activity that centers itself around loops. Thanks in advance! Activity: Conditionals and Loops II Page 1 of 4 Goals:

Need assistance with a quick coding activity that centers itself around loops. Thanks in advance!

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Activity: Conditionals and Loops II Page 1 of 4 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. 0 The constructor takes an ArrayList of integer values public Temperatures (ArrayList temperatures In) { o o o getLow Temp: takes no parameters; returns an integer value getHigh Temp: takes no parameters; returns an integer value lowerMinimum: takes an int parameter, returns an integer value higher Maximum: takes an int parameter, returns an integer value toString: no parameters, returns a String O Part 2: Temperatures: constructor, get Low Temp In your constructor, set temperatures equal to temperatures in. In getLow Temp, first return 0 if the ArrayList is empty: if (temperatures.isEmpty()) { return 0; Now iterate through the entire list and find the lowest temperature: int low = temperatures.get(0); for (int i = _; i++) { if (temperatures.get(i) high) { high = _ return high; Add code to the toString method to return a string containing the low and high temperatures (hint: make a method call to getLow Temp and getHighTemp): public String toString({ return \tTemperatures: *+ temperatures + " \tLow: - + getLowTempo) + " \tHigh: + getHighTemp(); Part 4: lower Minimum & higher Maximum The lower Minimum method takes an int value and returns the parameter if it is lower than the value returned by getLow Temp. Otherwise, it returns the return of getLow Temp. public int lowerMinimum (int lowin) { return lowIn 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); Page 2 of 4 Activity: Conditionals and Loops II Page 3 of 4 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 Temperature Info with a main method. Declare and instantiate a Scanner object called user Input 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 temps List 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 temps List, create a Temperatures object with the tempsList: String temp Input = ""; do { System.out.print("Enter a temperature (or nothing to end list): "); temp Input = userInput.nextLine().trim(); if (!tempInput.equals("")) { tempsList.add(Integer.parseInt(tempInput)); } while (!tempInput.equals("")); Temperatures temps = new Temperatures (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). Activity: Conditionals and Loops II Page 4 of 4 char choice= 'E'; do { System.out.print("Enter choice - [L]ow temp. [H]igh temp. [P]rint, [E]nd: "); choice = user Input.nextLine().toUpperCase().charAt(0); switch (choice) { casc 'L': System.out.println("\tLow is - + temps. break; case 'H': System.out.println("\tHigh is " + break; _.getHighTemp()); case 'P': System.out.println(temps); break; case 'E System.out.println("\tDone"); break; default: System.out.println("\tInvalid choice!"); } while (choice != 'B'); Run your program as below to test its output: ----jGRASP exec: java TemperatureInfo Enter a temperature (or nothing to end list): 34 Enter a temperature (or nothing to end list): 56 Enter a temperature (or nothing to end list): 78 Enter a temperature (or nothing to end list): -10 Enter a temperature (or nothing to end list): 95 Enter a temperature (or nothing to end list): Enter choice - [L]ow temp, High temp, [Print, [E]nd: L Low is -10 Enter choice - [L]ow temp. [High temp, [P]rint, [B]nd: H High is 95 Enter choice - [L]ow temp. [High temp. [Print, [End: P Temperatures: [34, 56, 78, -10, 95] Low: -10 High: 95 Enter choice - [L]ow temp. [High temp. [Print, [E]nd: E Done ----GRASP: 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. Page 4 of 4 Activity: Conditionals and Loops II Page 1 of 4 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. 0 The constructor takes an ArrayList of integer values public Temperatures (ArrayList temperatures In) { o o o getLow Temp: takes no parameters; returns an integer value getHigh Temp: takes no parameters; returns an integer value lowerMinimum: takes an int parameter, returns an integer value higher Maximum: takes an int parameter, returns an integer value toString: no parameters, returns a String O Part 2: Temperatures: constructor, get Low Temp In your constructor, set temperatures equal to temperatures in. In getLow Temp, first return 0 if the ArrayList is empty: if (temperatures.isEmpty()) { return 0; Now iterate through the entire list and find the lowest temperature: int low = temperatures.get(0); for (int i = _; i++) { if (temperatures.get(i) high) { high = _ return high; Add code to the toString method to return a string containing the low and high temperatures (hint: make a method call to getLow Temp and getHighTemp): public String toString({ return \tTemperatures: *+ temperatures + " \tLow: - + getLowTempo) + " \tHigh: + getHighTemp(); Part 4: lower Minimum & higher Maximum The lower Minimum method takes an int value and returns the parameter if it is lower than the value returned by getLow Temp. Otherwise, it returns the return of getLow Temp. public int lowerMinimum (int lowin) { return lowIn 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); Page 2 of 4 Activity: Conditionals and Loops II Page 3 of 4 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 Temperature Info with a main method. Declare and instantiate a Scanner object called user Input 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 temps List 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 temps List, create a Temperatures object with the tempsList: String temp Input = ""; do { System.out.print("Enter a temperature (or nothing to end list): "); temp Input = userInput.nextLine().trim(); if (!tempInput.equals("")) { tempsList.add(Integer.parseInt(tempInput)); } while (!tempInput.equals("")); Temperatures temps = new Temperatures (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). Activity: Conditionals and Loops II Page 4 of 4 char choice= 'E'; do { System.out.print("Enter choice - [L]ow temp. [H]igh temp. [P]rint, [E]nd: "); choice = user Input.nextLine().toUpperCase().charAt(0); switch (choice) { casc 'L': System.out.println("\tLow is - + temps. break; case 'H': System.out.println("\tHigh is " + break; _.getHighTemp()); case 'P': System.out.println(temps); break; case 'E System.out.println("\tDone"); break; default: System.out.println("\tInvalid choice!"); } while (choice != 'B'); Run your program as below to test its output: ----jGRASP exec: java TemperatureInfo Enter a temperature (or nothing to end list): 34 Enter a temperature (or nothing to end list): 56 Enter a temperature (or nothing to end list): 78 Enter a temperature (or nothing to end list): -10 Enter a temperature (or nothing to end list): 95 Enter a temperature (or nothing to end list): Enter choice - [L]ow temp, High temp, [Print, [E]nd: L Low is -10 Enter choice - [L]ow temp. [High temp, [P]rint, [B]nd: H High is 95 Enter choice - [L]ow temp. [High temp. [Print, [End: P Temperatures: [34, 56, 78, -10, 95] Low: -10 High: 95 Enter choice - [L]ow temp. [High temp. [Print, [E]nd: E Done ----GRASP: 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. Page 4 of 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions