HW

swaps first/last elements

import java.util.Arrays; 
 public class unit6hw {
    public static void main(String[] args)
 {}}
        int[] array1 = {92, 38, 23, 90, 12, 54, 72};
        System.out.println("Original Array: "+Arrays.toString(array1)); 
        int x = array1[0];
        array1[0] = array1[array1.length-1];
        array1[array1.length-1] = x;
        System.out.println("New array: "+Arrays.toString(array1)); 
        //swaps first/last elements
Original Array: [92, 38, 23, 90, 12, 54, 72]
New array: [72, 38, 23, 90, 12, 54, 92]

all even values to 0

//all even values to 0
import java.util.Arrays; 
 public class unit6hw {
    public static void main(String[] args)
 {}}
        int[] array1;
        array1 = new int[7];
        int array1[]  = {92, 38, 23, 90, 65, 54, 72, 99};
        // const int size = 7;
        // int array1[size] = { 1,2,3,4,5,6,7 };    
        for (int i = 0; i < array1.length; i++)
        {
            if (array1[i] % 2 == 0)
                array1[i] = 0;
    System.out.println("New array: "+Arrays.toString(array1)); }
New array: [0, 38, 23, 90, 65, 54, 72, 99]
New array: [0, 0, 23, 90, 65, 54, 72, 99]
New array: [0, 0, 23, 90, 65, 54, 72, 99]
New array: [0, 0, 23, 0, 65, 54, 72, 99]
New array: [0, 0, 23, 0, 65, 54, 72, 99]
New array: [0, 0, 23, 0, 65, 0, 72, 99]
New array: [0, 0, 23, 0, 65, 0, 0, 99]
New array: [0, 0, 23, 0, 65, 0, 0, 99]

VOCAB IMPLEMENTATION

The following items are used by arrays and arraylists:

  • Casting, specifically for truncating or rounding: Arrays and arraylists may use casting for truncating or rounding values. For example, if an array or arraylist contains floating-point numbers, they may be cast to integers using the '(int)' syntax to round or truncate the values.

  • Wrapper classes, why wrap int, double. Show examples: Arrays and arraylists may use wrapper classes to store primitive data types such as 'int' and 'double' in objects. Wrapper classes allow these primitive data types to be treated as objects, which can be useful for certain operations and data structures. For example, an array of 'int' values could be wrapped in the 'Integer' wrapper class to create an array of 'Integer' objects.

  • Math class, specifically Random usage: The 'Math' class, which contains various mathematical functions and constants, may be used by arrays and arraylists. For example, the 'random' method of the 'Math' class can be used to generate random numbers, which could be used to initialize the values of an array or arraylist.

  • for loop, enhanced for loop: Arrays and arraylists may be iterated over using a 'for' loop or an 'enhanced for' loop. These loops allow the programmer to easily access and manipulate each element in the array or arraylist in turn.

  • while loop versus do while loop: Arrays and arraylists may be iterated over using a 'while' loop or a 'do-while' loop. These loops are similar to 'for' loops, but offer more flexibility in terms of controlling the iteration. A 'while' loop will only execute its body if a certain condition is met, whereas a 'do-while' loop will always execute its body at least once.

  • nested loops: Arrays and arraylists may be nested within other loops, allowing the programmer to perform complex operations on the data. For example, a 'for' loop could be used to iterate over an array of arrays, allowing the programmer to access and manipulate the elements of each inner array in turn.

  • Show use case of access modifiers: Public, Private, Protected: Arrays and arraylists may be declared with access modifiers such as 'public', 'private', and 'protected'. These modifiers control the visibility and accessibility of the array or arraylist within the program. For example, a 'private' array or arraylist can only be accessed within the class in which it is declared, whereas a 'public' array or arraylist can be accessed by any other class.

  • Static methods, Class methods: Arrays and arraylists may be declared as static or class methods. Static methods are methods that belong to the class itself, rather than to individual instances of the class. This means that they can be called without creating an instance of the class, and are often used for utility functions. Class methods are similar to static methods, but can be overridden by subclasses.

  • this Keyword: The 'this' keyword may be used in the context of arrays and arraylists to refer to the current instance of the class. This can be useful when dealing with multiple variables with the same name, or when calling methods on the current instance of the class.

  • main method, tester methods: Arrays and arraylists may be tested using a 'main' method or other tester methods. The 'main' method is the entry point for a Java program, and is typically used to test the functionality of the program. Tester methods are similar to the 'main' method, but are used to test specific components or features of the program.

  • Inheritance

import java.util.Arrays;
import java.util.Random;

public class ArrayExample {
  private static int[] values;
  private static Integer[] wrapped;

  public static void main(String[] args) {
    // Initialize the array with random values
    values = new int[10];
    Random rand = new Random();
    for (int i = 0; i < values.length; i++) {
      values[i] = rand.nextInt(100);
    }

    // Print the array
    System.out.println("Original array: " + Arrays.toString(values));

    // Truncate and round the array elements
    processArray();

    // Print the updated array
    System.out.println("Truncated and rounded array: " + Arrays.toString(values));

    // Use a wrapper class to store the array elements as objects
    wrapped();

    // Print the squared array
    System.out.println("Squared array: " + Arrays.toString(wrapped));

    // Use a while loop to iterate over the array
    int i = 0;
    while (i < values.length) {
      // Print the current element
      System.out.println("Element at index " + i + ": " + wrapped[i]);
      i++;
    }
  }

