Java Programming: Problem 2 (name this Lab11_Problem2 ) Write a main() method th
ID: 3767550 • Letter: J
Question
Java Programming:
Problem 2 (name this Lab11_Problem2)
Write a main() method that creates and populates an array, using the native order values shown below, then allows the user to either left or right shift the values.
See the sample output and compare it to the native order so you understand what left and right shifting does.
Write a method that will display the array along with an appropriate caption. Hint: pass the caption as an argument. The possible captions are: (Native Order, Left Shifted, and Right Shifted followed by a colon, space and the array contents.
Display the native order:
Native Order: 47 93 108 173 4 38 62 87 19 11
In main() prompt the user to enter a choice of performing either a left shift or a right shift on the array, or provides the option to quit. Continue until the user chooses to quit.
If the user chooses L, then call a method to left shifts the values. Name it fvLeftShift.
If the user types R, then call a method to right shift the values. Name it fvRightShift.
If the user types Q, quit the program.
For any other value, display "Error: invalid menu choice."
Don't display anything in the two shift methods (left and right).
In main() display the shifted order, then repeat the menu.
Some sample input/output dialog:
Native Order: 47 93 108 173 4 38 62 87 19 11
Enter L for left shift, R for right, Q to quit: X
Error: type L, R, or Q
Enter L for left shift, R for right, Q to quit: L
For a left-shift choice, display this:
Left Shifted: 93 108 173 4 38 62 87 19 11 47
For a right-shift, display this:
Right Shifted: 11 47 93 108 173 4 38 62 87 19
Note: Each time you repeat the menu with an L or R choice, the array should be based on the previous order; the native order will be lost once you perform the shift operation. You'd have to cycle through it 10 times to come back to the original order. Make sure it does this.
Explanation / Answer
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class ShiftArray {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner;
try {
System.out.print("Native Order: ");
scanner = new Scanner(System.in);
int a[] = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = scanner.nextInt();
}
int resL[] = a;
int resR[] = a;
do {
System.out
.print("Enter L for left shift, R for right, Q to quit: ");
String choice = scanner.next();
if (choice.equals("Q")) {
break;
} else if (choice.equals("L")) {
resL = shiftL(resL);
printArr("Left Shifted: ", resL);
} else if (choice.equals("R")) {
resR = shiftR(resR);
printArr("Right Shifted: ", resR);
} else {
System.out.println("Error: type L, R, or Q");
}
} while (true);
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* @param a
* @return
*/
public static int[] shiftL(int a[]) {
int res[] = new int[a.length];
res[a.length - 1] = a[0];
for (int i = 0; i < a.length - 1; i++) {
res[i] = a[i + 1];
}
return res;
}
/**
* @param a
* @return
*/
public static int[] shiftR(int a[]) {
int res[] = new int[a.length];
res[0] = a[a.length - 1];
for (int i = 1; i < a.length; i++) {
res[i] = a[i - 1];
}
return res;
}
/**
* @param message
* @param a
*/
public static void printArr(String message, int a[]) {
System.out.print(message);
for (int i = 0; i < a.length; i++) {
System.out.print(" " + a[i]);
}
System.out.println();
}
}
OUTPUT:
Native Order: 1 2 3 4 5 6 7 8 9 0
Enter L for left shift, R for right, Q to quit: E
Error: type L, R, or Q
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 2 3 4 5 6 7 8 9 0 1
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 3 4 5 6 7 8 9 0 1 2
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 4 5 6 7 8 9 0 1 2 3
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 5 6 7 8 9 0 1 2 3 4
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 6 7 8 9 0 1 2 3 4 5
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 7 8 9 0 1 2 3 4 5 6
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 8 9 0 1 2 3 4 5 6 7
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 9 0 1 2 3 4 5 6 7 8
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 0 1 2 3 4 5 6 7 8 9
Enter L for left shift, R for right, Q to quit: L
Left Shifted: 1 2 3 4 5 6 7 8 9 0
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 0 1 2 3 4 5 6 7 8 9
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 9 0 1 2 3 4 5 6 7 8
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 8 9 0 1 2 3 4 5 6 7
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 7 8 9 0 1 2 3 4 5 6
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 6 7 8 9 0 1 2 3 4 5
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 5 6 7 8 9 0 1 2 3 4
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 4 5 6 7 8 9 0 1 2 3
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 3 4 5 6 7 8 9 0 1 2
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 2 3 4 5 6 7 8 9 0 1
Enter L for left shift, R for right, Q to quit: R
Right Shifted: 1 2 3 4 5 6 7 8 9 0
Enter L for left shift, R for right, Q to quit: Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.