review how to perform some of the most fundamental tasks when dealing with arrays. Some
Question:
review how to perform some of the most fundamental tasks when dealing with arrays.
Some code at the bootom. (Take a look)
Program that implements the following menu to interact with the user and manipulate an array of integers:
1. Size the array 2. Fill the array with random (and possibly duplicate) values 3. Print the array 4. Swap array elements 5. Search the array for a value 6. Count occurrences of a value in the array 7. Scramble the array 8. Fill the array with unique (no duplicates) random values 9. Custom feature of your own 10. Quit
In your solution, use built-in Java operators and methods to get the job done. Make sure that your program loops until you select the option to quit.
Tip: Public variables are okay. They would start with "public static" and come before your main() method.
Here's what each option should do:
Size the array
Ask the user for a positive integer size value and make the array that size. If the array has already been sized, using this command should make a new, empty array; that is, the values in the previous array will be lost.
Fill the array with random (and possibly duplicate) values
Set each element in the array to a random value from 1 to the size of the array. It is OK for the same number to occur more than once.
Print the array
Print out the array elements, in the order they are stored, on a single line with a space between each value.
Swap array elements
Swap one array element with another. (Be careful not to overwrite one of the two elements you are swapping in the process.
Search the array for a value
Get an integer value from the user and print out the first array index where that value is found in the array. Print out -1 if the value is not found.
Count occurrences of a value in the array
Get an integer value from the user and print out how many times that value is found in the array. Print 0 is the value is not found.
Scramble the array
Rearrange the element values in the array so that they are not sorted.
Fill the array with unique (no duplicate) random values
Set the elements in the array to new values from 1 to the size of the array, with no value being duplicated. (HINT: There are many ways to do this, but all you need to ensure is that the array contains all the values from 1 to the size of the array in a non-sorted order.)
Custom feature of your own
How else could you manipulate the data? Come up with a suggestion of your own and implement it in code.
When you are finished
Show your teacher your work and get the OK to continue. Ms. Wiebe will even mark it on the spot or give you hints on how to get certain functions working.
This what I have but it does not work. (Could you help me Please)
importjava.util.Scanner;
public classArrayProgram {
public static voidmain(String[] args) {
String menu ="Enter the numbers to perform the corresponding operation "
+"1. Size the array "
+"2. Fill the array with random (and possibly duplicate) values "
+"3. Print the array "
+"4. Swap array elements "
+"5. Search the array for a value "
+"6. Count occurrences of a value in the array "
+"7. Scramble the array "
+"8. Fill the array with unique (no duplicates) random values "
+"9. Custom feature of your own "
+"10. Quit";
Scanner sc =newScanner(System.in);
int[] arr =null;
do{
System.out.println(menu);
String input = sc.next();
switch(input) {
case"1":intn = sc.nextInt();
arr =new int[n];
break;
case"2":for(inti =0;i < arr.length;i++) {
arr[i] = (int)Math.floor(Math.random()*(arr.length)+1);
}
break;
case"3":for(inti: arr) {
System.out.print(i +" ");
}
break;
case"4":inttemp = arr[0];
arr[0] = arr[arr.length-1];
arr[arr.length-1] = temp;
break;
case"5":intvalue = sc.nextInt();
booleanfound =false;
for(inti =0;i < arr.length;i++) {
if(arr[i] == value) {
System.out.println(i);
found =true;
break;
}
}
if(!found)
System.out.println(-1);
break;
case"6":intv = sc.nextInt(),c =0;
for(inti: arr) {
if(i == v) {
c++;
}
}
System.out.println(c);
break;
case"7":for(inti =0;i < arr.length-1;i+=2) {
temp = arr[i];
arr[i] = arr[i +1];
arr[i +1] = temp;
}
break;
case"8":intx =1;
for(inti =0;i <= arr.length/2;i++,x++) {
arr[i] = x;
if(i != (arr.length-1- i))
arr[arr.length-1- i] = ++x;
}
break;
case"9":intsum =0;
for(inti =0;i < arr.length;i++) {
sum+= arr[i];
System.out.print(sum +" ");
}
break;
case"10":return;
default: System.out.println("Invalid option");
continue;
}
}
while(true);
}
}
International Marketing And Export Management
ISBN: 9781292016924
8th Edition
Authors: Gerald Albaum , Alexander Josiassen , Edwin Duerr