Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STEP 1 Run a Project in a Java IDE IN GRASP or Similar software complete the fol

ID: 3911105 • Letter: S

Question

STEP 1                        Run a Project in a Java IDE

IN GRASP or Similar software complete the following

class ArrayDemo {

     public static void main(String[] args) {

          int[] anArray;            // declares an array of integers

          anArray = new int[10];    // allocates memory for 10 integers

           

          anArray[0] = 100; // initialize first element

          anArray[1] = 200; // initialize second element

          anArray[2] = 300; // etc.

          anArray[3] = 400;

          anArray[4] = 500;

          anArray[5] = 600;

          anArray[6] = 700;

          anArray[7] = 800;

          anArray[8] = 900;

          anArray[9] = 1000;

          System.out.println("Element at index 0: " + anArray[0]);

          System.out.println("Element at index 1: " + anArray[1]);

          System.out.println("Element at index 2: " + anArray[2]);

          System.out.println("Element at index 3: " + anArray[3]);

          System.out.println("Element at index 4: " + anArray[4]);

          System.out.println("Element at index 5: " + anArray[5]);

          System.out.println("Element at index 6: " + anArray[6]);

          System.out.println("Element at index 7: " + anArray[7]);

          System.out.println("Element at index 8: " + anArray[8]);

          System.out.println("Element at index 9: " + anArray[9]);

     }

}

            Modify your code in Project 1 to demonstrate how to clone or copy a Java array. Starter          code follows.

Steps to Complete this Project

STEP 1                        Run a Project in a Java IDE

The Java System class has an arraycopy method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object src,

                             int srcPos,

                             Object dest,

                             int destPos,

                             int length)

The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

Use the following code snippet declaring an array of char elements, spelling the word "decaffeinated". Note the code uses arraycopy to copy a subsequence of array components into a second array:

    public static void main(String[] args) {

        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',

                      'i', 'n', 'a', 't', 'e', 'd' };

        char[] copyTo = new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);

        System.out.println(new String(copyTo));

    }

Run your program and test your output to spell out the word caffeine.  

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

ArrayDemo.java
----------
class ArrayDemo {

public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',

'i', 'n', 'a', 't', 'e', 'd' };

char[] copyTo = new char[7];

System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));

  
int[] anArray; // declares an array of integers
anArray = new int[10]; // allocates memory for 10 integers

anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("------------------------");
System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
System.out.println("------------------------");
int[] copyNums = new int[10];
System.arraycopy(anArray, 0, copyNums,0, 10);
System.out.println(" The copied array contains");
System.out.println("Element at index 0: " + copyNums[0]);
System.out.println("Element at index 1: " + copyNums[1]);
System.out.println("Element at index 2: " + copyNums[2]);
System.out.println("Element at index 3: " + copyNums[3]);
System.out.println("Element at index 4: " + copyNums[4]);
System.out.println("Element at index 5: " + copyNums[5]);
System.out.println("Element at index 6: " + copyNums[6]);
System.out.println("Element at index 7: " + copyNums[7]);
System.out.println("Element at index 8: " + copyNums[8]);
System.out.println("Element at index 9: " + copyNums[9]);

  
}

}

output
-----
caffein
------------------------
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
------------------------

The copied array contains
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000