Question

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"

Homework Answers

Answer #1
// TestCode.java
public class TestCode {
    public static String rightN(String s, int n){
        return s.substring(s.length()-n)+s.substring(0,s.length()-n);
    }
    public static void main(String args[])
    {
        System.out.println(rightN("hello",2));
        System.out.println(rightN("Chocolate",3));
        System.out.println(rightN("Chocolate",1));
    }
}

public String rightN(String s, int n){
    return s.substring(s.length()-n)+s.substring(0,s.length()-n);
}
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
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); }...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 you have the following code what edits can you do #include <stdio.h> #include <string.h> void StringChallenge(char str[]) {      // code goes...
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows: public static int countNumAlphabetic ( String str ) { } Sample Output: countNumAlphabetic("mA13Th9zB86") returns 8
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
in java pls Write a method that will receive 2 numbers as parameters and will return...
in java pls Write a method that will receive 2 numbers as parameters and will return a string containing the pattern based on those two numbers. Take into consideration that the numbers may not be in the right order. Note that the method will not print the pattern but return the string containing it instead. The main method (already coded) will be printing the string returned by the method. Remember that you can declare an empty string variable and concatenate...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Write a function int count_consonants(char str[]) that determines and returns the number of consonants in a...
Write a function int count_consonants(char str[]) that determines and returns the number of consonants in a given string. Then write a simple main() function where you can repeatedly enter a string and then the num- ber of consonants is determined and printed on the screen (from the main() function). If the entered string is empty (it will contain only a ’\n’ then) the program should stop its execution. You can safely assume that the entered string will be not longer...