• Home
  • Textbooks
  • Introduction to Java Programming and Data Structures, Comprehensive Version
  • Lists, Stacks, Queues, and Priority Queues

Introduction to Java Programming and Data Structures, Comprehensive Version

Daniel Y. Liang

Chapter 20

Lists, Stacks, Queues, and Priority Queues - all with Video Answers

Educators


Chapter Questions

19:54

Problem 1

Write a program that reads words from a text file and displays all the words (duplicates allowed) in descending alphabetical order. The words must start with a letter. The text file is passed as a command-line argument.

Nicole Powell
Nicole Powell
Numerade Educator
04:38

Problem 2

rite a program that lets the user enter numbers from a graphical user interface and displays them in a text area, as shown in Figure 20.17a. Use a linked list to store the numbers. Do not store duplicate numbers. Add the buttons Sort, Shuffle, and Reverse to sort, shuffle, and reverse the list.

Tarandeep Singh
Tarandeep Singh
Numerade Educator
02:21

Problem 3

Rewrite Programming Exercise 8.37 to store the pairs of states and capitals so that the questions are displayed randomly.

Harriet O'Brien
Harriet O'Brien
Numerade Educator

Problem 4

Implement a new class that implements Comparator of GregorianCalendar class to be able sort the calendar in increasing order based on day, month, and year, in that order. Write a method to display the GregorianCalendar instance in "dd-MMM-yyyy" format using Si mpleDateFormat class. Write a test program with 10 Gregori anCal endar instances and display the results after the sort.

Check back soon!
06:18

Problem 5

The example in Section 20.8 displays multiple bouncing balls. Extend the example to detect collisions. Once two balls collide, remove the later ball that was added to the pane and add its radius to the other ball, as shown in Figure 20.17b. Use the Suspend button to suspend the animation, and the Resume button to resume the animation. Add a mouse-pressed handler that removes a ball when the mouse is pressed on the ball.

Lisa Tarman
Lisa Tarman
Numerade Educator
01:13

Problem 6

Write a test program that stores 10 million integers in ascending order (i.e., $1,2, \ldots, 10 \mathrm{~m}$ ) in an ArrayList, displays the execution time taken to traverse the list, and searches for the 10 millionth element using the get (index) vs using the iterator method.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 7

Programming Exercise 7.35 presents a console version of the popular hangman game. Write a GUI program that lets a user play the game. The user guesses a word by entering one letter at a time, as shown in Figure 20.18. If the user misses seven times, a hanging man swings. Once a word is finished, the user can press the Enter key to continue to guess another word.

Check back soon!
04:47

Problem 8

