=========================================================== ****** Please use Ja
ID: 3768463 • Letter: #
Question
===========================================================
****** Please use Java to write the code *******
===========================================================
/*
* (name header)
*/
import java.util.Scanner;
public class ArrayMethods
{
public static final int CAPACITY = 20;
public static final int MAX = 10;
public static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
int myArray[] = new int[CAPACITY];
int numOfElements = processArray(myArray);
char again = 'y';
do
{
askToInsert(myArray, numOfElements);
++numOfElements;
printArray(myArray, numOfElements);
int numberToRemove = askToRemove();
int idx = search(myArray, numOfElements, numberToRemove);
if (idx >= 0)
{
remove(myArray, numOfElements, idx);
--numOfElements;
}
else
{
System.out.println("Number not found.");
}
printArray(myArray, numOfElements);
System.out.print(" Would you like to repeat the process? (y/n) ");
again = input.next().charAt(0);
System.out.println();
}
while (again == 'y');
}
// Method processArray
// Your code here...
// Method askToInsert
// Your code here...
// Method insert
// Your code here...
// Method askToRemove
// Your code here...
// Method search
// Your code here...
// Method remove
// Your code here...
// Method printArray
// Your code here...
}
Exercise 22: Array Methods Folder name: A170 E22 YourLastName YourFirstName For this exercise, you need to implement seven (7) methods using the ex_ 23_array_methods project The project contains a main method that declares an array of capacity 20 and calls the methods you need to implement. Do NOT modify the main method. These are the methods you need to implement: Method processArray o Parameter: An array of integers. o Return type: An integer. o Asks the user to enter positive integers, specifying not to enter more than the maximum allowed (this is set by the constant MAX) and when to quit (see output below for details) Uses a WHILE loop to read the integers entered by the user. You need to keep track of how many numbers the user enters so that you can return the number of elements in the array. o o Method askTolnsert o Parameters: An array of integers and an integer that stores the number of elements in the array. o This is a void method. o Asks the user which number to add to the array and in which index. o Calls the method insert . Method insert Parameters: An array of integers, an integer that stores the number of elements in the array, an integer that stores the number to insert, and an integer that stores the index where the number will be inserted o o This is a void method. o Inserts the given number at the given index. o Do not worry about increasing the number of elements. This is already implemented in the main method when the askTolnsert method is called. Method askToRemove o No parameters. o Return type: An integer that stores the number to remove. o Asks the user which number to remove from the array. . Method search Parameters: An array of integers, an integer that stores the number of elements in the arra and the integer to search for. Return type: An integer that stores the index where the number is in the array, or -1 if the number is not found. Uses a WHILE loop to find the element. The while loop should STOP when the element is found or when the search has reached the end of the array. o o o Method removeExplanation / Answer
import java.util.Scanner;
public class ArrayMethods
{
public static final int CAPACITY = 20;
public static final int MAX = 10;
public static Scanner input = new Scanner(System.in);
static int processArray(int array[])
{
int count=0;
System.out.println("Enter up to "+MAX+"positive integers");
System.out.println("Enter -1 to exit");
int i=input.nextInt();
while(i!=-1)
{
array[count]=i;
count++;
i=input.nextInt();
}
return count;
}
static void askToInsert(int myArray[], int numOfElements)
{
System.out.println("Which number you want to add to array?");
int number=input.nextInt();
System.out.println("At which index");
int index=input.nextInt();
insert(myArray, numOfElements,number,index);
}
static void insert(int myArray[], int numOfElements,int number,int index)
{
for(int i=numOfElements-1;i>=index;i--)
{
myArray[i+1]=myArray[i];
}
myArray[index]=number;
}
static int askToRemove()
{
System.out.println("Which number would you like to remove");
int number_remove=input.nextInt();
return number_remove;
}
static int search(int myArray[],int numOfElements,int number_remove)
{
int count=0;
int res=0;
for(int i=0;i<numOfElements;i++)
{
if(myArray[i]==number_remove)
res= i;
else count++;
}
if(count==numOfElements)
res= -1;
return res;
}
static void remove(int myArray[], int numOfElements,int idx)
{
for(int i=idx;i<numOfElements;i++)
{
myArray[i]=myArray[i+1];
}
}
static void printArray(int myArray[], int numOfElements)
{ for(int i=0;i<numOfElements;i++)
{if(numOfElements==0)
System.out.println("Array is empty");
else
System.out.print(myArray[i]+" ");
}
}
public static void main(String[] args)
{
int myArray[] = new int[CAPACITY];
int numOfElements = processArray(myArray);
char again = 'y';
do
{
askToInsert(myArray, numOfElements);
++numOfElements;
printArray(myArray, numOfElements);
int numberToRemove = askToRemove();
int idx = search(myArray, numOfElements, numberToRemove);
if (idx >= 0)
{
remove(myArray, numOfElements, idx);
--numOfElements;
}
else
{
System.out.println("Number not found.");
}
printArray(myArray, numOfElements);
System.out.print(" Would you like to repeat the process? (y/n) ");
again = input.next().charAt(0);
System.out.println();
}
while (again == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.