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
can do a hand drawing or on the computer, thank you!!
Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...
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 ...
Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0) return 1; else System.out.println(n* foo(n-1) ); } //line 15 At compile time, you get the following error: Text.java: 15: missing return statement } ...
Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string), findFirstP(finds first p in a string)) in java language. public class simpleLoops { /** * @param args */ public static void main(String[] args) { System.out.println(longestRun("aabbbccd")); System.out.println("Expected 3"); System.out.println(longestRun("aaa")); System.out.println("Expected 3"); System.out.println(longestRun("aabbbb")); System.out.println("Expected 4"); int count = countP("Mississippi"); System.out.println(count); int result = findLastP("Mississippi"); System.out.println(result); result = findFirstP("stop"); System.out.println(result); result = findFirstP("xxxyyyzzz"); System.out.println(result); } /** *...
instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1. What will this method return if you call it this: xMethod (4) static int xMethod (int n) { if (n == 1) return 1; else return n + xMethod (n - 1); } 1. 10 2. 11 3. 12 4. 9 2. Analyze the following code: public class Test { public static void main (String [] args) { int [] x = {1, 2,...
In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...
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 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 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. 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) { 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');...