Revise Programming Exercise 3.15 to add an additional \$2,000 award if two digits from the user input are in the lottery number. (Hint: Sort the three digits in the lottery number and three digits in the user input into two lists, and use the Collection's containsAll method to check whether the two digits in the user input are in the lottery number.)

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 9

Modify Listing 20.10, Mu1tip1eBa11App . java to assign a random radius between 2 and 20 when a ball is created. When the - button is clicked, one of largest balls is removed.

Check back soon!
03:29

Problem 10

Given two stacks of textbooks of the following subjects \{"Chemistry", "Mathematics", "Biology", "English"\} and \{"Biology", "English", "Geography", "Physics"\}, find the subjects that are (1) only present in the first stack; (2) only present in the second stack; (3) present in both stacks.

Carson Merrill
Carson Merrill
Numerade Educator
View

Problem 11

Write a program that reads 10 integers and displays them in the reverse of the order in which they were read. If two consecutive numbers are identical, then only display one of them. Implement your program using only stack and not arrays or queues.

Nick Johnson
Nick Johnson
Numerade Educator

Problem 12

Define a class MyStack that extends Stack to be able to have its constructor use a list of objects instead of pushing each object individually.

Check back soon!
02:46

Problem 13

Postfix notation is a way of writing expressions without using parentheses. For example, the expression $(1+2) * 3$ would be written as $12+3 *$ A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two operands with the result. The following diagram shows how to evaluate $12+3 *$.
Write a program to evaluate postfix expressions. Pass the expression as a command-line argument in one string.

SS
Sarvesh Somasundaram
Numerade Educator
02:46

Problem 14

Postfix notation is a way of writing expressions without using parentheses. For example, the expression $(1+2) * 3$ would be written as $12+3$ *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two operands with the result. The following diagram shows how to evaluate $12+3$ *:
Write a program to evaluate postfix expressions. Pass the expression as a command-line argument in one string.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 15

Improve Programming Exercise 20.13 to enable the computer to display the expression if one exists, as shown in Figure 20.19. Otherwise, report that the expression does not exist. Place the label for verification result at the bottom of the UI. The expression must use all four cards and evaluated to 24 .

Check back soon!

Problem 16

Improve Programming Exercise 20.15 by adding another expression verifier text box below the card images that accept prefix notations instead of infix notations. You have to write your own prefix evaluator.

Check back soon!
04:48

Problem 17

This exercise is a variation of the 24-point card game described in Programming Exercise 20.13. Write a program to check whether there is a 24-point solution for the four specified numbers. The program lets the user enter four values, each between 1 and 13, as shown in Figure 20.20. The user can then click the Solve button to display the solution or display "No solution" if none exists:

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 18

Listing 18.10, DirectorySize.java, gives a recursive method for finding a directory size. Rewrite this method without using recursion. Your program should use a queue to store the subdirectories under a directory. The algorithm can be described as follows:
long getSize(File directory) \{
long size $=0$;
add directory to the queue;
while (queue is not empty) \{
Remove an item from the queue into $t$;
if ( $t$ is a file)
size $+=$ t.length ();
else
add all the files and subdirectories under $t$ into the
queue;
\}
return size;
\}

Check back soon!

Problem 19

Write the following generic method using selection sort and a comparator:
public static <E> void selectionSort(E[] list, Comparator<? super E> comparator)
Write a test program that creates an array of 10 GeometricObjects and invokes this method using the GeometricObjectComparator introduced in Listing 20.4 to sort the elements. Display the sorted elements. Use the following statement to create the array:
Geometric0bject[] 1ist1 $=$ \{new Circle(5), new Rectangle(4, 5),
new Circle(5.5), new Rectangle $(2.4,5)$, new Circle(0.5),
new Rectangle(4, 65), new Circle $(4.5)$, new Rectangle $(4.4,1)$,
new Circle(6.5), new Rectangle $(4,5)\}$;

Check back soon!
01:16

Problem 20

Implement the moveDisks method in Listing 18.8 using a stack instead of using recursion.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 21

Write the following generic method using selection sort and a comparator:
public static <E> void selectionSort(E[] list,
Comparator<? super E> comparator)
Write a test program that creates an array of 10 GeometricObjects and invokes this method using the GeometricObjectComparator introduced in Listing 20.5 to sort the elements. Display the sorted elements. Use the following statement to create the array:

Geometric0bject[] 1ist1 $=$ \{new Circle(5), new Rectangle(4, 5),
new Circle(5.5), new Rectangle(2.4,5), new Circle(0.5),
new Rectangle(4,65), new Circle(4.5), new Rectangle $(4.4,1)$,
new Circle $(6.5)$, new Rectangle $(4,5)\}$;

Also in the same program, write the code that sorts six strings by their last character. Use the following statement to create the array:

String[] 1ist2 = \{"red", "b1ue", "green", "ye11ow", "orange",
"pink"\};

Check back soon!
01:16

Problem 22

Implement the moveDisks method in Listing 18.8 using a stack instead of using recursion.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 23

Modify Listing 20.12, EvaluateExpression class to make all operators $\left(+-^* /\right)$ have equal precedence. For example, $4+3-2$ * 10 is 50. Write a test program that prompts the user to enter an expression and displays the result.

Check back soon!