Question

In: Computer Science

we are looking at the "changeSpecificIndex" method. i was able to replace the value of that...

we are looking at the "changeSpecificIndex" method.

i was able to replace the value of that specific index, but im stumped at adding a value to a specific index AND shifting the values after it to the next index.

Here is my code

_____________________________________________

import java.util.Scanner;
import java.util.Arrays;

public class ArrayMenu{
static int count;
static Scanner kb = new Scanner(System.in);
  
  
  
  
public static void main(){
int item=0;
int[] numArray=new int[100];
count=0;
  
  
while (item !=8){
menu();
item = kb.nextInt();
if (item==1)
initializeArray(numArray);
else if (item==2)
printArray(numArray);
else if (item==3)
addToEnd(numArray);
else if (item == 4)
changeSpecificIndex(numArray);
/* else if (item == 5)
removeElement(numArray);
*/ else if (item == 6)
minMaxSumAvg(numArray);
/* else if (item == 7)
Search(numArray);
*/
}
  
System.out.println("Goodby!");
  
}

public static void menu(){
System.out.println("1. Initialize Array");
System.out.println("2. Display Array");
System.out.println("3. Add element to the end");
System.out.println("4. Add an element at a specific index");
System.out.println("5. Remove an element at specific index");
System.out.println("6. Show min, max, sum and average");
System.out.println("7. Search");
System.out.println("8. Exit");
System.out.println("________________________________________");
}
  
public static void initializeArray(int[] arr){
count=0;
int num;
System.out.print("Enter integer values to fill the array -value to stop: ");
do
{
num = kb.nextInt();
if (num >=0)
{
arr[count]=num;
count++;
}
} while (num > 0);
  
}
public static void printArray(int[] arr){
for (int i=0; i< count; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
  
public static void addToEnd(int[] arr){
System.out.print("Enter number to add: ");
int num = kb.nextInt();
arr[count] = num;
count++;
System.out.println();

}
  
public static void minMaxSumAvg(int[] arr){
  
double max = arr[0];
double min = arr[0];
double sum = 0;
double avg = 0;
  
for(int i = 1; i < count; i++){
  
int numby = arr[i];
  
if(numby > max){
max = numby;

}
  
if(numby < min){
min = numby;
}
  
sum = sum + arr[i];
}
  
avg = sum / count;
  
  
System.out.println("Sum is: " + sum);
System.out.println("Average is: " + avg);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);

}
  
  
public static void changeSpecificIndex(int[] arr){
int arrIndex;
int newValue;
  
System.out.println("What index would you like to change: ");
arrIndex = kb.nextInt() - 1;
System.out.println("Assign new value: ");
newValue = kb.nextInt();
  
arr[arrIndex] = newValue;
  
System.out.println("The value of Index " + (arrIndex + 1) + " has been changed to " + newValue);

  

  
}
  
  
  
  
  
  
}

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Arrays;

public class ArrayMenu {
        static int count;
        static Scanner kb = new Scanner(System.in);

        public static void main(String args[]) {
                int item = 0;
                int[] numArray = new int[100];
                count = 0;

                while (item != 8) {
                        menu();
                        item = kb.nextInt();
                        if (item == 1)
                                initializeArray(numArray);
                        else if (item == 2)
                                printArray(numArray);
                        else if (item == 3)
                                addToEnd(numArray);
                        else if (item == 4)
                                changeSpecificIndex(numArray);
                        /*
                         * else if (item == 5) removeElement(numArray);
                         */ else if (item == 6)
                                minMaxSumAvg(numArray);
                        /*
                         * else if (item == 7) Search(numArray);
                         */
                }

                System.out.println("Goodby!");

        }

        public static void menu() {
                System.out.println("1. Initialize Array");
                System.out.println("2. Display Array");
                System.out.println("3. Add element to the end");
                System.out.println("4. Add an element at a specific index");
                System.out.println("5. Remove an element at specific index");
                System.out.println("6. Show min, max, sum and average");
                System.out.println("7. Search");
                System.out.println("8. Exit");
                System.out.println("________________________________________");
        }

        public static void initializeArray(int[] arr) {
                count = 0;
                int num;
                System.out.print("Enter integer values to fill the array -value to stop: ");
                do {
                        num = kb.nextInt();
                        if (num >= 0) {
                                arr[count] = num;
                                count++;
                        }
                } while (num > 0);

        }

        public static void printArray(int[] arr) {
                for (int i = 0; i < count; i++)
                        System.out.print(arr[i] + " ");
                System.out.println();
        }

        public static void addToEnd(int[] arr) {
                System.out.print("Enter number to add: ");
                int num = kb.nextInt();
                arr[count] = num;
                count++;
                System.out.println();

        }

