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.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)?
What is returned by the call: ben(51) ? public static String ben(int x) { if(x /...
What is returned by the call: ben(51) ? public static String ben(int x) { if(x / 5 <= 0) return x % 5; else return (x % 5) + ben(x / 5); }
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Java Write the method rightN. The method has two inputs: a String str and an int...
Java Write the method rightN. The method has two inputs: a String str and an int n. The output of the method is a new String that contains a "rotated right n" version of str. You can assume that the length of str will be at least n. Here is some sample input and output: rightN("Hello", 2) → "loHel" rightN("Chocolate", 3) → "ateChocol " rightN("Chocolate", 1) → "eChocolat"
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT