Explanation of Solution
Modified last loop in Listing 7.8:
The last loop in Listing 7.8 is modified by using “for” loop with method “getNumberOfEntries” which is given below:
/* Compute the number of entries in the list using method "getNumberOfEntries" */
int count = toDoList.getNumberOfEntries();
/* Finally display the number of items in list using "for" loop */
for(int position = toDoList.START_POSITION; position <= count; position++)
//Display the list of items
System.out.println(toDoList.getEntryAt(position));
Explanation:
The modified loop is used to display the each item in the list using method “getNumberOfEntries”.
- • First compute the number of entries in the list using method “getNumberOfEntries” and store it to an integer variable “count”.
- • Then display the item one by one in the list by using “for” loop.
Complete code after changing the given loop:
The complete executable code for given program after changing the last loop in Listing 7.8 is given below:
File name: “OneWayNoRepeatsList.java”
public class OneWayNoRepeatsList
{
//Refer the textbook Listing 7.9 of chapter 7.
}
File name: “ListDemo.java” (Listing 7.8)
//Import required package
import java.util.Scanner;
//Define "ListDemo" class
public class ListDemo
{
//Initializes required variable
public static final int MAX_SIZE = 3; //Assumed > 0
//Define main function
public static void main(String[] args)
{
//Create object "toDoList" from "OneWayNoRepeatsList" class
OneWayNoRepeatsList toDoList = new OneWayNoRepeatsList(MAX_SIZE);
//Prompt statement for user
System.out.println("Enter items for the list, when prompted.");
//Initializes Boolean variable
boolean moreEntries = true;
//Assign string variable
String next = null;
//Create object for scanner class
Scanner keyboard = new Scanner(System.in);
//Read an item from user
while (moreEntries && !toDoList...
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Seld the loop.arrow_forwardIn c++ Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, th backwards. End each loop with a newline. Ex: If courseGrades (7, 9, 11, 10), print: - 7 9 11 10 10 11 9 7 Hint Use two for loops. Second loop starts with i=NUM VALS-1. (Notes) Note: These activities may test code with different test values. This activity will perform two tests, both with a 4-element array (int courseGrades[4]) See How to Use zyBooks" Also note: If the submitted code tries to access an invalid array element, such as courseGrades[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message 2 using namespace std; 3 4 int main() { 5 6 7 8 9 10 11 12 13 14 const int NUM VALS-4; int courseGrades [NUM VALS]; int i; for (i-8; 1 NUM VALS; ++) { cin>> courseGrades[1]; } Your solution goes…arrow_forwardwrite a program that uses a for loop. You’ll need to be able to explain it (not just send the code). In particular, explain how the loop works, and how the different forms of loop (for, for/list, for/vector, etc) are different from one another (this will require some experimentation to find out what each one does). Using racketarrow_forward
- Create a class NestedForPatternB. Use nested for-loops that display the following pattern:arrow_forwardCreate a while loop that finds a product of all even numbers from 2 to 12.arrow_forwardWrite code that uses any type of loop. The code should continually ask for a user input and sums all user inputs that are divisible by 2. The loop should continue until the user enters a negative number. The code should output the sum a single time once user entry has completed. You may assume all libraries and namespaces have been previously written into the code, you are just writing everything that would go inside the main function (beyond the return 0:).arrow_forward
- Write a for loop that computes the sum of the integers from 1 to n. Get the value of n from the user.arrow_forwardWrite a loop of your choice that continues to read an integer x as long as x is not in the range of [5,10]. Complete the missing code in the space provided below. int x=1;arrow_forwardPlease unroll the following loop three times. What is the benefit of loop unrolling? for (i=0; i<42; i++) { a[i] = b[i] * i;}arrow_forward
- The for loop iterates across the elements in a given list. So long as there are objects to process, it will keep going. In what way does this statement hold true or false?arrow_forwardAssignment Use python Write your answers in the text entry and submit the assignment. Given: items = ["mask", "gloves", "soap"] Respond to the following: What line of code could you use to add "sanitizer" to the list of items? What 2 lines of code could you use to ask the user for another item to add, and then add that item to the list as well? Write a loop that will print to the screen "I've got ______" for every item in the list of items. Sample output: I’ve got mask. I’ve got gloves. I’ve got soap. I’ve got sanitizer. I’ve got . Note: There is a period at the end of each line.arrow_forwardPizzas: Think of at least three kinds of your favorite pizza. Store these pizza names in a list, and then use a for loop to print the name of each pizza. Modify your for loop to print a sentence using the name of the pizza instead of printing just the name of the pizza. For each pizza you should have one line of output containing a simple statement like I like pepperoni pizza. Add a line at the end of your program, outside the for loop, that states how much you like pizza. The output should consist of three or more lines about the kinds of pizza you like and then an additional sentence, such as I really love pizza!arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education