        public static void minMaxSumAvg(int[] arr) {

                double max = arr[0];
                double min = arr[0];
                double sum = 0;
                double avg = 0;

                for (int i = 1; i < count; i++) {

                        int numby = arr[i];

                        if (numby > max) {
                                max = numby;

                        }

                        if (numby < min) {
                                min = numby;
                        }

                        sum = sum + arr[i];
                }

                avg = sum / count;

                System.out.println("Sum is: " + sum);
                System.out.println("Average is: " + avg);
                System.out.println("Max is: " + max);
                System.out.println("Min is: " + min);

        }

        public static void changeSpecificIndex(int[] arr) {
                int arrIndex;
                int newValue;

                System.out.println("What index would you like to change: ");
                arrIndex = kb.nextInt() - 1;
                System.out.println("Assign new value: ");
                newValue = kb.nextInt();
                
                if(arrIndex > count) {
                        System.out.println("Invalid Index entered. Valid indices are from 0 to " + count);
                } else {
                        
                        for(int i=count; i>arrIndex; i--) {
                                arr[i] = arr[i-1];
                        }
                        arr[arrIndex] = newValue;

                        System.out.println("The value of Index " + (arrIndex ) + 
                                        " has been changed to " + newValue);
                        
                        count++;
                }


        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

C++ Objective To wrap up this week, we will be looking at the Call by Value...
C++ Objective To wrap up this week, we will be looking at the Call by Value mechanism, and how it differs from Call by Reference; whether it is how they work in memory or how they help you accomplish a practical task, knowing the differences between these two mechanisms is important foundational knowledge. **You should have one main function + 4 other functions for your submission for this lab. you have no need for extra functions beyond that, and you...
How is it that we are able to hear sounds? Why are we able to hear...
How is it that we are able to hear sounds? Why are we able to hear different pitches of a particular frequency ( e.g., an ambulance approaches you then recedes from you)?
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
A company is looking into two types of compressors to replace an aging compressor. The data...
A company is looking into two types of compressors to replace an aging compressor. The data in the table below has been compiled for comparison Centrifugal Compressor Reciprocating Compressor Installed Cost $2,000,000 $850,000 Annual O&M costs $50,000 $150,000 Service Life in years 15 8 Salvage Value $80,000 $60,000 If the firm's MARR is known to be 16%, which option will you recommend?
A hotel is looking to replace its outdated alarm system to comply with safety standards. It...
A hotel is looking to replace its outdated alarm system to comply with safety standards. It has two options: System A costs $35,000 and lasts nine years. System B costs $22,000 and lasts six years. Maintenance throughout the year will cost $2,000 for System A and $3000 for System B, recognized at the end of each year. The hotel’s cost of capital is 10%. Which alarm system should the hotel choose? (Please Show work and explain answer)
The CSI Corporation is looking to replace an existing printing press with one of two newer...
The CSI Corporation is looking to replace an existing printing press with one of two newer models that are more efficient. The current press is three years old, cost 32,000 and is being depreciated under MACRS using a 5-year recovery period. The first alternative under consideration, Printing Press A, cost $40,000 to purchase and $8,000 to install. It has a 5 year usable life and will be depreciated under MACRS using a 5-year recovery period. The second alternative, press B...
The Fox Corporation is looking to replace an existing printing press with one of two newer...
The Fox Corporation is looking to replace an existing printing press with one of two newer models that are more efficient. The current press is three years old, cost 32,000 and is being depreciated under MACRS using a 5-year recovery period. The first alternative under consideration, Printing Press A, cost $40,000 to purchase and $8,000 to install. It has a 5 year usable life and will be depreciated under MACRS using a 5-year recovery period. The second alternative, press B...
The CSI Corporation is looking to replace an existing printing press with one of two newer...
The CSI Corporation is looking to replace an existing printing press with one of two newer models that are more efficient. The current press is three years old, cost 32,000 and is being depreciated under MACRS using a 5-year recovery period. The first alternative under consideration, Printing Press A, cost $40,000 to purchase and $8,000 to install. It has a 5 year usable life and will be depreciated under MACRS using a 5-year recovery period. The second alternative, press B...
The Fox Corporation is looking to replace an existing printing press with one of two newer...
The Fox Corporation is looking to replace an existing printing press with one of two newer models that are more efficient. The current press is three years old, cost 32,000 and is being depreciated under MACRS using a 5-year recovery period. The first alternative under consideration, Printing Press A, cost $40,000 to purchase and $8,000 to install. It has a 5 year usable life and will be depreciated under MACRS using a 5-year recovery period. The second alternative, press B...
The Rauzi Company is looking to replace an existing computer system with a new, more efficient...
The Rauzi Company is looking to replace an existing computer system with a new, more efficient system. They feel the new will increase cash flow. The new system will cost $435,000 today and have a 4 year production life. It will be depreciated using 100% bonus depreciation with an estimated salvage value of $175,000 at the end of its production life. The project will also set aside $15,000 for working capital. The old computer is currently two years old and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT