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

Part 1 Write a program that reads in 5 integers and stores them in a 5 element a

ID: 3682118 • Letter: P

Question

Part 1

Write a program that reads in 5 integers and stores them in a 5 element array. Print the size of the array using the “.length” data field. Create a second 5 element array and copy the data from the first array to the second one using a for-loop. Finally, print out the contents of the second array using a for-loop.

Part 2

Write a method with the following header:

public static int[] doubleCapacity (int[] list).

Put the doubleCapacity method and the main method in one class. The purpose of the main method is to test your doubleCapacity method. Design your own main method—the minimal requirements are:

Create an input array of your choice.

Display the contents of the input array.

Invoke the doubleCapacity method to create a new double-sized array.

Display the contents of the new array.

Implement another method to display the contents of an array and invoke this method for Steps 2 and 4.

Explanation / Answer

Part1

ArrayTest1.java

public class ArrayTest1 {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       java.util.Scanner in = new java.util.Scanner(System.in);
       int n=5;
       int values1[] = new int[n];
       for(int i=0; i<n; i++){
       System.out.println("Please Enter Integer value: ");
       values1[i] = in.nextInt();
       }
       System.out.println("First array size ["+values1.length+"]");
       int values2[] = new int[n];
       for(int i=0; i<n; i++){
           values2[i] = values1[i];
       }
       System.out.println("Second Array size ["+values2.length+"]");
       for(int i=0; i<n; i++)
           System.out.println("Second array element ["+i+"] : "+values2[i]);
   }

}

Output:

Please Enter Integer value:
1
Please Enter Integer value:
2
Please Enter Integer value:
3
Please Enter Integer value:
4
Please Enter Integer value:
5
First array size [5]
Second Array size [5]
Second array element [0] : 1
Second array element [1] : 2
Second array element [2] : 3
Second array element [3] : 4
Second array element [4] : 5

Test3.java

public class Test3 {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       java.util.Scanner in = new java.util.Scanner(System.in);
       int n=5;
       int a[] = new int[n];
       for(int i=0; i<n; i++){
       System.out.println("Please Enter Integer value: ");
       a[i] = in.nextInt();
       }
       System.out.println("Array Size ["+a.length+"]");
       for(int i=0; i<a.length; i++){
           System.out.println("First Array Element ["+i+"] : "+a[i]);
       }
       int b[] = doubleCapacity(a);
       displayArrayElements(a,b);
   }
   public static int[] doubleCapacity (int[] list){
       int b[] = new int[2 * list.length];
       java.util.Scanner in = new java.util.Scanner(System.in);
       for(int i=0; i<b.length; i++){
       System.out.println("Please Enter Integer value: ");
       b[i] = in.nextInt();
       }  
       for(int i=0; i<b.length; i++){
           System.out.println("Second Array Element ["+i+"] : "+b[i]);
       }
       return b;
   }
   public static void displayArrayElements (int[] list1, int[] list2){
       System.out.println("First Array Size ["+list1.length+"]");
       for(int i=0; i<list1.length; i++){
           System.out.println("First Array Element ["+i+"] : "+list1[i]);
       }
       System.out.println("Second Array Size ["+list2.length+"]");
       for(int i=0; i<list2.length; i++){
           System.out.println("Second Array Element ["+i+"] : "+list2[i]);
       }      
   }

}

Output:

Please Enter Integer value:
1
Please Enter Integer value:
2
Please Enter Integer value:
3
Please Enter Integer value:
4
Please Enter Integer value:
5
Array Size [5]
First Array Element [0] : 1
First Array Element [1] : 2
First Array Element [2] : 3
First Array Element [3] : 4
First Array Element [4] : 5
Please Enter Integer value:
6
Please Enter Integer value:

7
Please Enter Integer value:
8
Please Enter Integer value:
9
Please Enter Integer value:
0
Please Enter Integer value:
11
Please Enter Integer value:
12
Please Enter Integer value:
13
Please Enter Integer value:
14
Please Enter Integer value:
15
Second Array Element [0] : 6
Second Array Element [1] : 7
Second Array Element [2] : 8
Second Array Element [3] : 9
Second Array Element [4] : 0
Second Array Element [5] : 11
Second Array Element [6] : 12
Second Array Element [7] : 13
Second Array Element [8] : 14
Second Array Element [9] : 15
First Array Size [5]
First Array Element [0] : 1
First Array Element [1] : 2
First Array Element [2] : 3
First Array Element [3] : 4
First Array Element [4] : 5
Second Array Size [10]
Second Array Element [0] : 6
Second Array Element [1] : 7
Second Array Element [2] : 8
Second Array Element [3] : 9
Second Array Element [4] : 0
Second Array Element [5] : 11
Second Array Element [6] : 12
Second Array Element [7] : 13
Second Array Element [8] : 14
Second Array Element [9] : 15