Question

public class Mystery { public static String mystery(String str, int input) { String result = "";...

public class Mystery {
  public static String mystery(String str, int input) {
    String result = "";
    for (int i = 0; i < str.length() - 1; i++) {
      if (input == 0) {
        str = "";
        result = str;
      }
      
      if (input == -2) {
        result = str.substring(2, 4);
      }
      
      if (input == 1) {
        result = str.substring(0, 1); 
      }
      
      if (input == 2) {
        result = str.substring(0, 2); 
      }
      
      if (input == 3) {
        result = str.substring(2, 3);
      }
      
      if (input == 4) {
        result = str.substring(3, 4);
      }
      
     
      
  
  
    }
    return result;
  }
}
Failed 1 tests. Here is one:
Testing mystery(String str = "test", int input = 8) failed:
Solution returned: "test"
Submission returned: ""

In a public class named Mystery, how do I write a static method that takes a String and an int value, and returns a string based on the int value.

These are some possible tests:

Testing mystery(String str = "GW", int input = -3) failed:
Solution returned: "GW"
Testing mystery(String str = "es", int input = -3) failed:
Solution returned: "es"
Testing mystery(String str = "nPdrLLS", int input = -4) failed:
Solution returned: "rLLS"
Testing mystery(String str = "ZoMEPS", int input = -3) failed:
Solution returned: "EPS"
Testing mystery(String str = "ULwFTrKGnp", int input = -6) failed:
Solution returned: "TrKGnp"

Homework Answers

Answer #1
public class Mystery {
  public static String mystery(String str, int input) {
    String result = "";
    for (int i = 0; i < str.length() - 1; i++) {
      if (input == 0) {
        str = "";
        result = str;
      }
      
      if (input == -2) {
        result = str.substring(2, 4);
      }
      
      if (input == 1) {
        result = str.substring(1,str.length(); 
      }
      
      if (input == 2) {
        result = str.substring(2, str.length()); 
      }
      
      if (input == 3) {
        result = str.substring(2, 3);
      }
      
      if (input == 4) {
        result = str.substring(3, 4);
      }
      
     
      
  
  
    }
    return result;
  }
}
Know the answer?
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
Similar Questions
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
public static int mystery(int[] elements, int target)   {     for (int j = 0; j <...
public static int mystery(int[] elements, int target)   {     for (int j = 0; j < elements.length; j++)      {    if (elements[j] == target)    {     return j;       }        }    return -1;    } What would the following code return from mystery ([90, -30, 50], -30)?
C Programming 1. Provide an equivalent of the java class in C class Example { public...
C Programming 1. Provide an equivalent of the java class in C class Example { public static int[][] question4(int n) { int[][] result = new int [n][n]; for(int i=1; i<=n; i+=1) for (int j=1; j<=n; j+=1) result[i][j] = (i*n)+j; return result; } public static void main(String[] args) { int[][] my array = question4(4); } } 2. Rewrite the following function using no loops, and only tail call recursion int min (int n, int[] input) { int i; int result; for...
import java.util.Scanner; import java.io.*; public class P1 { static final int ROW = 1000; static final...
import java.util.Scanner; import java.io.*; public class P1 { static final int ROW = 1000; static final int COL = 667; public static void readImage(int[][][] startImage, String fileName) { Scanner inputF = new Scanner(fileName); int row = 0, col = 0; int line = 1; while (inputF.hasNext()) { if (line <= 4) { inputF.nextLine(); line++; } else { line += 3; if (col < COL) { startImage[row][col][0] = inputF.nextInt(); startImage[row][col][1] = inputF.nextInt(); startImage[row][col][2] = inputF.nextInt(); col++; } else { row++; col...
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...
Write a method with the following header: public static int getValidInput(int low, int high, String message)...
Write a method with the following header: public static int getValidInput(int low, int high, String message) This method will return a user entered number between high and low inclusive. If a number is entered that is not between high and low, string message will be printed to the screen. If high is less than low, a -1 is returned. For example, given this call: int input = getValidInput(0, 100, “Not a valid grade, Please re- enter”); The following could occur:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT