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

Programming with arrays in Java: 1.Write a program that reads an integer from th

ID: 3669906 • Letter: P

Question

Programming with arrays in Java:

1.Write a program that reads an integer from the user, then creates an array of integers of that length. It then fills the array with integers read from the user.

2.In your program’s main class, define a static method printArray that takes an array of integers as its only argument and prints the elements of the array. You should print all the elements of the array on one line, separated by spaces, with no extra space after the last element. Extend your program from Question 1 to use this method to print the array after reading it.

3.Extend your program to print the index of the middle element of the array and the value stored in the array at that index (with an informative message). Explain in a comment how you decide what the middle is.

4.Extend your program to compute the minimum value in the array and print it out (with an informative message).

5.Extend your program to read an integer from the user and add it to every element of the array. Print the array after changing it (with an informative message).

6.Extend your program to declare another integer array variable and make its value be a copy of the first array. That is, its value should be a reference to a new array of the same length that contains the same element values (this is called a shallow copy). Print the first and last elements of the copy (with an informative message).

7.Arrays of the same length are added by adding the corresponding elements (i.e., the first element of the sum is the sum of the first elements of each array, same for the second element, and so on). In math:

[a0,a1,...,an] + [b0,b1,...,bn] = [a0 +b0,a1 +b1,...,an +bn]

Extend your program to add the copy of the array to the original. This should change the original array but not the copy. Print both arrays afterwards to show that this is so (with an informative message).

Explanation / Answer

import java.util.Scanner;

import java.util.ArrayList;

/*In this program all methods are static so there is no need to create an object for callin methods*/

public class ArrayExample

{

   public static void main (String args[])

    {

Int[] b;

Int[] sum;

/*reads an integer from the user, then creates an array of integers of that length. It then fills the array with integers read from the user.*/

Scanner sc=new Scanner(System.in);

System.out.println("Please enter Array Length");

int n= sc.nextInt();

        int[] a= new int[n];

        System.out.println("Please enter elements in the array");

        for(int j=0;j<n;j++)

        {

            a[j]=sc.nextInt();

        }

/* a static method printArray that takes an array of integers as its only argument and prints the elements of the array. */

Public static void printArray(int a[])

{

                int i;

                for (i = 0; i < a.length; i++)

                {

/* print all the elements of the array on one line, separated by spaces, with no extra space after the last element using       System.out.print() and adding space*/                          

   System.out.print(a[i]+ " ");

                }

}

/* print the index of the middle element of the array and the value stored in the array at that index */

Public static void middle()

{

/* number of elements in array using a.length property then divided by 2*/

Int mei= a.length/2;

System.out.println("Index of Middle element of array is"+mei);

System.out.println("Value stored in the Index of Middle element of array is "+a[mei]);

}

Public static void minimum()

{

int smallest = a[0];

for(int i=1; i<a.length; i++)

{

if (a[i] < smallest)

smallest = a[i];

}

System.out.println("Smallest Number in array is : " + smallest);

}

Public static void addelement()

{

System.out.println("Please enter Integer wants add array");

Int e= sc.nextInt();

for(int i=1; i<a.length; i++)

{

a[i]=a[i]+e;

}

System.out.println(" Array with adding integer"+e);

for (i = 0; i < a.length; i++)

       {

   System.out.print(a[i]+ " ");

          }

}

//shallow copy - copy reference values

        b = a;

//print a value using second reference

Int l=b.length-1;

      System.out.println(("first element of shallow copy given array"+b[0]);

      System.out.println(("last element of shallow copy given array"+b[l]);

Public static void addarray(int c[])

{

// Arrays of the same length are added by adding the corresponding elements

     int[] b= new int[n];

        System.out.println("Please enter elements in the second array");

        for(int l=0;l<n;l++)

        {

            b[l]=sc.nextInt();

        }

for( int i = 0; i < a.length - 1; i++)

       {

         for(int l = 0; i < b.length - 1; i++)

             {

                      sum[a[i] + b[l]];

                 }

/*add the copy of the sum array to the original array, This should change the original array but not the copy.*/

for( int i = 0; i < a.length - 1; i++)

       {

         for(int j = 0; i < sum.length - 1; i++)

             {

                      a[a[i] + sum[j]];

                 }

}

   System.out.println(" The sum array of two given array");

for (i = 0; i < sum.length; i++)

       {

   System.out.print(sum[i]+ " ");

          }

   System.out.println(" Original array after adding sum array");

for (i = 0; i < a.length; i++)

       {

   System.out.print(a[i]+ " ");

             }

}

}