Question

Data Structures: Abstraction and Design Using Java Elliot B. Koffman; Paul A. T. Wolfgang ch 5...

Data Structures: Abstraction and Design Using Java Elliot B. Koffman; Paul A. T. Wolfgang ch 5 review questions 5 and 7

5. For Towers of Hanoi, show the output string that would be created by the method call showMoves(3, 'R', 'M', 'L'). Also, show the sequence of method calls.

7. Write a recursive method that will dispense change for a given amount of money. The method will display all combinations of quarters, dimes, nickels, and pennies that equal the desired amount.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

5.

class TowersOfHanoi
{
   static void showMoves(int n, char r, char m, char l)
   {
       if(n>0)
       {
           showMoves(n-1,r,l,m);
           System.out.println("Move "+ n + " disk from " + r + " to " + m);
           showMoves(n-1,l,m,r);
       }
   }
  
   public static void main (String[] args)
   {
       showMoves(3, 'R', 'M', 'L');
}
}

Output Strings:

Move 1 disk from R to M
Move 2 disk from R to L
Move 1 disk from M to L
Move 3 disk from R to M
Move 1 disk from L to R
Move 2 disk from L to M
Move 1 disk from R to M

Sequence of method calling:

class TowersOfHanoi
{
   static void showMoves(int n, char r, char m, char l)
   {
       if(n>0)
       {   
           System.out.println("showMoves(" + (n-1) + ", " + r + ", " + l + ", " + m + ")");
           showMoves(n-1,r,l,m);
// System.out.println("Move "+ n + " disk from " + r + " to " + m);
           System.out.println("showMoves(" + (n-1) + ", " + l + ", " + m + ", " +r + ")");
           showMoves(n-1,l,m,r);
       }
   }
  
   public static void main (String[] args)
   {
       showMoves(3, 'R', 'M', 'L');
}
}

Output of method calling sequence (without the moves):


showMoves(2, r, l, m)
showMoves(1, r, m, l)
showMoves(0, r, l, m)
showMoves(0, l, m, r)
showMoves(1, m, l, r)
showMoves(0, m, r, l)
showMoves(0, r, l, m)
showMoves(2, l, m, r)
showMoves(1, l, r, m)
showMoves(0, l, m, r)
showMoves(0, m, r, l)
showMoves(1, r, m, l)
showMoves(0, r, l, m)
showMoves(0, l, m, r)


7.

import java.util.*;

class Change
{
   static ArrayList<int[]> combos = new ArrayList<int[]>();
  
   //method to genarate different combinations of changes
   static void change(int amount, int q, int d, int n, int p)
   {
// base condition
if (q * 25 + d * 10 + n * 5 + p > amount) return;

       //write the combination
writeCombo(q, d, n, p);

       /*check if penny greater than or equal to 5 then increase nickel
       * by one and decrease penny by 5 */
if (p >= 5) change(amount, q, d, n + 1, p - 5);
  
   /*check if penny greater than or equal to 10 then increase dime
       * by one and decrease penny by 10 */
if (p >= 10) change(amount, q, d + 1, n, p - 10);

/*check if penny greater than or equal to 25 then increase quarter
       * by one and decrease penny by 25 */
if (p >= 25) change(amount, q + 1, d, n, p - 25);
}
  
//method to write the combination
static void writeCombo(int q, int d, int n, int p)
{
int[] newCombo = { q, d, n, p };

for (int[] combo : combos)
{
if (Arrays.equals(newCombo, combo))
   return; // this combination already stored
}
combos.add(newCombo);
}  
      
   //main method
   public static void main (String[] args)
   {
       //create object of Scanner class
       Scanner sc = new Scanner(System.in);
      
       System.out.print("Enter any amount in cents: ");
      
       //read the amount in cents
       int amount = sc.nextInt();
      
       //call the change() method
       change(amount, 0, 0, 0, amount);
      
       System.out.println("\nAll possible combinations for " + amount + " cents:");

//display all the combinations
for (int[] a : combos)
{
System.out.println(a[0] + " Quarter\t" + a[1] + " Dime\t" + a[2] + " Nickel\t" + a[3] + " Penny");
}
}
}

Output:


Enter any amount in cents: 49

