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
building java programs a back to basics approach
Building Java Programs A Back To Basics Approach 5th Edition Stuart Reges, Marty Stepp - Solutions
The java.util package has an interface called ListIterator that extends the Iterator interface and includes additional methods that are specific to iterating through the elements of lists forward or backward. Write a class called LinkedListIterator2 that adds some or all of these methods for
What is the difference between a linked list and an array list? How are they similar?
Write a method called set that accepts an index and a value and sets the list’s element at that index to have the given value. You may assume that the index is between 0 (inclusive) and the size of the list (exclusive).
The actual List interface in the java.util package has several methods beyond the ones implemented in this chapter. Write a version of LinkedList that adds some or all of these methods. The methods to add are the following (some headers are slightly modified; see the Java API Specification for
What is one benefit of making the list iterator into an inner class?
Why is it important to set empty elements to null when we are clearing or removing from the list of type E, when we didn’t need to clear out these elements in the previous ArrayIntList?Data from Previous ExerciseWhat is an annotation? How are annotations useful in writing our ArrayList class?
What is an annotation? How are annotations useful in writing our ArrayList class?
What changes need to be made to the indexOf method to search for objects of type E in the new list class, and why are these changes necessary?
Since our list stores an unfilled array, the empty elements were filled with the value 0 when our array was full of integers. What value occupies the empty cells when our list stores values of type E?
What problem do we encounter when we try to construct an array of type E? How do we resolve this problem?
Write a method called switchPairs that switches the order of values in the list in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. If the list contains an odd number of values, the
Write a method called average that returns the average of the values in the list as a real number. For example, if a variable called list stores [11, –7, 3, 42, 0, 14], the call of list.average() should return 10.5. If the list is empty, average should return 0.0.
Write a method called sum that returns the sum of all values in the list. For example, if a variable called list stores [11, –7, 3, 42, 0, 14], the call of list.sum() should return 63. If the list is empty, sum should return 0.
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 example, if the list stores [1, 7, 3, 9, 4, 6, 5], your method should change it to store [8, 12, 10,
What is a precondition of the iterator’s remove method? How does the iterator enforce this precondition, and what does it do if the precondition is violated?
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 the list stores [1, 8, 2, 7], your method should change it to store [1, 8, 2, 7, 1, 8, 2, 7].
How does the array list iterator know if there are more elements left to examine? What does it do if the client tries to examine a next element but there are none left to examine?
Write a method called stretch that takes an integer as a parameter and that increases a list of integers by a factor of by replacing each integer in the original list with copies of that integer. For example, if a variable called list stores [18, 7, 4, 24, 11] and we make the call of
What state does the array list iterator store?
Write a method called stutter that replaces every value with two of that value. For example, if the list initially stores [42, 7, 0, –3, 15], after the call it should store [42, 42, 7, 7, 0, 0, –3, –3, 15, 15].
What is the benefit of adding an iterator to the list class?
Write a method called mirror that doubles the size of a list by appending the mirror image of the original sequence to the end of the list. The mirror image is the same sequence of values in reverse order. For example, if a variable called list stores [1, 3, 2, 7] and the client calls
When this new version of the class fills to its capacity, it resizes. How much does it grow? Why choose this growth rate, rather than increasing the capacity by a single element or other constant amount?
Write a method called printInversions that lists all inversions in a list of integers. An inversion is a pair of numbers in which the first appears before the second in the list, but the first is greater than the second. Thus, for a sorted list such as [1, 2, 3, 4] there are no inversions at all,
Why do we bother to add the contains, isEmpty, and remove methods to the list class, when the client can already perform this same functionality with the indexOf, size, and remove methods, respectively?
Write a method removeAll that accepts an integer value as a parameter and that removes all occurrences of the given value from the list.
Once we check thoroughly for preconditions in the code, what data invariants can we now assume about the list?
Write a method called removeFront that takes an integer as a parameter and that removes the first values from a list of integers. For example, if a variable called list stores [8, 17, 9, 24, 42, 3, 8] and a call of list.removeFront(4); is made, the list’s contents should become [42, 3, 8]. You
What is the purpose of the checkCapacity method? Where is it called in the list class? Describe a way that the client can utilize an ArrayIntList that will be caught by checkCapacity.
Write a method called removeLast that removes and returns the last value from a list of integers. For example, if a variable called list stores [8, 17, 42, 3, 8], a call of list.removeLast(); should return 8 and change the list’s state to [8, 17, 42, 3]. The next call would return 3 and remove 3
What is the purpose of the checkIndex method? Where is it called in the list class? Describe a way that the client can utilize an ArrayIntList that will be caught by checkIndex.
Write a method called longestSortedSequence that returns the length of the longest sorted sequence within a list of integers. For example, if a variable called list stores [1, 3, 5, 2, 9, 7, –3, 0, 42, 308, 17], then the call of list.longestSortedSequence() would return 4 because it is the length
Describe the overall preconditions placed on the list class in this section. What assumptions do we make about how clients will use the list?
Write a method called maxCount that returns the number of occurrences of the most frequently occurring value in a sorted list of integers. Because the list will be sorted, all duplicates will be grouped together, which will make it easier to count duplicates. For example, if a variable called list
Write methods called min and max that return the smallest and largest values in the list respectively. For example, if a variable called list stores [11, –7, 3, 42, 0, 14], the call of list.min() should return –7 and the call of list.max() should return 42. If the list is empty, the methods
Write a method called count that accepts an element value as a parameter and returns the number of occurrences of that value in the list. For example, suppose a variable named list stores [2, -3, 2, 0, 5, 2, 2, 6]. A call of list.count(2) should return 4 because there are four occurrences of that
An element can be inserted at the beginning, middle, or end of an array list. Which of the three insertion points is the most computationally expensive, and why? Which is the most expensive location to remove an element from the list?
Write a method called isPairwiseSorted that returns whether or not a list of integers is pairwise sorted. A list is considered pairwise sorted if each successive pair of numbers is in nondecreasing order. For example, if a variable called list stores [3, 8, 2, 5, 19, 24, –3, 0, 4, 4, 8, 205, 42]
We wrote the class to have public methods called size (to read the number of elements of the list) and get (to access the element value at a specific index). Why is this approach better than declaring the fields (such as size ) public?
Write a method called fill that accepts an integer value as a parameter and replaces every value in the list with that value. For example, if a variable called list initially stores [42, –7, 3, 0, 15] and the call of list.fill(2); is made, the list will be changed to store [2, 2, 2, 2, 2].
Why does the list class use a toString method rather than a print method?
Write a method called runningTotal that returns a new ArrayIntList that contains a running total of the original list. In other words, the ith value in the new list should store the sum of elements 0 through i of the original list. For example, given a variable list that stores [2, 3, 5, 4, 7, 15,
Based on the implementation of ArrayIntList or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list, rather than appending it to the end of the list, it is placed in
In this version of the list class, what happens if the client adds too many values to fit in the array?
Write a method called reverse that reverses the order of the elements in the array list. For example, if a variable called list stores [11, –7, 3, 42, 0, 14, 56], the call of list.reverse(); should change the list to store [56, 14, 0, 42, 3, –7, 11]. An empty or one-element list is not changed
How would the output of the Client1 program shown in this section change if each field from ArrayIntList were declared static ?
The actual ArrayList class in the java .util package has a method called subList that returns a view of a subportion of a list through a given range of indexes. It can be useful to think of part of a list as if it were its own list, complete with its own set of indexes and values. The sublist is
Write a method called replaceAll that accepts two integer values as parameters and replaces all occurrences of the first value in the list with the second value. For example, if a variable called list stores [11, –7, 3, 42, 3, 0, 14, 3] , the call of list.replaceAll(3, 999); should change the
What fields must be included in the ArrayIntList class, and why is each field important? Would the class still work correctly if we removed any of these fields?
The java.util package has an interface called ListIterator that extends the Iterator interface and includes additional methods specific to iterating through the elements of lists forward or backward. Write a class called ArrayListIterator 2 that adds some or all of these methods. The methods to add
Write a method called indexOfSubList that accepts another list L as a parameter and returns the starting index of where L first appears in this list, or -1 if it is not found. All elements of L must appear in sequence and in the same order. For example, if variables called list1 and list2 store
What is the difference between an array list’s size and its capacity? What is the relationship between the two values? (Is one always larger or smaller than the other, for instance?)
The actual List interface in the java.util package has several methods beyond the ones implemented in this chapter. Write a version of ArrayList that adds some or all of these methods. The methods to add are as follows (some headers are slightly modified; see the Java API Specification for
Write a method called lastIndexOf that accepts an integer as a parameter and returns the index in the list of the last occurrence of that value, or -1 if the value is not found in the list. For example, if the list stores [1, 18, 2, 7, 18, 39, 18, 40], then the last index of 18 is 6 and the last
Write a piece of code that finds and prints the longest string in a stack of strings. For example, in the stack [hello, hi, goodbye, howdy], the longest string is "goodbye". When your code is done running, the stack should still contain the same contents as it had at the start. In other words, if
Write a piece of code that prints the elements of a queue of integers, one per line. When your code is done running, the queue should still contain the same contents as it had at the start. In other words, don’t destroy the queue as you print it. If you like, put your code into a method called
Write a method called maxToTop that takes a stack of integers as a parameter and moves the largest value in the stack to the top of the stack, leaving all other values in their original order. You may assume that the stack does not contain any duplicates. For example, if a stack s stores [27, 5,
The following piece of code incorrectly attempts to remove all even values from a stack of integers. What is wrong with the code, and how would you fix it? while (!s.isEmpty ()) { int n = s.pop (); if (n % 2 != 0) { s.push (n); // put back in stack if odd
Write a method called interleave that accepts a queue of integers as a parameter and rearranges the elements by alternating the elements from the first half of the queue with those from the second half of the queue. For example, if the queue stores [2, 8, −5, 19, 7, 3, 24, 42] , your method
The following piece of code incorrectly attempts to compute the sum of all positive values in a queue of integers. What is wrong with the code, and how would you fix it? int sum = while (!q.isEmpty ()) { if (q. remove () > 0) { sum += q.remove () ;
Write a method called removeMin that accepts a stack of integers as a parameter and removes and returns the smallest value from the stack. For example, if the stack stores [2, 8, 3, 19, 2, 3, 2, 7, 12, −8, 4], your method should remove and return −8, leaving the stack storing [2, 8, 3, 19, 2,
The following piece of code incorrectly attempts to find the largest value in a queue of integers. What is wrong with the code, and how would you fix it? int largest = q.remove () ; for (int i = 0; i < q.size (); i++) { largest = Math.max (largest, q. remove () );
Write a method called mirrorHalves that accepts a queue of integers as a parameter and replaces each half of that queue with itself plus a mirrored version of itself (the same elements in the opposite order). For example, if the queue stores [10, 50, 19, 54, 30, 67], your method should change it to
Write a method called compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, if the stack stores [2, 2, 2, 2, 2, −4, −4,
Write the output produced when the following method is passed each of the following queues:a. [1, 2, 3, 4, 5, 6]b. [42, −3, 4, 15, 9, 71]c. [30, 20, 10, 60, 50, 40, 3, 0] public static void mystery2 (Queue q) { Stack s = new Stack () ; int size = q.size (); for (int i = 0; i < size; i++) { int n
Write a method called mirror that accepts a stack of integers as a parameter and replaces the stack contents with itself plus a mirrored version of itself (the same elements in the opposite order). For example, if the stack stores [10, 53, 19, 24] , your method should change it to store [10, 53,
Write the output produced when the following method is passed each of the following stacks:a. [2, 6, 1]b. [42, −3, 4, 15, 9]c. [30, 20, 10, 60, 50, 40] public static void mysteryl (Stack s) { Queue q = new LinkedList (); %3D while (!s.isEmpty ()) { int n = s.pop (); q. add (n) ; q.add (n); while
Write a method called isSorted that accepts a stack of integers as a parameter and returns true if the elements in the stack occur in ascending (nondecreasing) order from top to bottom. That is, the smallest element should be on top, growing larger toward the bottom. For example, if the stack
What is the output of the following code?Queue q = new
Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20, 30, 40, 50, 60, 70, 80, 90] , the
What is the output of the following code?Stack s = new
Write a method called expunge that accepts a stack of integers as a parameter and makes sure that the stack’s elements are in nondecreasing order from top to bottom, by removing from the stack any element that is smaller than any element(s) on top of it. For example, if the stack stores [4, 20,
Write a method called shift that accepts a stack of integers and an integer n as parameters and that shifts n values from the bottom of the stack to the top of the stack. For example, if the stack named s stores [1, 2, 3, 4, 5, 6, 7, 8] , and we make the call shift(s, 3); your method should shift
Stacks and queues have less functionality than other similar collections like lists and maps. Why are they still useful despite lacking functionality? What possible advantages are there of using a less powerful collection?
Write a method called reorder that accepts a queue of integers as a parameter and that puts the integers into sorted (nondecreasing) order, assuming that the queue is already sorted by absolute value. For example, if the queue stores [1, 2, −2, 4, −5, 8, −8, 12, −15], notice that the values
Stacks and queues do not have index-based methods such as get from ArrayList. How can you access elements in the middle of a stack or queue?
Write a method called isConsecutive that accepts a stack of integers as a parameter and that returns true if the stack contains a sequence of consecutive integers starting from the bottom of the stack.Consecutive integers are integers that come one after the other, as in 3, 4, 5, etc. If the stack
Write a piece of code that declares a queue of strings and fills it with the following contents, such that "alpha" is at the front of the queue and "delta" is at the back: [alpha, beta, gamma, delta]. You can print the queue to verify its state.
Write a method called switchPairs that accepts a stack of integers as a parameter and swaps neighboring pairs of numbers starting at the bottom of the stack. For example, if the stack initially stores [1, 2, 8, 6, −1, 15, 7] , your method should swap the first pair (1, 2), the second pair (8, 6),
Write a piece of code that declares a stack of integers and uses a loop to fill it with the multiples of 2 from 0 through 100 inclusive, such that 0 is at the top of the stack and 100 is at the bottom: [100, 98, 96, ..., 4, 2, 0]. You can print the stack to verify its state.
Write a method called isPalindrome that accepts a queue of integers as a parameter and returns true if the numbers in the queue are the same in reverse order. For example, if the queue stores [3, 8, 17, 9, 17, 8, 3], your method should return true because this sequence is the same in reverse order.
Write a piece of code that declares a stack of strings and fills it with the following contents, such that "howdy" is at the top of the stack and "hello" is at the bottom: [hello, hi, goodbye, howdy]. You can print the stack to verify its state.
Write a method called reverseHalf that accepts a queue of integers as a parameter and reverses the order of all the elements in oddnumbered positions (position 1, 3, 5, etc.), assuming that the first value in the queue has position 0. For example, if the queue stores [1, 8, 7, 2, 9, 18, 12, 0] ,
The following piece of code incorrectly attempts to declare a queue of integers. What is wrong with the code, and how would you fix it?Queue q = new Queue<>();
Write a method called rearrange that accepts a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the queue. For example, if the queue stores [3, 5, 4, 17, 6, 83, 1,
If you create a new empty queue and add the values 1, 2, and 3 in that order, and call remove on the queue once, what value will be returned?
Write a Guitar Hero program that uses a queue to simulate the creation of guitar notes. When a guitar string is plucked, it vibrates to generate sound. The sound starts from the note’s initial note pitch or frequency, then it undergoes a wave-like oscillation and gradually fades in volume over
Write a method called equals that accepts two stacks of integers as parameters and returns true if the two stacks store exactly the same sequence of integer values in the same order. Your method must restore the two stacks to their original state before returning. Use one stack as auxiliary storage.
If you create a new empty stack and push the values 1, 2, and 3 in that order, and call pop on the stack once, what value will be returned?
Write a Maze Explorer program that uses stacks and queues to implement an algorithm to escape from a maze. The overall pseudocode of the algorithm is the following. The algorithm can be implemented using a stack or a queue. What are the pros and cons of each? create an empty stack of locations to
Write a method called collapse that accepts a stack of integers as a parameter and that collapses it by replacing each successive pair of integers with the sum of the pair. For example, if the stack stores [7, 2, 8, 9, 4, 11, 7, 1, 42] , the first pair should be collapsed into 9 (7 + 2), the second
When you call add on a queue, where is the new element placed relative to the other elements in the queue? When you call remove, which element from the queue is returned?
Write a method called copyStack that accepts a stack of integers as a parameter and returns a copy of the original stack (i.e., a new stack with the same values as the original, stored in the same order as the original). Your method should create the new stack and fill it up with the same values
When you call push on a stack, where is the new element placed relative to the other elements in the stack? When you call pop, which element from the stack is returned?
Write a method called stutter that accepts a stack of integers as a parameter and replaces every value in the stack with two occurrences of that value. Preserve the original relative order. For example, if the stack stores [3, 7, 1, 14, 9], your method should change it to store [3, 3, 7, 7, 1, 1,
What is a real-world example of data that could be modeled using a stack? Using a queue?
Write a Primes program that finds prime numbers using the Sieve of Eratosthenes, an algorithm devised by a Greek mathematician of the same name who lived in the third century BC. The algorithm finds all prime numbers up to some maximum value n, as described by the following pseudocode:Several web
Write a method called splitStack that accepts a stack of integers as a parameter and rearranges its elements so that all the negatives appear on the bottom of the stack and all the nonnegatives appear on the top. If after this method is called you were to pop numbers off the stack, you would first
Which of the following statements about stacks and queues is true?a. Stacks and queues can store only integers as their data.b. A stack returns elements in the same order as they were added (first-in, first-out).c. A queue’s remove method removes and returns the element at the front of the
Trace the complete execution of the merge sort algorithm when called on each array below. Show the sub-arrays that are created by the algorithm and show the merging of sub-arrays into larger sorted arrays.a. {29, 17, 3, 94, 46, 8, –4, 12}b. {6, 5, 3, 7, 1, 8, 4, 2}c. {33, 14, 3, 95, 47, 9, –42,
Showing 200 - 300
of 1041
1
2
3
4
5
6
7
8
9
10
11
Step by Step Answers