Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 7.3, Problem 15STQ

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...

Blurred answer
Students have asked these similar questions
What the following code does? Replace the while loop with a for loop in the code:   int s=0, i=2; while(i<=200) {    s=s+i; i=i+2; }
In 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…
Write 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:).

Chapter 7 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Ch. 7.2 - Give the definition of a static method called...Ch. 7.2 - Prob. 12STQCh. 7.2 - The following method compiles and executes but...Ch. 7.2 - Suppose that we add the following method to the...Ch. 7.3 - Prob. 15STQCh. 7.3 - Replace the last loop in Listing 7.8 with a loop...Ch. 7.3 - Suppose a is an array of values of type double....Ch. 7.3 - Suppose a is an array of values of type double...Ch. 7.3 - Prob. 19STQCh. 7.3 - Consider the partially filled array a from...Ch. 7.3 - Repeat the previous question, but this time assume...Ch. 7.3 - Write an accessor method getEntryArray for the...Ch. 7.4 - Prob. 23STQCh. 7.4 - Write the invocation of the method selectionSort...Ch. 7.4 - How would you need to change the method...Ch. 7.4 - How would you need to change the method...Ch. 7.4 - Consider an array b of int values in which a value...Ch. 7.5 - What output is produced by the following code?...Ch. 7.5 - Revise the method showTable in Listing 7.13 so...Ch. 7.5 - Write code that will fill the following array a...Ch. 7.5 - Write a void method called display such that the...Ch. 7.6 - Prob. 33STQCh. 7 - Write a program in a class NumberAboveAverage that...Ch. 7 - Write a program in a class CountFamiles that...Ch. 7 - Write a program in a class CountPoor that counts...Ch. 7 - Write a program in a class FlowerCounter that...Ch. 7 - Write a program in a class characterFrequency that...Ch. 7 - Create a class Ledger that will record the sales...Ch. 7 - Define the following methods for the class Ledger,...Ch. 7 - Write a static method isStrictlyIncreasing (double...Ch. 7 - Write a static method removeDuplicates(Character[]...Ch. 7 - Write a static method remove {int v, int [] in}...Ch. 7 - Suppose that we are selling boxes of candy for a...Ch. 7 - Create a class polynomial that is used to evaluate...Ch. 7 - Write a method beyond LastEntry (position) for the...Ch. 7 - Revise the class OneWayNoRepeatsList, as given in...Ch. 7 - Write a static method for selection sort that will...Ch. 7 - Overload the method selectionSort in Listing 7.10...Ch. 7 - Revise the method selectionSort that appears in...Ch. 7 - Prob. 18ECh. 7 - Write a sequential search of an array of integers,...Ch. 7 - Write a static method findFigure (picture,...Ch. 7 - Write a static method blur (double [] [] picture)...Ch. 7 - Write a program that reads integers, one per line,...Ch. 7 - The following code creates a small phone book. An...Ch. 7 - Write the method rotateRight that takes an array...Ch. 7 - The following code creates a ragged 2D array. The...Ch. 7 - Write a program that will read a line of text that...Ch. 7 - Prob. 2PPCh. 7 - Add a method bubbleSort to the class ArraySorter,...Ch. 7 - Add a method insertionSort to the class...Ch. 7 - The class TimeBook in Listing 7.14 is not really...Ch. 7 - Define a class called TicTacToe. An object of type...Ch. 7 - Repeat Programming Project 10 from Chapter 5 but...Ch. 7 - Prob. 8PPCh. 7 - Write a GUI application that displays a picture of...Ch. 7 - ELIZA was a program written in 1966 that parodied...Ch. 7 - Prob. 11PPCh. 7 - Create a GUI application that draws the following...Ch. 7 - Practice Program 2 used two arrays to implement a...Ch. 7 - Practice Program 5.4 asked you to define Trivia...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,