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
Write a method called pairCounts that accepts a list of strings representing individual words and counts the number of occurrences of all 2-character sequences of letters in those words. For example,
Write the map returned when the following method is passed the following maps:a. map1: {bar=1, baz=2, foo=3, mumble=4}, map2: {1=earth, 2=wind, 3=air, 4=fire}b. map1: {five=105, four=104, one=101,
What is recursion? How does a recursive method differ from a standard iterative method?
Write a recursive method called starString that accepts an integer as a parameter and prints to the console a string of stars (asterisks) that is 2n (i.e., 2 to the nth power) long. For
Write a recursive program to solve the “Missionaries and Cannibals” problem. Three missionaries and three cannibals come to a river and find a boat that holds two. If the cannibals ever outnumber
Consider the following method:For each of the following calls, indicate the output that is produced by the method:a. mystery1(1);b. mystery1(2);c. mystery1(3);d. mystery1(4);e. mystery1(16);f.
Write a method called writeSequence that accepts an integer n as a parameter and prints to the console a symmetric sequence of n numbers composed of descending integers that ends in 1, followed by a
Write a recursive program to solve the Towers of Hanoi puzzle. The puzzle involves manipulating disks that you can move between three different towers. You are given a certain number of disks (four
The Koch snowflake is a fractal that is created by starting with a line segment, then recursively altering it as follows:1. Divide the line segment into three segments of equal length.2. Draw an
Consider the following method:For each of the following calls, indicate the output that is produced by the method:a. mystery2(113);b. mystery2(70);c. mystery2(42);d. mystery2(30);e. mystery2(10);
Write a recursive method called doubleDigits that accepts an integer n as a parameter and returns the integer obtained by replacing every digit of with two of that digit. For example,
Write a program that uses recursive backtracking to generate all anagrams from a phrase typed by the user. An anagram is a word or phrase made by rearranging the letters of another word or phrase.
Consider the following method:For each of the following calls, indicate the output that is produced by the method:a. mystery3(0);b. mystery3(1);c. mystery3(2);d. mystery3(4);e. mystery3(5); public
Write a recursive method called writeBinary that accepts an integer as a parameter and writes its binary representation to the console. For example, writeBinary(44) should print 101100.
Write a program that uses recursive backtracking to play the game of Boggle. Boggle is a word game played on a 4 x 4 grid where the player tries to find all valid dictionary words that can be made by
Consider the following method:For each of the following calls, indicate the output that is produced by the method:a. mysteryXY(4, 1);b. mysteryXY(4, 2);c. mysteryXY(8, 2);d. mysteryXY(4, 3);e.
Write a recursive method called writeSquares that accepts an integer parameter and prints the first squares separated by commas, with the odd squares in descending order followed by the even squares
Write a program that uses recursive backtracking to find all ancestors and descendants of a person given a file of familial relationships. For ancestors it must show all parents, all grandparents,
Convert the following iterative method into a recursive method: // Prints each character of the string reversed twice. // doubleReverse ("hello") prints oolllleehh public static void doubleReverse
Write a recursive method called writeChars that accepts an integer parameter and that prints out a total of characters. The middle character of the output should always be an asterisk ( "*" ). If you
What is a call stack, and how does it relate to recursion?
Write a recursive method called multiplyEvens that returns the product of the first even integers. For example, multiplyEvens(1) returns 2 and multiplyEvens(4) returns 384 (because 2 * 4 * 6 * 8 =
What would be the effect if the code for the reverse method were changed to the following? public static void reverse (Scanner input) { if (input.hasNextLine () ) { // recursive case (nonempty file)
Write a recursive method called sumTo that accepts an integer parameter and returns a real number representing the sum of the first reciprocals. In other words, sumTo(n) returns (1 + 1/2 + 1/3 +
What would be the effect if the code for the reverse method were changed to the following? public static void reverse (Scanner input) { if (input.hasNextLine () ) { // recursive case (nonempty file)
Write a recursive method called digitMatch that accepts two nonnegative integers as parameters and that returns the number of digits that match between them. Two digits match if they are equal and
The following method is an attempt to write a recursive pow method to compute exponents. What is wrong with the code? How can it be fixed? public static int pow (int x, int y) { pow (x, y - 1);
Write a recursive method called repeat that accepts a string and an integer as parameters and that returns concatenated together times. For example, repeat("hello", 3) returns "hellohellohello", and
What are the differences between the two versions of the pow method shown in Section 12.3? What advantage does the second version have over the first version? Are both versions recursive?
Write a recursive method called isReverse that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order
Consider the following method:For each of the following calls, indicate the value that is returned:a. mystery4(6, 13)b. mystery4(14, 10)c. mystery4(37, 10)d. mystery4(8, 2)e. mystery4(50, 7) public
Write a recursive method called indexOf that accepts two strings as parameters and that returns the starting index of the first occurrence of the second string inside the first string (or -1 if not
Consider the following method:For each of the following calls, indicate the value that is returned:a. mystery5(5, 7)b. mystery5(12, 9)c. mystery5(-7, 4)d. mystery5( – 23, –48)e. mystery5(128,
Consider the following method:For each of the following calls, indicate the value that is returned:a. mystery6(7, 1)b. mystery6(4, 2)c. mystery6(4, 3)d. mystery6(5, 3)e. mystery6(5, 4) public static
Write a recursive method vowelsToEnd that takes a string as a parameter and returns a string in which all of the vowels have been moved to the end. The vowels should appear in reverse order of what
Convert the following iterative method into a recursive method: // Returns n!, such as 5! = 1*2*3*4 *5 %3D public static int factorial (int n) { int product 1; for (int i = 1; i
Write a recursive method called evenDigits that accepts an integer parameter and that returns the integer formed by removing the odd digits from it. For example, evenDigits(8342116) returns 8426 and
The following method has a bug that leads to infinite recursion. What correction fixes the code? // Adds the digits of the given number. // Example: digitsum (3456) returns 3+4+5+6 = 18 public static
Write a recursive method called permut that accepts two integers n and as parameters and returns the number of unique permutations of r items from a group of n items. For given values of n and r,
Sometimes the parameters that a client would like to pass to a method don’t match the parameters that are best for writing a recursive solution to the problem. What should a programmer do to
The Sierpinski carpet is a fractal that is defined as follows: The construction of the Sierpinski carpet begins with a square. The square is cut into nine congruent subsquares in a 3-by-3 grid, with
The Fibonacci sequence is a sequence of numbers in which the first two numbers are 1 and each subsequent number is the sum of the previous two Fibonacci numbers. The sequence is 1, 1, 2, 3, 5, 8, 13,
The Cantor set is a fractal that is defined by repeatedly removing the middle thirds of line segments as shown in Figure 12.15.Write a program to draw the Cantor set on a DrawingPanel recursively.
What is a fractal image? How does recursive programming help to draw fractals?
Write a recursive method called waysToClimb that takes a positive integer value representing a number of stairs and prints each unique way to climb a staircase of that height, taking strides of one
Write Java code to create and draw a regular hexagon (a type of polygon).
Write a recursive method called countBinary that accepts an integer n as a parameter and that prints all binary numbers that have exactly n digits in ascending order, each on its own line. All digits
Why is recursion an effective way to implement a backtracking algorithm?
Write a recursive method called subsets to find every possible sub-list of a given list. A sub-list of a list L contains 0 or more of L’s contains 0 or more of ’s elements. Your method should
What is a decision tree? How are decision trees important for backtracking?
Write a recursive method called maxSum that accepts a list of integers, L and an integer limit as parameters and uses backtracking to find the maximum sum that can be generated by adding elements of
Draw the decision tree that would have resulted for Figure 12.9 if the backtracking solution had explored NE first instead of last in the recursive explore method. start (0,0) NE (0,1) (1,0) (1,1) NE
Write a recursive method called printSquares to find all ways to express an integer as a sum of squares of unique positive integers. For example, the call printSquares(200); should produce the
The original North/East backtracking solution printed the following ways of traveling to (1, 2) in this order. In what order would they be printed if the solution had explored NE first instead of
Figure 12.12 shows only part of the decision tree for the first two levels. How many entries are there at the second level of the full tree? How many are at level 3 of the full tree? empty col l row
If our 8 Queens algorithm tried every possible square on the board for placing each queen, how many entries are there at the 8th and final level of the full tree? What does our algorithm do to avoid
The 8 Queens explore method stops once it finds one solution to the problem. What part of the code causes the algorithm to stop once it finds a solution? How could the code be modified so that it
The following code is not robust against invalid user input. Describe how to change the code so that it will not proceed until the user has entered a valid age and grade point average (GPA). Assume
Write classes to model a shopping list. Make an Item class that represents a grocery item’s name and price, such as tissues for $3. Also implement an ItemOrder class that represents a shopper’s
Write a program to reverse the lines of a file and also to reverse the order of the words in each line of the file. Use ArrayList s to help you.
Write a family database program. Create a class to represent a person and to store references to the person’s mother, father, and any children the person has. Read a file of names to initialize the
Write a class that models a list of possibly overlapping rectangular twodimensional window regions, like the windows for the programs open on your computer. The order of the rectangles in the list
Extend the ranked choice voting case study to allow for incomplete preferences. Voters are normally allowed to fill out the ballot in an incomplete manner. For example, with the sample ballots with
Write a method called interleave that accepts two ArrayLists of integers a1 and a2 as parameters and inserts the elements of a2 into a1 at alternating indexes. If the lists are of unequal length, the
Write a method called mirror that accepts an ArrayList of strings as a parameter and produces a mirrored copy of the list as output, with the original values followed by those same values in the
Modify the Point class from Chapter 8 so that it defines a natural ordering by implementing the Comparable interface. Compare the Points by -major order; that is, points with smaller -coordinate
What is a natural ordering? How do you define a natural ordering for a class you’ve written?
Modify the TimeSpan class from Chapter 8 to include a compareTo method that compares time spans by their length. A time span that represents a shorter amount of time is considered to be “less
Consider the following variable declarations:Integer n1 = 15;Integer n2 = 7;Integer n3 = 15;String s1 = "computer";String s2 = "soda";String s3 = "pencil";Indicate whether the result of each of the
Modify the CalendarDate class from this chapter to include a year field, and modify its compareTo method to take years into account when making comparisons. Years take precedence over months, which
Use the compareTo method to write code that reads two names from the console and prints the one that comes first in alphabetical order. For example, the program’s output might look like the
Write code to read a line of input from the user and print the words of that line in sorted order, without removing duplicates. For example, the program output might look like the following: Type a
Describe how to arrange an ArrayList into sorted order. What must be true about the type of elements in the list in order to sort it?
Write the output produced when the following method is passed each of the following lists:a. [10, 20, 30]b. [8, 2, 9, 7, 4]c. [-1, 3, 28, 17, 9, 33] public static void mystery4 (ArrayList list) { for
Write the output produced when the following method is passed each of the following lists:a. [72, 20]b. [1, 2, 3, 4, 5, 6]c. [10, 20, 30, 40] public static void mystery3 (ArrayList list) { for (int i
Write the output produced when the following method is passed each of the following lists:a. [10, 20, 30]b. [8, 2, 9, 7, 4]c. [-1, 3, 28, 17, 9, 33] public static void mystery2 (ArrayList list) { for
Write a method called clump that accepts an ArrayList of strings as a parameter and replaces each pair of strings with a single string that consists of the two original strings in parentheses
Write the output produced when the following method is passed each of the following lists:a. [2, 6, 1, 8]b. [30, 20, 10, 60, 50, 40]c. [-4, 16, 9, 1, 64, 25, 36, 4, 49] public static void mystery1
Write a method called filterRange that accepts an ArrayList of integers and two integer values min and max as parameters and removes all elements whose values are in the range min through max
What is a wrapper class? Describe the difference between an int and an Integer .
Write a method called removeShorterStrings that accepts an ArrayList of strings as a parameter and removes from each pair of values the shorter string in the pair. If the list is of odd length, the
The code that follows does not compile. Why not? Explain how to fix it.ArrayList numbers = new ArrayList<>();numbers.add(7);numbers.add(19);System.out.println(numbers);
Write a method called reverse3 that accepts an ArrayList of integer values as a parameter and reverses each successive sequence of three values in the list. If the list has extra values that are not
When the code that follows runs on an ArrayList of String s, it throws an exception. Why? for (String s: words) { System.out.println (s); if (s.equals ("hello")) { words.add ("goodbye"); }
Write a method called markLength4 that accepts an ArrayList of strings as a parameter and that places a string of four asterisks " **** " in front of every string of length 4. For example, suppose
Given the ArrayList from problem 4, write a for-each loop that prints the uppercase version of each String in the list on its own line.Data from Problem 4Write code to insert two additional elements,
Write a method called stutter that accepts an ArrayList of strings and an integer as parameters and that replaces every string with copies of that string. For example, if the list stores the values
Given the ArrayList from problem 4, write code to print out the index at which your list contains the value "stormy" and the index at which it contains "dark". Do not use a loop.Data from Problem
Write a method called removeInRange that accepts three parameters, an ArrayList of strings, a beginning string, and an ending string, and removes from the list any strings that fall alphabetically
Write code to print out whether or not a list of String s contains the value "IS". Do not use a loop.
Write code to declare an ArrayList holding the first 10 multiples of 2: 0, 2, 4,..., 18. Use a loop to fill the list with the proper elements.
Write code to remove from the list any Strings that contain the letter "a". The following should be the list’s contents after your code has executed:["It", "IS", "stormy", "night"]
Write code to change the second element’s value to "IS", producing the following ArrayList as the result:["It", "IS", "a", "dark", "and", "stormy", "night"]
Write code to insert two additional elements, " dark " and " and ", at the proper places in the list to produce the following ArrayList as the result:["It", "was", "a", "dark", "and", "stormy",
The next five questions refer to the following String elements:["It", "was", "a", "stormy", "night"]Write the code to declare an ArrayList containing these elements. What is the size of the list?
Write a method called rangeBetweenZeroe s that takes as a parameter an ArrayList of integers and returns the number of indexes apart the two farthest occurrences of the number 0 are. For example, if
Write a method called maxLength that takes an ArrayList of String s as a parameter and that returns the length of the longest String in the list. If your method is passed an empty ArrayList, it
Write a method called removeZeroe s that takes as a parameter an ArrayList of integers and eliminates any occurrences of the number 0 from the list. For example, if the list stores the values [0, 7,
Write a method called removeDuplicates that takes as a parameter a sorted ArrayList of strings and eliminates any duplicates from the list. For example, if the list stores the values ["be", "be",
Write a method called minToFront that takes an ArrayList of integers as a parameter and moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example,
Showing 400 - 500
of 1041
1
2
3
4
5
6
7
8
9
10
11