All possible combinations for 49 cents:
0 Quarter 0 Dime 0 Nickel 49 Penny
0 Quarter 0 Dime 1 Nickel 44 Penny
0 Quarter 0 Dime 2 Nickel 39 Penny
0 Quarter 0 Dime 3 Nickel 34 Penny
0 Quarter 0 Dime 4 Nickel 29 Penny
0 Quarter 0 Dime 5 Nickel 24 Penny
0 Quarter 0 Dime 6 Nickel 19 Penny
0 Quarter 0 Dime 7 Nickel 14 Penny
0 Quarter 0 Dime 8 Nickel 9 Penny
0 Quarter 0 Dime 9 Nickel 4 Penny
0 Quarter 1 Dime 7 Nickel 4 Penny
0 Quarter 1 Dime 6 Nickel 9 Penny
0 Quarter 1 Dime 5 Nickel 14 Penny
0 Quarter 2 Dime 5 Nickel 4 Penny
0 Quarter 1 Dime 4 Nickel 19 Penny
0 Quarter 2 Dime 4 Nickel 9 Penny
1 Quarter 0 Dime 4 Nickel 4 Penny
0 Quarter 1 Dime 3 Nickel 24 Penny
0 Quarter 2 Dime 3 Nickel 14 Penny
0 Quarter 3 Dime 3 Nickel 4 Penny
1 Quarter 0 Dime 3 Nickel 9 Penny
0 Quarter 1 Dime 2 Nickel 29 Penny
0 Quarter 2 Dime 2 Nickel 19 Penny
0 Quarter 3 Dime 2 Nickel 9 Penny
1 Quarter 1 Dime 2 Nickel 4 Penny
1 Quarter 0 Dime 2 Nickel 14 Penny
0 Quarter 1 Dime 1 Nickel 34 Penny
0 Quarter 2 Dime 1 Nickel 24 Penny
0 Quarter 3 Dime 1 Nickel 14 Penny
0 Quarter 4 Dime 1 Nickel 4 Penny
1 Quarter 1 Dime 1 Nickel 9 Penny
1 Quarter 0 Dime 1 Nickel 19 Penny
0 Quarter 1 Dime 0 Nickel 39 Penny
0 Quarter 2 Dime 0 Nickel 29 Penny
0 Quarter 3 Dime 0 Nickel 19 Penny
0 Quarter 4 Dime 0 Nickel 9 Penny
1 Quarter 2 Dime 0 Nickel 4 Penny
1 Quarter 1 Dime 0 Nickel 14 Penny
1 Quarter 0 Dime 0 Nickel 24 Penny

Add a comment
Know the answer?
Add Answer to:
Data Structures: Abstraction and Design Using Java Elliot B. Koffman; Paul A. T. Wolfgang ch 5...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Resource: Ch. 5, "Recursion", of Data Structures: Abstraction and Design Using Java, Exercises for Section 5.6...

    Resource: Ch. 5, "Recursion", of Data Structures: Abstraction and Design Using Java, Exercises for Section 5.6 Complete the following Review Questions within the "Exercises for Sections 5.6" subsection in Section 5.6, "Backtracking" of Ch. 5, "Recursion" in Data Structures: Abstraction and Design Using Java Review Question #7 For the maze path found in Figure 5.19, explain why cells (3, 4), (2, 5), (3, 5), and (4, 5) were never visited and why cells (5, 1) and (3, 0) through (9,...

  • Resource: Ch. 10, "Graphs", of Data Structures: Abstraction and Design Using Java, Exercises for Section 10.4;...

    Resource: Ch. 10, "Graphs", of Data Structures: Abstraction and Design Using Java, Exercises for Section 10.4; Self-Check #1 Complete the Self-Check Question #1 within "Exercises for Sections 10.4" subsection in Section 10.4, "Traversals of Graphs" of Ch. 10, "Graphs" in Data Structures: Abstraction and Design Using Java. Document the breadth-first search trees in the Self-Check question. Submit the assignment to the Assignment Files tab. 4. Show the breadth-first search trees for the following graphs. 2 1 3 0 4

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • This needs to be done in Java using Using Multiple Generic Data Structures – LinkedList and Array...

    this needs to be done in Java using Using Multiple Generic Data Structures – LinkedList and ArrayList In this assignment, you will write a system to manage email address lists (ostensibly so that you could send emails to a list which has a name – which would send an email to each of the addresses in the list – although we will not actually implement sending emails.  Displaying the list will simulate being able to send the emails). We will...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT