Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that accepts three command line arguments: - First, a Strin

ID: 3664656 • Letter: W

Question

Write a Java program that accepts three command line arguments:

- First, a String, str

- Second, an integer corresponding to the position of a letter in str. Parse this into an int position1.

- Third, an integer corresponding to the position of a later letter in str. Parse this into an int position2.

      For example, the position of the letter 3 in the String 123456789 is 3. [Hint: The so-called 'index' of each letter in a String is 1 smaller than its position.]

      Now your program should use methods in the String() class to transpose the position1 and position2 letters, and print the original and modified Strings at the command line in the following format (the java example is given in the first line:

            bash$ java SwapLetters "123456789" 2 8

            Original String: 123456789; Swap Letters 2 and 8; New String: 183456729

Note: There are definitely multiple ways to do this.

Write a Java class TestSwapLetters.java with a main method to test each of these programs. Use TestProgram.java from this exercise as a template – just follow the same exact approach, but change the names and test inputs to suit this program. Note that Demo.java has only one command line input, where SwapLetters has 3. Use the following values to test your programs:

SwapLetters: Test1: "123456789" 2 8; Test2: "54325674" 5 3; Test3: "2345643" 1 7; Test4: "7654993" 4 5

Explanation / Answer

public class TestSwapLetters {
   public static void main(String[ ] args) {
       
       int number1 = Integer.parseInt(args[1]);
       int number2 = Integer.parseInt(args[2]);
     
       String firstStr = Integer.toString(number1);
     
       String secondStr = Integer.toString(number2);
     
       String str = args[0];
     
   String replaced = str.replace(firstStr, secondStr);
     System.out.println("Replacing character in String");
     System.out.println("Original String before replace : " + str);
     System.out.println("Replaced String : " + replaced);
      
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote