In: Computer Science
// SET5. Write a Java statement that changes the month of the first object of this array to be a copy of the month
// of the last object of the array. (Assign the month of the last object to the month of the first object).
// SET6. Write a segment of code to swap the first and the second objects in this array. Declare any additional
// variable you might need.
Program Code [JAVA]
import java.util.Arrays;
public class Statement {
public static void main(String[] args) {
// SET 5
// First we need to have an array of months
String[] months = {"Jan", "Feb", "March", "April",
"May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
// Showing array before changing first object with last object
System.out.println("**** SET 5 ****");
System.out.println("\nBefore copying last object in first
object");
System.out.println(Arrays.toString(months));
// Now Copying - set5
months[0] = months[months.length-1]; // answer of set5
System.out.println("\nAfter copying last object in first
object");
System.out.println(Arrays.toString(months));
// Swapping first and second objects
String temp = months[0]; // answer of set6
months[0] = months[1];
months[1] = temp;
System.out.println("\n**** SET 6 ****");
System.out.println("\nAfter swapping the first and second
objects");
System.out.println(Arrays.toString(months));
}
}
Sample Output:-
----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!