Problem 2 - Java Implement the swapNext method in the Problem02 class. Its purpo
ID: 3777182 • Letter: P
Question
Problem 2 - Java
Implement the swapNext method in the Problem02 class. Its purpose is to swap the elements at indices i and i + 1. For example, when i is 3, it should swap the elements at indices 3 and 4.
The expected output of this program when swapNext is correctly implemented is:
Implement the swapNext method in the Problem02 class. Its purpose is to swap the elements at indices i and i + 1. For example, when i is 3, it should swap the elements at indices 3 and 4. The expected output of this program when swapNext is correctly implemented is: 0: e 1:a 2:b 3:d 4:c There is nothing for you to do in Problem 3. Continue to Problem 4.Explanation / Answer
Problem02.java
public class Problem02 {
public static void swapNext(char a[], int i){
char temp;
temp = a[i+1];
a[i+1] = a[i];
a[i]=temp;
}
public static void main(String[] args) {
int[] indices = new int[]{3,2,3,1,0};
char a[] = new char[]{'a','b','c','d','e'};
for(int i: indices){
Problem02.swapNext(a, i);
}
for(int i=0; i<a.length; i++){
System.out.println(i + ":"+a[i]);
}
}
}
Output:
0:e
1:a
2:b
3:d
4:c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.