Array

Objective

In this Assignment, we test your knowledge of using arrays in Java. In this assignment, you will create a Java program. Requirements are given below:

Introduction

An array is a simple data structure used to store a collection of data in a contiguous block of memory. Each element in the collection is accessed using an index, and the elements are easy to find because they are stored sequentially in memory. Because the collection of elements in an array is stored as a big block of data, we typically use arrays when we know exactly how many pieces of data we are going to have.

Tasks:

  1. Write a program that creates an array of integers with a length of 3. Assign the values 1, 2, and 3 to the indexes. Print out each array element.

  2. Write a program that returns the middle element in an array. Give the following values to the integer array: {13, 5, 7, 68, 2} and produce the following output: 7

  3. Write a program that creates an array of String type and initializes it with the strings “red,” “green,” “blue,” and “yellow.” Print out the array length. Make a copy using the clone( ) method. Use the Arrays.toString( ) method on the new array to verify that the original array was copied.

  4. Write a program that creates an integer array with 5 elements (i.e., numbers). The numbers can be any integers. Print out the value at the first index and the last index using length - 1 as the index. Now try printing the value at index = length (e.g., myArray[myArray.length] ). Notice the type of exception which is produced. Now try to assign a value to the array index 5. You should get the same type of exception.

  5. Write a program where you create an integer array with a length of 5. Loop through the array and assign the value of the loop control variable (e.g., i) to the corresponding index in the array.

  6. Write a program where you create an integer array of 5 numbers. Loop through the array and assign the value of the loop control variable multiplied by 2 to the corresponding index in the array.

  7. Write a program where you create an array of 5 elements. Loop through the array and print the value of each element, except for the middle (index 2) element.

  8. Write a program that creates a String array of 5 elements and swaps the first element with the middle element without creating a new array.

  9. Write a program to sort the following int array in ascending order: {4, 2, 9, 13, 1, 0}. Print the array in ascending order, and print the smallest and the largest element of the array. The output will look like the following:

    Array in ascending order: 0, 1, 2, 4, 9, 13
    The smallest number is 0
    The biggest number is 13
    
  10. Create an array that includes an integer, 3 strings, and 1 double. Print the array.

  11. Write some Java code that asks the user how many favorite things they have. Based on their answer, you should create a String array of the correct size. Then ask the user to enter the things and store them in the array you created. Finally, print out the contents of the array.

Example:

How many favorite things do you have?
7

Enter your thing: phone
Enter your thing: tv
Enter your thing: xbox
Enter your thing: wine
Enter your thing: beer
Enter your thing: sofa
Enter your thing: book
Your favorite  things are:
phone tv xbox wine beer sofa book
package PracticeAssignment;

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

public class Array {
    public static void main(String[] args) {
        // Task 1: Write a program that creates an array of integers with a length of 3.
        // Assign the values 1, 2, and 3 to the indexes.
        // Print out each array element
        int[] arr1 = new int[3];
        arr1[0] = 1;
        arr1[1] = 2;
        arr1[2] = 3;
        for (int element : arr1) {
            System.out.println(element);
        }

        // Task 2: Write a program that returns the middle element in an array.
        // Give the following values to the integer array: {13, 5, 7, 68, 2} and
        // produce the following output: 7
        int[] arr2 = {13, 5, 7, 68, 2};
        int midIndex = arr2.length / 2;
        System.out.println(arr2[midIndex]);

        // Task 3: Write a program that creates an array of String type and
        // initializes it with the strings “red,” “green,” “blue,” and “yellow.”
        // Print out the array length. Make a copy using the clone( ) method.
        // Use the Arrays.toString( ) method on the new array to verify that
        // the original array was copied
        String[] arr3 = {"red", "green", "blue", "yellow"};
        System.out.println(arr3.length);
        String[] arr3Copy = arr3.clone();
        System.out.println(Arrays.toString(arr3Copy));

        // Task 4: Write a program that creates an integer array with 5 elements (i.e., numbers).
        // The numbers can be any integers.
        // Print out the value at the first index and
        // the last index using length - 1 as the index.
        // Now try printing the value at index = length (e.g., myArray[myArray.length] ).
        // Notice the type of exception which is produced.
        // Now try to assign a value to the array index 5.
        // You should get the same type of exception
        int[] arr4 = {1, 2, 3, 4, 5};
        System.out.println(arr4[0]);
        System.out.println(arr4[arr4.length - 1]);
//        System.out.println(arr[arr.length]);
//        arr[5] = 6;

        // Task 5: Write a program where you create an integer array with a length of 5.
        // Loop through the array and assign the value of the loop control variable (e.g., i)
        // to the corresponding index in the array
        int[] arr5 = new int[5];
        for (int i = 0; i < arr5.length; i++) {
            arr5[i] = i;
        }
        System.out.println(Arrays.toString(arr5));

        // Task 6: Write a program where you create an integer array of 5 numbers.
        // Loop through the array and assign the value of the loop control
        // variable multiplied by 2 to the corresponding index in the array
        int[] arr6 = new int[5];
        for (int i = 0; i < arr6.length; i++) {
            arr6[i] = i * 2;
        }
        System.out.println(Arrays.toString(arr6));

        // Task 7: Write a program where you create an array of 5 elements.
        // Loop through the array and print the value of each element,
        // except for the middle (index 2) element
        int[] arr7 = {1, 2, 3, 4, 5};
        int[] arr7LeftPart = Arrays.copyOfRange(arr7, 0, arr7.length / 2);
        int[] arr7RightPart = Arrays.copyOfRange(arr7, arr7.length / 2 + 1, arr7.length);
        System.out.println(Arrays.toString(arr7LeftPart) + Arrays.toString(arr7RightPart));

        // Task 8: Write a program that creates a String array of 5 elements and swaps
        // the first element with the middle element without creating a new array
        String[] arr8 = {"element1", "element2", "element3", "element4", "element5"};
        String temp = arr8[0];
        arr8[0] = arr8[arr8.length / 2];
        arr8[arr8.length / 2] = temp;
        System.out.println(Arrays.toString(arr8));

        // Task 9: Write a program to sort the following int array in ascending order: {4, 2, 9, 13, 1, 0}.
        // Print the array in ascending order, and print the smallest and the largest element of the array
        int[] arr9 = {4, 2, 9, 13, 1, 0};
        Arrays.sort(arr9);
        System.out.println(Arrays.toString(arr9));
        System.out.println(arr9[0]);
        System.out.println(arr9[arr9.length - 1]);

        // Task 10: Create an array that includes an integer, 3 strings, and 1 double. Print the array
        Object[] arr10 = {1, "Str1", "str2", "str3", "1.2"};
        System.out.println(Arrays.toString(arr10));

        // Task 11: Write some Java code that asks the user how many favorite things they have.
        // Based on their answer, you should create a String array of the correct size.
        // Then ask the user to enter the things and store them in the array you created.
        // Finally, print out the contents of the array
        Scanner input = new Scanner(System.in);
        System.out.println("How many favorite things do you have?");
        int n = input.nextInt();
        String[] arr11 = new String[n];
        for (int i = 0; i < n; i++) {
            System.out.println("Enter your thing:");
            arr11[i] = input.next();
        }
        System.out.println("Your favorite things are:");
        for (int i = 0; i < arr11.length; i++) {
            System.out.print(arr11[i] + " ");
        }
    }
}

Last updated