All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
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
Hire a Tutor
AI Study Help
New
Search
Search
Sign In
Register
study help
computer science
building java programs a back to basics approach
Questions and Answers of
Building Java Programs A Back to Basics Approach
If you perform a binary search on an array of one million integers, which of the following is closest to the number of elements that the search algorithm will need to examine?a. All 1,000,000 of the
Write a recursive program to generate random sentences from a given BNF grammar. A BNF grammar is a recursively defined file that defines rules for creating sentences from tokens of text. Rules can
Write a method called reverse that reverses the order of the elements in the list. (This is very challenging!) For example, if the variable list initially stores the values [1, 8, 19, 4, 17], the
In this chapter, we implemented a HashSet that used separate chaining to resolve collisions. Implement your own version of HashSet that uses linear probing to resolve collisions. Perform lazy
Rewrite the SideEffect program from this section so that it does not contain any side effects. Rather than modifying a global variable, make the function accept the value of x to use as a parameter.
Write a method theLines that accepts a file name as a parameter and uses stream operations to return a count of the number of lines in the file that start with the word “The”, case-insensitive.
Suppose you have a list of strings declared as follows. Write code to use stream operations to print all of the four-letter words in the list.List list = Arrays.asList("four", "score", ..., "ago");
Write a method called countEmpty that returns the number of empty branches in a tree. An empty tree is considered to have one empty branch (the tree itself). For nonempty trees, your methods should
Write a method called countLeftNodes that returns the number of left children in the tree. A left child is a node that appears as the root of the left-hand subtree of another node. For example,
Write a method called firstLast that can accept either type of integer list as a parameter and that moves the first element of the list to the end. For example, if a variable called list contains the
Write methods called min and max that return the smallest and largest values in the linked list, respectively. For example, if a variable called list stores [11, –7, 3, 42, 0, 14], the call of
You’ll see pictures of long chains of linked nodes before and after changes. (The . . . in the middle of the chain signifies an indeterminate large number of nodes.) Write the code that will
You’ll see pictures of long chains of linked nodes before and after changes. (The . . . in the middle of the chain signifies an indeterminate large number of nodes.) Write the code that will
You’ll see pictures of long chains of linked nodes before and after changes. (The . . . in the middle of the chain signifies an indeterminate large number of nodes.) Write the code that will
Write a method called surroundWith that takes an integer x and an integer y as parameters and surrounds all nodes in the list containing the value x with new nodes containing the value y. In
Write a method called shift that rearranges the elements of a list of integers by moving to the end of the list all values that are in oddnumbered positions and otherwise preserving list order. For
Write a method called rotate that moves the value at the front of a list of integers to the end of the list. For example, if a variable called list stores the values [8, 23, 19, 7, 45, 98, 102, 4],
Write a method called doubleList that doubles the size of a list by appending a copy of the original sequence to the end of the list. For example, if a variable list stores the values [1, 3, 2, 7]
Write a method called removeRange that accepts a starting and ending index as parameters and removes the elements at those indexes (inclusive) from the list. For example, if a variable list stores
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Write a method called removeEvens that removes the values in evennumbered indexes from a list, returning a new list that contains those values in their original order. For example, consider a
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Write a method called equals that accepts a second list as a parameter, returns true if the two lists are equal, and returns false otherwise. Two lists are considered equal if they store exactly the
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Write a method called removeAll that removes all occurrences of a particular value. For example, if a variable list stores the values [3, 9, 4, 2, 3, 8, 17, 4, 3, 18], the call of list.removeAll(3);
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Write a method called transferFrom that accepts a second linked list as a parameter and that moves values from the second list to this list. You are to attach the second list’s elements to the end
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Write a method called split that rearranges the elements of a list so that all of the negative values appear before all of the nonnegatives. For example, suppose a variable list stores the values [8,
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Write a method called compress that replaces every pair of elements in the list with a single element equal to the sum of the pair. If the list is of odd size, leave the last element unchanged. For
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
You’ll see pictures of linked nodes before and after changes. Write the code that will produce the given result by modifying links between the nodes shown and/or creating new nodes as needed. There
Draw a picture of what the given linked nodes would look like after the given code executes.list.next.next = null; list 1 2 3
Write a method called fromCounts that converts an ArrayIntList of counts into a new ArrayIntList of values as follows. Assume that the list stores a sequence of integer pairs that each pair indicates
Write a method called rotate that moves the value at the front of a list of integers to the end of the list. For example, if a variable called list stores [8, 23, 19, 7, 12, 4], the call of
The following code contains four problems causing it to fail to compile. What are they? public static int longestLineLength (String filename) { return Files.lines (filename) .map (String: :length)
Write code to print all lines from the file notes.txt that are at least 40 characters long.
Suppose you have an array of integers called numbers. Write a piece of code that uses stream operations to make a new array called positives that stores only the positive integers from numbers.
The following code does not compile. Why not? What is upsetting the compiler? int a = 10; %3D int b = 20; int sum = IntStream.of (1, 2, 3, 4, 5) . map (n -> n + b - (++a)) sum () ;
What are the free variables and bound variables in the lambda function in the following code?int a = 1;int b = 2;compute((c, d) -> c + b - a);
What is the difference between a free variable and a bound variable?
The following code does not compile. Why not? How must it be modified? double avg Doublestream.of (3.1, -4.5, 8.9, -6.2, 1.0) %3D - map (n -> Math.abs (n)) .average ();
Write a method firstFive that accepts a file name string as its parameter and uses stream operations to print the first five non-blank lines of the file. Do not use any loops or collections; perform
What value is stored into the variable result by the following code? int result IntStream.of (3, -4, 8, -6, 1) %3D . map (n -> Math.abs (n)) .reduce (0, (a, b) -> a + 2 * b);
Write a method fourLetterWords that accepts a file name as a parameter and returns a count of the number of unique lines in the file that are exactly four letters long. Assume that each line in the
Write a piece of code that uses stream operations to count the number of even integers in a stream. For example, if the stream contains {18, 1, 6, 8, 9, 2}, there are 4 even integers.
Write a piece of code that uses stream operations to compute the sum the negations of a stream of integers. For example, if the stream contains {2, 4, -1, 8}, the sum to compute is −2 + −4 + 1 +
Write a method glueReverse that accepts a List of strings as its parameter and uses stream operations to return a single string consisting of the list’s elements concatenated together in reverse
What value is stored into the variable result by the following code? int result IntStream.of (1, 2, 3, 4, 5, 6, 7) %3D . map (n -> n / 2) .distinct () .count ();
Write a method stdev that computes the standard deviation of an array of real numbers. The formula for computing a standard deviation σ of N values is the following, where xi represents each ith
Is a stream the same as an array? How are they similar, and how are they different?
Write a method toSortedForm that uses stream operations to convert a String parameter into a sorted form with its letters in alphabetical order. For example, if the string passed is "tennessee",
Write a lambda expression that accepts two strings representing a first and last name and concatenates them together into a string in “Last, First” format. For example, if passed "Cynthia" and
Write a method countVowels that uses stream operations to count the number of vowels in a given string. A vowel is an A, E, I, O, or U, caseinsensitive. For example, if the string is "SOO beautiful",
Write a lambda expression that accepts two integers and chooses the larger of the two; for example, if given 4 and 11, it would return the 11.
Write a method pigLatin that uses stream operations to convert a String parameter into its “Pig Latin” form. For this problem we’ll use a simple definition of Pig Latin where the first letter
Write a lambda expression that converts an integer into the square of that integer; for example, 4 would become 16.
Write a method countNegatives that uses stream operations to count how many numbers in a given array of integers are negative. For example, if the array is {5, -1, -3, 20, 47, -10, -8, -4, 0, -6,
What change must be made to the math drilling program from this section for it to support subtraction problems?
Write a method totalCircleArea that uses stream operations to compute and return the sum of the areas of a group of circles, rounded to the nearest whole number. Your function accepts an array of
Write a method largestEven that uses stream operations to find and return the largest even number from an array of integers. For example, if the array is {5, -1, 12, 10, 2, 8}, your method should
What side effect does the following function have? How could it be rewritten to avoid side effects? // Doubles the values of all elements in an array. public static void doubleAll (int[] a) { for
Write a method sumAbsVals that uses stream operations to compute the sum of the absolute values of an array of integers. For example, the sum of {-1, 2, -4, 6, -9} is 22.
Why is calling System.out.println considered a side effect? Does this imply that calling System.out.println is a bad thing?
Write a program that prompts the user for an integer value and that reports the sum of the first prime numbers, reporting a sum of 0 if the user enters a value less than 1. Structure your program to
Write a method printDoubled that uses stream operations to print twice the value of each element of array of integers. For example, if the array passed is {2, -1, 4, 16}, print 4 -2 8 32.
Why do functional programmers want to avoid side effects?
Write a file searching program that uses streams to efficiently search a set of files for a given substring. Write two versions of the code, one that sequentially reads each file with a Scanner and
Draw the array representation of the heap you computed as your answer to Self-Check Problem 21 (after all of the elements are added to it).Data from Self Problem 21Draw the tree for the binary
Draw the array representation of the heap you computed as your answer to Self-Check Problem 19 (after all of the elements are added to it).Data from Self Problem 19Draw the tree for the binary
Draw the min-heap being represented by the given array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 29 41 30 55 68 37 41 80
The following is an incorrect diagram of the array representations of the heap shown in Self-Check Problem 17 using the array representation described in this section of the chapter (before any adds
Perform 3 removals on the heap you drew in the previous problem. Show the complete state of the tree after each removal.Data from Previous ProblemDraw the tree for the binary min-heap that results
Draw the tree for the binary min-heap that results from inserting 11, 9, 12, 14, 3, 15, 7, 8, 1 in that order into an initially empty heap.
Perform 3 removals on the heap you drew in the previous problem. Show the complete state of the tree after each removal.Data from Previous ProblemDraw the tree for the binary min-heap that results
Draw the tree for the binary min-heap that results from inserting 4, 9, 3, 7, 2, 5, 8, 6 in that order into an initially empty heap.
Simulate the adding of value 7 to the same heap from the previous problem, after the 21 has already been added.Data from Previous ProblemSimulate the adding of the value 21 to the following min-heap:
Simulate the adding of the value 21 to the following min-heap: overall root 12 29 70 30 39 84 91 55 64 40 99
Write a method in the HeapIntPriorityQueue class called merge that accepts another HeapIntPriorityQueue as a parameter and adds all elements from the other queue into the current queue, maintaining
In an array heap as implemented in this section, for the element at index 8 of the array, what are the indexes of its left and right children? What is the index of its parent? If the element is at
Write a method in the HeapIntPriorityQueue class called toString that returns a string representation of the elements in the queue, such as "[42, 50, 45, 78, 61]". The order of the elements in the
Which of the following are valid min-heaps? For the one(s) that are invalid, what makes them invalid?a.b.c. 12 30 20 19 34 21
Write a method in the HeapIntPriorityQueue class called toArray that returns the elements of the queue as a filled array. The order of theelements in the array is not important as long as all
If a binary heap has 26 nodes, what is its height? If it has 73 nodes, what is the height? How do you know for sure?
Write a method called fillGaps that accepts a PriorityQueue of integers as a parameter and adds elements to it until every element in its range of smallest to largest is represented once if it was
Which of the following statements about min-heaps is true?a. Smaller values are on the left and larger values are on the right.b. The smallest value is the root.c. The smallest value is one of the
Write a method called stutter that accepts a PriorityQueue of integers as a parameter and replaces every value in the queue with two occurrences of that value. For example, if the queue stores [7, 8,
Suppose we have a hash map that uses the standard “mod” hash function shown in the chapter and uses linear probing for collision resolution. The starting hash table length is 5, and the table
Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is
Write a hashCode method for a Student class, whose fields are a name (a string), age (an integer), student ID number (an integer), and weight in pounds (a real number). Follow the general contract
Write a method called isConsecutive that accepts a PriorityQueue of integers as a parameter and returns true if the queue contains a sequence of consecutive integers starting from the front of the
Write a hashCode method for a Date class, whose fields are a year, month, and day, as integers. Follow the general contract for the hashCode method.
Write a method called kthSmallest that accepts a PriorityQueue of integers k and an integer as parameters and returns the kth-smallest integer from the priority queue. For example, if the queue
For each of the following possible hashCode implementations: Is the following a legal hashCode method for a Point class, according to the general contract of that method? Does it distribute the hash
Write a method called descending that accepts an array of integers and rearranges the integers in the array to be in descending order using a PriorityQueue as a helper. For example, if the array
Suppose we have a hash set that uses the standard “mod” hash function shown in the chapter and uses linear probing for collision resolution. The starting hash table length is 11, and the table
Write a method in the HashIntSet class called toString that returns a string representation of the elements in the set, such as "[-2, 3, 5, 6, 8]". The order of the elements in the string does not
Suppose we have a hash set that uses the standard “mod” hash function shown in the chapter and uses linear probing for collision resolution. The starting hash table length is 5, and the table
Showing 1 - 100
of 1041
1
2
3
4
5
6
7
8
9
10
11