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
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
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 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
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
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
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()
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
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
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
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
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,
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
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
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
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,
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,
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
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,
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
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()
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,
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
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
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
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,
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
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
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
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
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
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 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
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
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
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
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
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
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 ();
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
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
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,
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
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
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,
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
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).
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
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
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
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
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
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
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
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
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,
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
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
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,
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,
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
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
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
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
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
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
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
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
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
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
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
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
Showing 200 - 300
of 1041
1
2
3
4
5
6
7
8
9
10
11