Write a java program that reads 20 integer values within the array. For this pro
ID: 3671031 • Letter: W
Question
Write a java program that reads 20 integer values within the array. For this program, there will be only one array. In your main function, define a constant MAXSIZE for an array. Initialize the MAXSIZE to 20. Initialize the array to contain the values from 0 to MAXSIZE -1, in array positions 0 to MAXSIZE -1, i.e., array position 3 has the value 3. A user then selects a transformation to apply to the array from a menu: square halve accumulate transpose shift right reverse quit program void square(int all) - Square every value in the array. void halve(int a[j) - Halve every value in the array (rounding down), for example if an element in the array is equal to 3 after calling function halve the element will be 1. void accumulate (int a[J) sum all elements of the array. void transpose(int a(]) - For every pair of neighbors in the array, switch their values. That is, switch the value of a[0] with that of a[1], and a[2] with a[3], etc. void shiftRight(int a[]) - Move every value in the array to the next index position on the right side, e.g., the value of a[3] is moved to a[4]. Except a[19] move the value of the last index to the first index, e.g., a[19] is moved to a[0] for an array of size 20. void reversetint a[]) - Reverse the values in the array, e.g., in an array of size 20 switch a[0] with a[19], a[1) with a[18l, etc.|Explanation / Answer
import java.util.Scanner;
public class ArrayTest {
public static void main(String args[]){
final MAXSIZE=20;
Scanner in = new Scanner(System.in);
for (i=0;i<20;i++)
{
System.out.println("Enter array element"+i);
int a[i]= in.nextInt();
}
System.out.println("Enter Integer for appropriate operations as below");
System.out.println("1 For square");
System.out.println("2 For Halve");
System.out.println("3 For acculamate");
System.out.println("4 For transpose ");
System.out.println("5 for ShiftRight");
System.out.println("6 For reverse");
System.out.println("7 For quit");
int o= in.nextInt();
switch(o)
{
case 1:
for(i=0;i<20;i++
{
a[i]=a[i]*a[i];
}
System.out.println("the square is");
for (i=0;i<20;i++)
{
System.out.print(a[i]);
}
break;
case 2:
int j=0;
if(MAXSIZE%2 ==0){
for(i=0; i< MAXSIZE; i=i+2)
{
a[i]=j++;
}
if(MAXSIZE %2 !=0){
for(i=1; j< MAXSIZE; j=j+2)
{
a[i]=j++;
}
System.out.println("The Halve Array is");
for (i=0;i<20;i++){
System.out.print(a[i]);
}//prints 0 0 1 1 2 2 3 3 4 4 ……up to 20 values
case 3 :
int sum=0;
for(i=0;i<20;i++)
{
sum=sum+a[i];
}
System.out.println("The sum of all elements in the array is"+sum);
break;
case 4 :
if(MAXSIZE%2 ==0){
for(int i=0; i< MAXSIZE; i=i+2){
a[i]=a[i+1];
a[i+1]=a[i];
int[] b={a[i]+a[i+1]};
}
}
if(MAXSIZE %2 !=0){
for(int j=0; j< MAXSIZE; j=j+2){
a[j]=a[j+1];
a[j+1]=a[j];
a[len-1]=a[len-1];
int[] b={a[j]+a[j+1]+a[MAXSIZE -1]};
}
System.out.println("The Swapping Array is");
for (i=0;i<20;i++){
System.out.print(a[i]);
}
case 5 :
int last = a[a.length-1]; // save off first element
// shift right
for( int index =a.length-2; index >= 0 ; index-- )
a[index+1] = a[index];
// wrap last element into first slot
a[0] = last
System.out.println("The ShiftRight array is");
for (i=0;i<20;i++){
System.out.print(a[i]);
}
break;
case 6:
for ( i = 0; i < a.length / 2; i++)
{
int temp = a[i];
// swap numbers
input[i] = input[(a.length – 1) - i];
input[(a.length – 1) - i] = temp;
}
System.out.println("The reverse array is");
for (i=0;i<20;i++){
System.out.print(a[i]);
}
break;
case 7:
System.exit(0);
break;
default :
System.out.println("Invalid option");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.