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 (7th Edition)
- Please 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_forwardEnter a list of at least three elements for y such that the value of x is 10 when the loop completes. x = 0 for value in y: if len(value > x: x = len(value)arrow_forwardExplain the main difference between a while-loop and a for-loop? When would you used one over the other?arrow_forward
- explain the use of voids and why do we use it. explain also if else loopsarrow_forwardFix the codearrow_forward***NEED HELP WITH THIS**** write a code that replicates the list's insert() method. Recall that insert(index, item) method inserts the item at specified index in the list. Write a code that: Asks the user to input 5 items in a list and display the list. Ask the user to input the index where they would like to insert a new item. Ask the user to input the item that they would like to insert into the list. Inserts the new item into the list and displays the modified list. If the index is greater than the length of the list, simply insert the new item at the end of list. Also i dont need to use the list's insert() method to do this program. I dont need to write a function, just the code to replicate the functionality of insert() method.arrow_forward
- Language: Python with Turtle Write a program that will have Turtle draw a row of 40 circles across the canvas starting at the left edge. They must not overlap and there should not be a line connecting them. Make sure that: You use a for loop for any code that repeats Each circle has a radius of 10 There are 20 circles drawn along the x-axis Hint: Remember that in order to have the circles sit next to one another, you need to move forward the diameter between each circle.arrow_forward* allSame returns true if all of the elements in list have the same value. * allSame returns false if any two elements in list have different values. * The array may be empty and it may contain duplicate values. * * Your solution should contain at most one loop. You may not use recursion. * Your solution must not call any other functions. * Here are some examples (using "==" informally): * * * * * * * * true == allSame (new double[] { }) true == allSame(new double[] {11}) true == allSame (new double[] { 11, 11, 11, 11 }) false == allSame(new double[] { 11, 11, 11, 22 }) false == allSame (new double[] { 11, 11, 22, 11 }) true == allSame (new double[] { 22, 22, 22, 22 }) * */ public static boolean allSame (double[] list) { return StdRandom.bernoulli(); //TODO: fix thisarrow_forwardCan you please write the follwing in Java. You will implement this program in a specific way in order to gain some experience with loops, arrays and array lists. Use an array of strings to store the 4 strings listed in the description. Use a do-while loop for your 'game engine'. This means the game starts once the user enters money. The decision to stop occurs at the bottom of the loop. The do-while loop keeps going until the user quits, or there is no money left. The pseudocode for this 'game engine' is shown below: determine the fruits to display (step 3 below) and print them determine if there are 3 or 4 of the same image display the results update the customer balance as necessary prompt to play or quit continue loop if customer wants to play and there's money for another game. Use the Random class to generate a random number between 0 and 3. This random number will be an index into the array of strings. Add the string at that index to an ArrayList. You'll have to do…arrow_forward
- Complete the following sentence. There are two basic forms of loop constructs: while loops and loops.arrow_forwardprogram5_2.pyWrite another program that generates another table with the same columns and decimals but by using a function that returns the kilometers for a specified miles parameter. Use a loop in main and generate random integers as before, but catch the value returned by the function and use it in the loop to print a line of the table. Repeat: The table is printed in main.arrow_forwardYou had a set of string and your task is to convert it to array and print the array without using loop in kotlinarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,