Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Integer listSize is read from input, then listSize strings are read from input and stored in ArrayList passengersToPick. listPassengers ( ) counts and outputs all

Integer listSize is read from input, then listSize strings are read from input and stored in ArrayList passengersToPick. listPassengers() counts and outputs all possible orderings of names in remainPassengers. Complete the listPassengers() base case to increment the value of optionsFound and output the following:
"#"
the value of optionsFound
": "
each element of pickedPassengers followed by a space
End with a newline. Then, return the value of optionsFound for the base case. import java.util.Scanner;
import java.util.ArrayList;
public class Names {
public static int listPassengers(ArrayList remainPassengers, ArrayList pickedPassengers, int optionsFound){
int i;
String pick;
if (remainPassengers.size()==0){
/* Your code goes here */
optionsFound++;
System.out.print("Number "+ optionsFound +": ");
for (i =0; i < pickedTokens.size(); ++i){
System.out.print(pickedTokens.get(i)+"");
}
System.out.println();
return optionsFound;
}
else {
for (i =0; i < remainPassengers.size(); ++i){
pick = remainPassengers.get(i);
remainPassengers.remove(i);
pickedPassengers.add(pick);
optionsFound = listPassengers(remainPassengers, pickedPassengers, optionsFound);
remainPassengers.add(i, pick);
pickedPassengers.remove(pickedPassengers.size()-1);
}
return optionsFound;
}
}
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
ArrayList passengersToPick = new ArrayList();
ArrayList picks = new ArrayList();
int listSize;
int i;
listSize = scnr.nextInt();
for (i =0; i < listSize; ++i){
passengersToPick.add(scnr.next());
}
System.out.println("All unique orderings:");
listPassengers(passengersToPick, picks, 0);
}
}
Explanation:
Increment optionsFound to count each unique sequence
Print "Number " followed by the value of optionsFound
Print each picked token followed by a space
Print a newline
Return the updated value of optionsFound
Here is an explanation of the full makeSequence() method:
Explanation:
Creating and printing every conceivable unique sequence (ordering) of the integers in the tokensToPick list is the aim.
It accomplishes this by building each sequence by recursively invoking itself.
The key parameters:
remainTokens - the sublist of tokens that haven't been picked yet
pickedTokens - the current sequence being built
optionsFound - count of sequences generated so far/////
The base case is when remainTokens is empty. At that point:
Increment optionsFound to count this finished sequence
Publish the series together with its index number.
Return the updated count
For each recursive call:
Add one token from the remaining tokens to the end of the tokens that you have chosen.
Call makeSequence recursively to create sequences using the selected token
Reverse the decision by deleting the selected token and adding the original token to remainTokens.
This explores every possible ordering.
When we have completed selecting tokens to create a full sequence, the printing and counting take place in the base case.
So in summary, It builds up and prints every variant in a methodical manner using recursion.
This prints the selected tokens, separated by spaces on each line, for each distinct sequence along with an index number. To count every sequence, the updated optionsFound value is sent back.
Failed to compile
Names.java:14: error: cannot find symbol
for (i =0; i < pickedTokens.size(); ++i){
^
symbol: variable pickedTokens
location: class Names
Names.java:15: error: cannot find symbol
System.out.print(pickedTokens.get(i)+"");
^
symbol: variable pickedTokens
location: class Names
2 errors

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

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago