Question

Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

Java class quiz need help~

This is the Backwards.java code.

public class Backwards {

   /**
    * Program starts with this method.
    * 
    * @param args A String to be printed backwards
    */
   public static void main(String[] args) {
      if (args.length == 0) {
         System.out.println("ERROR: Enter a String on commandline.");
      }
      else {
         String word = args[0];
         String backwards = iterativeBack(word); // A (return address)
         System.out.println("Iterative solution: " + backwards);
         backwards = recursiveBack(word); // B (return address)
         System.out.println("\n\nRecursive solution: " + backwards);
         backwards = recursiveBack2(word); // C (return address)
         System.out.println("\n\nRecursive solution2: " + backwards);
         System.out.println();
      }
   }

   /**
    * Writes a character string backward using iteration.
    * 
    * @param s a character string
    * @return a backwards string
    */
   public static String iterativeBack(String s) {
      int len = s.length();
      String backWord = new String();
      for (int i = len - 1; i >= 0; i--) {
         backWord = backWord + s.substring(i, i + 1);
      }
      return backWord;
   }

   /**
    * Writes a character string backward using recursion.
    * 
    * @param s a character string
    * @return a backwards string
    */
   public static String recursiveBack(String s) {
      int len = s.length();
      // base case: output string if only has one character
      if (len == 1) {
         return s;
      }
       // recursive case: display last character,
       // then call method again with shorter string
       // that has the last character cut off
      else {
         return s.substring(len - 1, len)
              + recursiveBack(s.substring(0, len - 1));
         // D (return address)
      } // end of else
   } // end of recursiveBack()

   /**
    * Writes a character string backward using recursion.
    * 
    * @param s a character string
    * @return a backwards string
    */
   public static String recursiveBack2(String s) {
      int len = s.length();
      System.out.println("String: " + s);
      System.out.println("Length: " + len);
      // base case: output string if only has one character
      if (len == 1) {
         System.out.println("return: " + s);
         return s;
      }
       // recursive case: call method again with shorter string
       // that has the first character cut off,
       // then display first character
      else {
         System.out.print("return: recursiveBack2(s.substring(1,len))");
         System.out.println(" + " + s.substring(0, 1));      
         return recursiveBack2(s.substring(1, len)) + s.substring(0, 1);
         // E (return address)
         
      } // end of else
   } // end of recursiveBack2()

} // end of class

The question is :Using the Backwards.java program

  • Do a box trace of a call to second recursive method recursiveBack2 (starts on line 73)
  • Use the string "dog" as the input parameter.
  • This should be very similar to what was done in the slides in the Lecture on Recursion pt 1, starting on slide 34.
  • Each activation record (ARI) (box) should have the return address (as shown in the code comments), the parameter(s), local variable(s), and the return value at each step.
  • Draw a new set of ARIs for each step

can do a hand drawing or on the computer, thank you!!

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...
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
  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • I just need to add comment for the code blow. Pleas add comment for each method...

    I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. *    * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...

  • (20 pts) Fill in the missing code: This recursive method returns “even” if the length of...

    (20 pts) Fill in the missing code: This recursive method returns “even” if the length of a give String is even, and “odd” if the length of the String is odd. public static String foo(String s) { if (s.length() ==0)    return “even”; else if (s.length() = = 1)    return “odd”; else     //your code goes here } (40 pts) You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n)...

  • This looks long but it is easy just having some trouble please help out thank you....

    This looks long but it is easy just having some trouble please help out thank you. Find and fix all syntax and semantic errors which prevent the program from compiling. Find and fix all logical errors which cause the program to crash and/or produce unexpected results. In addition to making sure the code compiles, we have one final method which we need to complete. There is a method in the code, printAll, which is responsible for printing out the entirety...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

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
ADVERTISEMENT