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

Introduction to Java Programming. Comprehensive Version

Y. Daniel Liang

Chapter 20

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

Educators


Chapter Questions

19:54

Problem 1

(Display words in ascending alphabetical order) Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending 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

Problem 2

(Store numbers in a linked list) Write 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.FIGURE CANT COPY
Ficure 20.17 (a) The numbers are stored in a list and displayed in the text area. (b) The colliding balls are combined.

Check back soon!
03:10

Problem 3

(Guessing the capitals) 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
01:21

Problem 4

(Sort points in a plane) Write a program that meets the following requirements:
- Define a class named Point with two data fields $x$ and $y$ to represent a point's $x$ - and $y$-coordinates. Implement the Comparable interface for comparing the points on $x$-coordinates. If two points have the same $x$-coordinates, compare their $y$-coordinates.
- Define a class named CompareY that implements Comparator<Point>. Implement the compare method to compare two points on their $y$-coordinates. If two points have the same $y$-coordinates, compare their $x$-coordinates.
- Randomly create 100 points and apply the Arrays . sort method to display the points in increasing order of their $x$-coordinates and in increasing order of their $y$-coordinates, respectively.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
06:18

Problem 5

(Combine colliding bouncing balls) The example in Section 20.7 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

Problem 6

(Use iterators on linked lists) Write a test program that stores 5 million integers in a linked list and test the time to traverse the list using an iterator vs. using the get (index) method.

Check back soon!

Problem 7

(Game: hangman) 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

(Game: lottery) 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 containsA11 method to check whether the two digits in the user input are in the lottery number.)

Morgan Cheatham
Morgan Cheatham
Numerade Educator
19:31

Problem 9

(Remove the largest ball first) Modify Listing 20.6, MultipleBallApp.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.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
07:28

Problem 10

(Perform set operations on priority queues) Create two priority queues, \{"George", "Jim", "John", "Blake", "Kevin", "Michael"\} and ["George", "Katie", "Kevin", "Miche1le", "Ryan"], and find their union, difference, and intersection.

Paul A.
Paul A.
California State Polytechnic University, Pomona
10:22

Problem 11

(Match grouping symbols) A Java program contains various pairs of grouping symbols, such as:
- Parentheses: (and)
- Braces: $\{$ and \}
- Brackets: [ and ]
Note that the grouping symbols cannot overlap. For example, (a $\{b)\}$ is illegal. Write a program to check whether a Java source-code file has correct pairs of grouping symbols. Pass the source-code file name as a command-line argument.
FIGURE CANT COPY
FIGURE 20.18 The program displays a hangman game.

Willis James
Willis James
Numerade Educator
03:16

Problem 12

(Clone PriorityQueue) Define MyPriorityQueue class that extends PriorityQueue to implement the Cloneable interface and implement the $\mathrm{cl}$ one $\mathrm{O}$ method to clone a priority queue.

SS
Sarvesh Somasundaram
Numerade Educator
02:18

Problem 13

(Game: the 24-point card game) The 24-point game is to pick any 4 cards from 52 cards, as shown in Figure 20.19. Note that the Jokers are excluded. Each card represents a number. An Ace, King, Queen, and Jack represent 1, 13,12, and 11, respectively. You can click the Shuffle button to get four new cards. Enter an expression that uses the four numbers from the four selected cards. Each number must be used once and only once. You can use the operators (addition, subtraction, multiplication, and division) and parentheses in the expression. The expression must evaluate to 24 . After entering the expression, click the Verify button to check whether the numbers in the expression are currently selected and whether the result of the expression is correct. Display the verification in a label before the Shuffle button. Assume that images are stored in files named 1.png, 2.png, ..., 52.png, in the order of spades, hearts, diamonds, and clubs. So, the first 13 images are for spades $1,2,3, \ldots$, and 13 .
FIGURE CANT COPY
Ficure 20.19 The user enters an expression consisting of the numbers in the cards and clicks the Verify button to check the answer.

Kari Hasz
Kari Hasz
Numerade Educator
02:46

Problem 14

(Postfix notation) 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
04:48

Problem 15

(Game: the 24-point card game) Improve Programming Exercise 20.13 to enable the computer to display the expression if one exists, as shown in Figure 20.20, Otherwise, report that the expression does not exist. Place the label for verification result at the bottom of UI. The expression must use all four cards and evaluates to 24 .
FIGURE CANT COPY
FicurE 20.20 The program can automatically find a solution if one exists.

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 16

(Convert infix to postfix) Write a method that converts an infix expression into a postfix expression using the following header:
public static String infixToPostfix(String expression)
For example, the method should convert the infix expression $(1+2)-3$ to $12+3=$ and $2=(1+3)$ to $213+=$.

Check back soon!
04:48

Problem 17

(Game; the 24-point card game) 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.21. The user can then click the Solve button to display the solution or display "No solution" if none exist.
FIGURE CANT COPY
Figure 20.21 The user enters four numbers and the program finds a solution.

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 18

(Directory size) Listing 20.7, 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 $+=\mathrm{t}$. length $)$;
else
add all the files and subdirectories under $t$ into the
\}
queue;
return size;
\}

Check back soon!
04:48

Problem 19

(Game: solution ratio for 24-point game) When you pick four cards from a deck of 52 cards for the 24-point game introduced in Programming Exercise 20.13, the four cards may not have a 24-point solution. What is the number of all possible picks of four cards from 52 cards? Among all possible picks, how many of them have 24-point solutions? What is the success ratio-that is, (number of picks with solutions)/ (number of all possible picks of four cards)? Write a program to find these answers.

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 20

(Directory size) Rewrite Programming Exercise 18.28 using a stack instead of a queue.

Check back soon!

Problem 21

(Use Comparator) Write the following generic method using selection sort and a comparator.
public static <E> void selectionSort(E[] 1ist, 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.
GeometricObject[] list $=$ \{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!

Problem 22

(Nonrecursive Tower of Hanoi) Implement the moveDisks method in Listing 18.8 using a stack instead of using recursion.

Check back soon!
04:21

Problem 23

(Evaluate expression) Modify Listing 20.9 EvaluateExpression.java to add operators ${ }^*$ for exponent and $\%$ for modulus. For example, 3 * 2 is 9 and $3 \% 2$ is 1 . The operator has the highest precedence and the $\%$ operator has the same precedence as the " and / operators. Your program should prompt the user to enter an expression. Here is a sample run of the program:FIGURE CANT COPY

SS
Sarvesh Somasundaram
Numerade Educator