Answered step by step
Verified Expert Solution
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 listPassengersArrayList remainPassengers, ArrayList pickedPassengers, int optionsFound
int i;
String pick;
if remainPassengerssize
Your code goes here
optionsFound;
System.out.printNumber optionsFound : ;
for i ; i pickedTokens.size; i
System.out.printpickedTokensgeti;
System.out.println;
return optionsFound;
else
for i ; i remainPassengers.size; i
pick remainPassengers.geti;
remainPassengers.removei;
pickedPassengers.addpick;
optionsFound listPassengersremainPassengers pickedPassengers, optionsFound;
remainPassengers.addi pick;
pickedPassengers.removepickedPassengerssize;
return optionsFound;
public static void mainString args
Scanner scnr new ScannerSystemin;
ArrayList passengersToPick new ArrayList;
ArrayList picks new ArrayList;
int listSize;
int i;
listSize scnrnextInt;
for i ; i listSize; i
passengersToPick.addscnrnext;
System.out.printlnAll unique orderings:";
listPassengerspassengersToPick picks, ;
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:: error: cannot find symbol
for i ; i pickedTokens.size; i
symbol: variable pickedTokens
location: class Names
Names.java:: error: cannot find symbol
System.out.printpickedTokensgeti;
symbol: variable pickedTokens
location: class Names
errors
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started