  public static void processArray() {
    for (int i = 0; i < values.length; i++) {
      // Truncate the element by casting it to an integer
      int truncated = (int) values[i];
      // Round the element by adding 0.5 and casting to an integer
      int rounded = (int) (values[i] + 0.5);

      // Update the array with the truncated and rounded values
      values[i] = truncated;
      values[i] = rounded;
    }
  }

  public static void wrapped() {
    wrapped = new Integer[values.length];
    for (int i = 0; i < values.length; i++) {
      // Wrap the element in an Integer object
      wrapped[i] = new Integer(values[i]);
    }

    // Use the Math class to square the array elements
    for (int i = 0; i < values.length; i++) {
      // Square the element using the Math.pow method
      wrapped[i] = (int) Math.pow(wrapped[i], 2);
    }
  }
}

public static void testArrayExample() {
  // Create an array of integers
  int[] arr = new int[] {3, 4, 90, 5, 6, 7};

  ArrayProcessor processor = new ArrayProcessor(arr);

  // Truncate and round the elements of the array
  processor.processArray();
  processor.wrapped();

  // Print the updated array
  System.out.println("Updated array: " + Arrays.toString(arr));
  System.out.println("Squared array: " + Arrays.toString(wrapped));
  System.out.println("Element at index " + i + ": " + wrapped[i]);
  System.out.println("Truncated and rounded array: " + Arrays.toString(values));
}
import java.util.Arrays;
import java.util.Random;

  // Subclass to process array elements
  public static class ArrayProcessor {
    private int[] array;

    // Constructor
    public ArrayProcessor(int[] arr) {
      this.array = arr;
    }

    // Method to truncate and round the array elements
    public void processArray() {
      for (int i = 0; i < this.array.length; i++) {
        // Truncate the element by casting it to an integer
        int truncated = (int) this.array[i];
        // Round the element by adding 0.5 and casting to an integer
        int rounded = (int) (this.array[i] + 0.5);

        // Update the array with the truncated and rounded values
        this.array[i] = truncated;
        this.array[i] = rounded;
      }
    }
  }

  // Tester method to test the ArrayProcessor class
  public static void testArrayProcessor() {}
    // Create an array of integers
    int[] arr = new int[] {1, 2, 3, 4, 5, 6};
  
    // Create an instance of the ArrayProcessor class
    ArrayProcessor processor = new ArrayProcessor(arr);
  
    // Truncate and round the elements of the array
    processor.processArray();
  
    // Print the updated array
    System.out.println("Updated array: " + Arrays.toString(arr));
Updated array: [1, 2, 3, 4, 5, 6]

What arrays and arraylists don't include:

  • Casting, specifically for division: Arrays and arraylists do not use casting for division. Division is performed using the '/' operator, and the type of the result depends on the types of the operands.

  • Concatenation, explain or illustrate rules on mixed type concatenation: Concatenation is not used by arrays or arraylists. Instead, these data structures store and manipulate individual elements of a specific type.

  • Compound Boolean expression: A compound boolean expression is a boolean expression that combines two or more individual boolean expressions using logical operators such as 'and' and 'or'. This is not directly used by arrays or arraylists, although they may be used in a boolean expression to check the value of an array or arraylist element.

  • Truth tables: Truth tables are not used by arrays or arraylists. Truth tables are used to show the logical relationships between different boolean expressions.

  • De Morgan's Law: De Morgan's Law is not used by arrays or arraylists. It is a fundamental theorem of logic that describes the relationship between the logical operators 'and' and 'or' and their negations 'not and' and 'not or'.

  • Comparing numbers: Comparing numbers is not used by arrays or arraylists. Arrays and arraylists store and manipulate individual elements, and the comparison of these elements is performed using the standard comparison operators such as '==', '>', and '<'.

  • Comparing strings: Comparing strings is not used by arrays or arraylists. Like numbers, arrays and arraylists store and manipulate individual elements, and the comparison of these elements is performed using the standard comparison operators such as '==', '>', and '<'.

  • Comparing objects: Comparing objects is not used by arrays or arraylists. Objects in Java are compared using the 'equals' method, which is defined by the 'Object' class and can be overridden by subclasses to provide custom comparison behavior.

  • Arrays and arraylists do not typically contain objects, and even if they did, the 'equals' method would not be used to compare them.

  • Creating a class, describe naming conventions: Creating a class is not used by arrays or arraylists. In Java, arrays and arraylists are implemented using existing classes, and do not require the creation of new classes. Naming conventions refer to the rules and guidelines for naming variables, methods, and other entities in a program, and are not directly related to arrays or arraylists.

  • Constructor, describe why there is no return: A constructor is not used by arrays or arraylists. A constructor is a special method that is used to create and initialize an instance of a class. Unlike other methods, constructors do not have a return type, because they do not return a value. Instead, they return a new instance of the class.

  • Accessor methods, relationship to getter: Accessor methods, also known as getter methods, are not used by arrays or arraylists. Getter methods are methods that are used to retrieve the value of an object's field. They are typically used in classes to provide a way for other objects to access the object's internal state without directly accessing its fields. Arrays and arraylists do not have fields, and do not use getter methods.

  • Mutator methods, relationship to setter, describe void return type: Mutator methods, also known as setter methods, are not used by arrays or arraylists. Setter methods are methods that are used to modify the value of an object's field. They are typically used in classes to provide a way