New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
introduction to java programming and data structure
Introduction To Java Programming And Data Structures Comprehensive Version 12th Edition Y. Daniel Liang - Solutions
Rewrite Programming Exercise 6.20 using streams.Data from Programming Exercise 6.20Write a method that counts the number of letters in a string using the following header:public static int countLetters(String s)Write a test program that prompts the user to enter a string and displays the
Rewrite Programming Exercise 6.23 using streams.Data from Programming Exercise 6.23Write a method that finds the number of occurrences of a specified character in a string using the following header: public static int count(String str, char a)For example, count("Welcome", 'e') returns 2. Write
Use streams to write a program that displays the distinct scores in the scores array in Section 8.8. Display the scores in increasing order, separated by exactly one space, five numbers per line.
Rewrite Programming Exercise 21.4 using streams.Data from Programming Exercise 21.4Write a program that prompts the user to enter a text file name and displays the number of vowels and consonants in the file. Use a set to store the vowels A, E, I, O, and U.
Rewrite Programming Exercise 21.8 using streams.Data from Programming Exercise 21.8Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by whitespace characters, punctuation marks (,;.:?), quotation marks ('"), and
Suppose the file test.txt contains floating-point numbers separated by spaces. Write a program to obtain the sum, average, maximum, and minimum of the numbers.
Create a CSS style sheet that defines a class for white fill and black stroke color and an id for red stroke and green color. Write a program that displays four circles and uses the style class and id. The sample run of the program is shown in Figure 31.48a. Exercise31 01 (a)
Write a program that displays a tic-tac-toe board, as shown in Figure 31.48b. A cell may be X, O, or empty. What to display at each cell is randomly decided. The X and O are images in the files x.gif and o.gif. Use the style sheet for border. EExercise31 02 XX (b)
Write a program that creates two shapes: a circle and a path consisting of two cubic curves, as shown in Figure 31.49b. Exercise31 05 (b)
Write a program that displays two eyes in an oval, as shown in Figure 31.49c. Exercise31 06 (c)
Write a program that displays a rectangle with upper-left corner point at (40, 40), width 50, and height 40. Enter the values in the text fields x and y and press the Translate button to translate the rectangle to a new location, as shown in Figure 31.50a. Exercise31.07 X: 10 Y 10 Translate (a)
Write a program that displays an ellipse. The ellipse is centered in the pane with width 60 and height 40. Enter the value in the text field Angle and press the Rotate button to rotate the ellipse, as shown in Figure 31.50b. Exercise31 08 Angle: 30 Rotate (b)
Write a program that displays an ellipse. The ellipse is centered in the pane with width 60 and height 40. Enter the scaling factors in the text fields and press the Scale button to scale the ellipse, as shown in Figure 31.50c. Exercise31_09 Scale factor for x: 3 y. 1 Scale (c)
Write a program that plots the sine function, as shown in Figure 31.51a. Exercise31 10 YA (a)
Write a program that plots the log function, as shown in Figure 31.51a. Exercise31_10 (a)
Write a program that plots the n2 function, as shown in Figure 31.51b Еxercise31_11 (b)
Write a program that plots the log, n, nlogn, and n2 functions, as shown in Figure 31.52b. Exercise31_13 (b)
Write a program that enables the user to scale and rotate the STOP sign, as shown in Figure 31.53. The user can press the UP/DOWN arrow key to increase/decrease the size and press the RIGHT/LEFT arrow key to rotate left or right. Exerdise3114 Exercise)1,14 Exercise31_14 - STOP STOP STOP
Write a program that displays a circle filled with a gradient color to animate a sun and display light rays coming out from the sun using dashed lines, as shown in Figure 31.54a. Evercise 1_15 (a)
Write a program that displays a cylinder, as shown in Figure 31.54b. Use dashed strokes to draw the dashed arc. I Exercise31_16 (b)
Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The formula for the calculation is as follows:futureValue = investmentAmount × (1 + monthlyInterestRate)years×12 Use text fields for interest rate, investment amount,
Modify Listing 31.9, MenuDemo.java, to create a popup menu that contains the menus Operations and Exit, as shown in Figure 31.56. The popup is displayed when you click the right mouse button on the panel that contains the labels and the text fields.Data from Listing 31.9, Exercise31 18 Numrber 1 3
Create a program that displays four shapes in split panes, as shown in Figure 31.57a. Exercise31 19 - (a)
Write a program using tab panes for performing integer and rational number arithmetic as shown in Figure 31.58. Exercise31_21 Exercise31,21 Integer Operations Rational Operations Integer Oparations Rational Operations nteger Calculation Integer Celculation Number 1: 3 Number 2: 5 Result 15.0 Number
Revise Listing 31.14 to add a button to delete the selected row from the table, as shown in Figure 31.59.Data from Listing 31.14 Exertise31_22 Delete Selected Row Country Copital Population (million) a Demeeratie? USA Washi 200.0 true Canoda Untec Kngdom iondon e09 Garmany France Ottens 120 true
Rewrite Listing 32.1 to display the output in a text area, as shown in Figure 32.30.Data from Listing 32.1 Concurrent Output a ibab3b 4b 5 bebbbebbbbtbbbbbbbbbbbbbbbbbbbbbbbbbebbbbbbbbb 7 Sbbbbbbbbbbbbbbbbbtbb 9bb10 16 11 12 13 14 15 17 18 1920 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
Rewrite Programming Exercise 15.29 using a thread to control car racing. Compare the program with Programming Exercise 15.29 by setting the delay time to 10 in both the programs. Which one runs the animation faster?Data from Programming Exercise 15.29Write a program that simulates car racing, as
Rewrite Programming Exercise 15.28 using a thread to control the fan animation.Data from Programming Exercise 15.28Write a program that displays a running fan, as shown in Figure 15.35c. Use the Pause, Resume, and Reverse buttons to pause, resume, and reverse fan running. e0 O Exercise15 28 Pause
Rewrite Programming Exercise 15.32 using a thread to control the clock animation.Data from Programming Exercise 15.32Modify Listing 14.21, ClockPane.java, to add the animation into this class and add two methods start() and stop() to start and stop the clock, respectively. Write a program that lets
Rewrite Listing 32.6, ThreadCooperation.java, using the object’s wait() and notifyAll() methods.Data from Listing 32.6, 1mport java.util.concurrent.: 2 1mport java.util.concurrent. locks.: 1 3 4 publ1c class ThreadCooperation { private static Account account = new Account (): 5 6 public static
Revise Listing 32.10, ParallelMergeSort.java, to define a generic parallelMergeSort method as follows:public static > voidparallelMergeSort(E[] list)Data from Listing 32.10, 1mport java.util.concurrent. RecursiveAction; 2 1mport java.util.concurrent. ForkJoinPool; 4 publ1c class
Programming Exercise 8.5 describes how to perform matrix addition. Suppose you have multiple processors, so you can speed up the matrix addition. Implement the following method in parallel:public static double[][] parallelAddMatrix(double[][] a, double[][] b)Write a test program that measures the
EightQueens.java, to develop a parallel algorithm that finds all solutions for the Eight Queens problem. Launch eight subtasks, each of which places the queen in a different column in the first row.
An alternative version of the Dijkstra algorithm can be described as follows:The algorithm uses cost[v] to store the cost of a shortest path from vertex v to the source vertex s. cost[s] is 0. Initially assign infinity to cost[v] to indicate that no path is found from v to s. Let V denote all
Since T is implemented using a list in the getMinimumSpanningTree and getShortestPath methods in Listing 29.2, WeightedGraph.java, testing whether a vertex u is in T by invoking T.contains(u) takes O(n) time. Modify these two methods by introducing an array named isInT. Set isInT[u] to true when a
The traveling salesperson problem (TSP) is to find the shortest round-trip route that visits each city exactly once and then returns to the starting city. The problem is similar to finding a shortest Hamiltonian cycle in Programming Exercise 28.17. Add the following method in the WeightedGraph
The getShortestPath method finds a u with the smallest cost[u] using a linear search, which takes O( |V| ). The search time can be reduced to O(log|V| ) using an AVL tree. Modify the method using an AVL tree to store the vertices in V–T. Use Listing 29.7, Test- ShortestPath.java, to test your new
Revise GraphView in Listing 28.6 to display a weighted graph. Write a program that displays the graph in Figure 29.1 as shown in Figure 29.24. (Instructors may ask students to expand this program by adding new cities with appropriate edges into the graph.)Data from Figure 29.1Data from Listing 28.6
Modify Listing 29.3, TestWeightedGraph.java, to create a file for representing graph1. The file format is described in Programming Exercise 29.9. Create the file from the array defined in lines 7–24 in Listing 29.3. The number of vertices for the graph is 12, which will be stored in the first
Write a program that reads a connected graph from a file and displays its minimum spanning tree. The first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0, 1, . . . , n−1. Each subsequent line describes the edges in the form of u1, v1,
Revise Listing 29.9, WeightedNineTail.java, for the weighted 4 * 4 16 tails problem. Your program should read the model object created from the preceding exercise.Data from Listing 29.9, 1mport java.util.: 3 publ1c class WeightedNineTail Model extends NineTail Model { " Construct a model / pub11c
The weighted nine tails problem in the text uses a 3 * 3 matrix. Assume that you have 16 coins placed in a 4 * 4 matrix. Create a new model class named WeightedTailModel16. Create an instance of the model and save the object into a file named WeightedTailModel16.
Modify Listing 28.4, UnweightedGraph.java, to override the remove(int u, int v) method defined in the Graph interface.Data from Listing 28.4, 1mport java.util.: 3 publ1c class UnweightedGraph 1mplements Graph { protected List vertices = new ArrayList (): // Store vertices protected List neighbors =
Modify Listing 28.4, UnweightedGraph.java, to override the remove(V v) method defined in the Graph interface.Data from Listing 28.4, 1mport java.util.: 3 publ1c class UnweightedGraph 1mplements Graph { protected List vertices = new ArrayList (): // Store vertices protected List neighbors = new
Listing 28.10, ConnectedCircles.java, allows the user to create circles and determine whether they are connected. Rewrite the program for rectangles. The program lets the user create a rectangle by clicking a mouse in a blank area that is not currently covered by a rectangle. As the rectangles are
Write a program that reads a graph from a file and displays it. The first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled 0, 1, . . . , n−1. Each subsequent line, with the format u x y v1 v2 ..., describes the position of u at (x, y) and
The Hamiltonian path algorithm is implemented in Supplement VI.E. Add the following getHamiltonianCycle method in the Graph interface and implement it in the UnweightedGraph class:/** Return a Hamiltonian cycle* Return null if the graph doesn't contain a Hamiltonian cycle */public List
Rewrite Programming Exercise 28.14 to enable the user to set an initial pattern of the 4 * 4 16 tails problem (see Figure 28.23a). The user can click the Solve button to display the solution, as shown in Figure 28.23b. Initially, the user can click the mouse button to flip a coin. If a solution
Listing 28.14, NineTail.java, presents a solution for the nine tails problem. Revise this program for the 4 * 4 16 tails problem. Note it is possible that a solution may not exist for a starting pattern. If so, report that no solution exists.Data from Listing 28.14 1mport java. util.Scanner;: 2 3
The program in Listing 28.14 lets the user enter an input for the nine tails problem from the console and displays the result on the console. Write a program that lets the user set an initial state of the nine coins (see Figure 28.22a) and click the Solve button to display the solution, as shown in
Define a new class named UnweightedGraphFindCycle that extends UnweightedGraph with a new method for finding a cycle starting at vertex u with the following header:public List getACycle(int u);The method returns a List that contains all the vertices in a cycle starting from u. If the graph
Define a new class named UnweightedGraphDetectCycle that extends UnweightedGraph with a new method for determining whether there is a cycle in the graph with the following header:public boolean isCyclic();Describe the algorithm in pseudocode and implement it. Note the graph may be a directed graph.
Define a new class named UnweightedGraphWithGetPath that extends UnweightedGraph with a new method for finding a path between two vertices with the following header:public List getPath(int u, int v);The method returns a List that contains all the vertices in a path from u to v in this order.
The depth-first search algorithm described in Listing 28.8, Depth-First Search Algorithm, uses recursion. Design a newalgorithm without using recursion. Describe it using pseudocode. Implement it by defining a new class named UnweightedGraphWithNonrecursiveDFS that extends UnweightedGraph and
Modify Listing 28.2, TestGraph.java, to create a file representing graph1. The file format is described in Programming Exercise 28.1. Create the file from the array defined in lines 8–21 in Listing 28.2. The number of vertices for the graph is 12, which will be stored in the first line of the
Write a program that reads a graph from a file and determines whether the graph is connected. The first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0, 1, . . . , n−1. Each subsequent line, with the format u v1 v2 ..., describes edges
The book uses LinkedList for buckets. Replace LinkedList with AVLTree. Assume E is Comparable. Redefine MyHashSet as follows:public class MyHashSet> implementsCollection {...}Test your program using the main method in Listing 27.5.Data from Listing 27.5. 1 publ1c class TestMy HashSet { publ1c
Design a class named Point that meets the following requirements:■ Two data fields x and y for representing a point with getter methods■ A no-arg constructor that constructs a point for (0, 0)■ A constructor that constructs a point with the specified x and y values■ Override the equals
Design a class named Date that meets the following requirements: ■ Three data fields year, month, and day for representing a date■ A constructor that constructs a date with the specified year, month, and day■ Override the equals method■ Override the hashCode method. (For reference, see
The implementations of the methods addAll, removeAll, retainAll, toArray(), and toArray(T[]) are omitted in the MyHashSet class. Implement these methods. Also add a new constructor MyHashSet(E[] list) in the MyHashSet class. Test your new MyHashSet class using the code at
MyArrayList is defined in Listing 24.2. Write a program that generates 1000000 random double values between 0 and 999999 and stores them in a MyArrayList and in a MyHashSet. Generate a list of 1000000 random double values between 0 and 1999999. For each number in the list, test if it is in the
Write a program that animates MyHashMap, as shown in Figure 27.9. You can change the initial size of the table. Assume the load-factor threshold is 0.75. O Hashing Separate Chaini x O liveexample.pearsoncmg.com/dsanimation/SeparateChainingeBook.html Q * Usage: Enter the table size and press the
You can find the kth smallest element in a BST in O(n) time from an inorder iterator. For an AVL tree, you can find it in O(log n) time. To achieve this, add a new data field named size in AVLTreeNode to store the number of nodes in the subtree rooted at this node. Note the size of a node v is one
The MyLinkedList class used in Listing 24.5 is a one-way directional linked list that enables one-way traversal of the list.Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows:public class Node {E element;Node next;Node previous;public
Define a new class named MyBST that extends the BST class with the following method: // Returns true if the tree is an AVL tree public boolean isAVLTree() Use https://liveexample.pearsoncmg.com/test/Exercise26_07.txt to test your code.
Huffman coding animation) Write a program that enables the user to enter text and displays the Huffman coding tree based on the text, as shown in Figure 25.23a. Display the weight of the subtree inside the subtree’s root circle. Display each leaf node’s character. Display the encoded bits for
Write a program that animates the AVL tree insert, delete, and search methods, as shown in Figure 26.2. D AVL Tree Animation by Y C O liveexample.pearsoncmg.com/dsanimation/AVLTreeeBook.html Q * O Usage: Enter an integer key and click the Search button to search the key in the tree. Click the
Modify Listing 25.8, BSTAnimation.java, to add three new buttons—Show Inorder, Show Preorder, and Show Postorder—to display the result in a label, as shown in Figure 25.22. You need also to modify Listing 25.4, BST.java to implement the inorderList(), preorderList(), and postorderList() methods
Write a program that prompts the user to enter a file name, then displays the frequency table of the characters in the file and the Huffman code for each character.
Design and write a complete test program to test if the BST class in Listing 25.4 meets all requirements.Data from Listing 25.4 1 publ1c class BST 1mplements Tree { protected TreeNode root: protected 1nt size = 0: protected java.util.Comparator c: 4 1** Create a default BST with a natural order
Define a new class named BSTWithNumberOfNonLeaves that extends BST with the following methods:/** Return the number of nonleaf nodes */public int getNumberofNonLeaves()Use https://liveexample.pearsoncmg.com/test/Exercise25_07.txt to test your code.
Define a new class named BSTWithNumberOfLeaves that extends BST with the following methods:/** Return the number of leaf nodes */public int getNumberOfLeaves()Use https://liveexample.pearsoncmg.com/test/Exercise25_06.txt to test your code.
A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2height + 1 - 1)./** Returns true if the tree is a perfect binary tree
Define a new class named BSTWithHeight that extends BST with the following method: /** Return the height of this binary tree */public int height()Use https://liveexample.pearsoncmg.com/test/Exercise25_01.txt to test your code.
Design and write a complete test program to test if the MyLinkedList class in Listing 24.5 meets all requirements.Data from Listing 24.5 1 publ1c class MyLinkedList 1mplements MyList private Node head, tail: private int size = 0: // Number of el ements in the list 2 4 ** Create an empty list/
Design and write a complete test program to test if the MyArrayList class in Listing 24.2 meets all requirements.Data from Listing 24.2 1 publ1c class MyArrayList 1mplementS MyList { publ1c stat 1c f1nal 1nt INITIAL CAPACITY = 16: private E[] data = (E[]) new Object[INITIAL CAPACITY]: private int
The implementations of the methods addAll, removeAll, retainAll, toArray(), and toArray(T[]) are omitted in the MyList interface. Implement these methods. Test your new MyList class using the code at https://liveexample.pearsoncmg.com/test/Exercise24_01.txt.
The implementations of the methods contains(E e), get(int index), indexOf(E e), lastIndexOf(E e), and set(int index, E e) are omitted in the MyLinkedList class. Implement these methods. Define a new class named MyLinkedListExtra that extends MyLinkedList to override these methods. Test your new
Write a program that animates the partition for a quick sort. The program creates a list that consists of 20 random numbers from 1 to 999. The list is displayed, as shown in Figure 23.22b. Clicking the Step button causes the program to move low to the right or high to the left, or swap the elements
Write a program that animates the merge of two sorted lists. Create two arrays, list1 and list2, each of which consists of 8 random numbers from 1 to 999. The array elements are displayed, as shown in Figure 23.22a. Clicking the Step button causes the program to move an element from list1 or list2
Write a program that animates the radix-sort algorithm. Create an array that consists of 20 random numbers from 0 to 1,000. The array elements are displayed, as shown in Figure 23.21. Clicking the Step button causes the program to place a number in a bucket. The number that has just been placed is
Write a program that animates the selection-sort algorithm. Create an array that consists of 20 distinct numbers from 1 to 20 in a random order. The array elements are displayed in a histogram, as shown in Figure 23.20a. Clicking the Step button causes the program to perform an iteration of the
Write a program that animates the bubble-sort algorithm. Create an array that consists of 20 distinct numbers from 1 to 20 in a random order. The array elements are displayed in a histogram, as shown in Figure 23.20b. Clicking the Step button causes the program to perform one comparison in the
Implement the clone and equals method in the Heap class. Use the template at liveexample.pearsoncmg.com/test/Exercise23_11.txt for your code.
The bin packing problem is to pack the objects of various weights into containers. Assume each container can hold a maximum of 10 pounds. The program uses an algorithm that places an object with the smallest weight into the first bin in which it would fit. Your program should prompt the user to
Write a program that finds all prime numbers up to 10,000,000,000. There are approximately 455,052,511 such prime numbers. Your program should meet the following requirements:■■ Your program should store the prime numbers in a binary data file, named PrimeNumbers.dat. When a new prime number is
Section 22.8 introduced an algorithm for finding the closest pair of points using a divide-and-conquer approach. Implement the algorithm to meet the following requirements: ■■ Define a class named Pair with the data fields p1 and p2 to represent two points and a method named getDistance()
Write an O(n) time program that prompts the user to enter a sequence of integers ending with 0 and finds the longest subsequence with the same number. Here is a sample run of the program: Enter a series of numbers ending with 0: 2 4 48 8 8 8 2 4 40-onter The longest same number sequence starts at
Revise the implementation for the Boyer-Moore algorithm in Listing 22.15 StringMatchBoyerMoore. java to test where a mismatch character is in the pattern in O(1) time using a set that consists of all the characters in the pattern. If the test is false, the algorithm can shift the pattern past the
Revise Programming Exercise 21.11 to prompt the user to enter year, gender, and name and display the ranking for the name. Prompt the user to enter another inquiry or exit the program. Here is a sample run:Data from Programming Exercise 21.11Use the data files from Programming Exercise 12.31 to
Write a program that prompts the user to enter a string and displays the maximum increasingly ordered subsequence of characters. Analyze the time complexity of your program. Here is a sample run: Enter a string: Welcome Porie Maximum increasingly ordered subsequence is Welo
Write a program that prompts the user to enter a string and displays the maximum consecutive increasingly ordered substring. Analyze the time complexity of your program. Here is a sample run: Enter a string: abcabcdgabxy ee Maximum consecutive substring is abcdg Enter a string: abcabcdgabmnsxy Perr
Use the data files from Programming Exercise 12.31 to write a program that enables the user to select a year, gender, and enter a name to display the ranking of the name for the selected year and gender, as shown in Figure 21.9. To achieve the best efficiency, create two arrays for boy’s names
Write a program that prompts the user to enter one of the filenames described in Programming Exercise 12.31 and displays the names that are used for both genders in the file. Use sets to store names and find common names in two sets. Here is a sample run: Enter a file name for baby name ranking:
Write a program that converts a Java file into an HTML file. In the HTML file, the keywords, comments, and literals are displayed in bold navy, green, and blue, respectively. Use the command line to pass a Java file and an HTML file. For example, the following command java Exercise21_05
Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by whitespace characters, punctuation marks (,;.:?), quotation marks ('"), and parentheses. Count words in case-insensitive fashion (e.g., consider Good and good to be the
Write a program that prompts the user to enter a text file name and displays the number of vowels and consonants in the file. Use a set to store the vowels A, E, I, O, and U.
Modify Listing 20.12, EvaluateExpression.java, to add operators ^ for exponent and % for remainder. 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
Write the following generic method using selection sort and a comparator:public static void selectionSort(E[] list,Comparator comparator)Write a test program that prompts the user to enter six strings, invokes the sort method to sort the six strings by their last character, and displays the sorted
The 24-point card game is to pick any four cards from 52 cards, as shown in Figure 20.19. Note 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
Showing 100 - 200
of 346
1
2
3
4
Step by Step Answers