HW

Create an ArrayList that includes 2 of the 4 factors listed below.

Find and display the hashCode of an Arraylist before and after being sorted

Return "ascending" if the list is sorted in ascending order, return "descending" if it is descending, and return "neither" if neither

import java.util.Scanner; //allows scanners to be in program
public class arrayList  
{  
public static void main(String args[])  
{}}
//object created (integer arraylist)

ArrayList<Integer> numbers = new ArrayList<Integer>();  
numbers.add(45);
numbers.add(10);
numbers.add(82);
numbers.add(66);
numbers.add(77);
numbers.add(95);
//printing out of order
System.out.println("The list is not in order (neither): " + numbers); 
System.out.println("Hashcode: " + numbers.hashCode());
//sorting ArrayList in ascending order  
Collections.sort(numbers);  
//printing ArrayList after sorting in ascension 
System.out.println("The list is ascending: " + numbers);  
//hashcode
System.out.println("Hashcode: " + numbers.hashCode());
//descending order

Collections.sort(numbers, Collections.reverseOrder());
System.out.println("The list is descending: " + numbers);
System.out.println("Hashcode: " + numbers.hashCode());
The list is not in order (neither): [45, 10, 82, 66, 77, 95]
Hashcode: -2107407840
The list is ascending: [10, 45, 66, 77, 82, 95]
Hashcode: 1217396476
The list is descending: [95, 82, 77, 66, 45, 10]
Hashcode: -609606810

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.ArrayList;
import java.util.Random;
import java.util.Scanner;

// Parent class
class Animal {
  // Private member variable
  private String name;

  // Constructor
  public Animal(String name) {
    this.name = name;
  }

  // Getter method for the name
  public String getName() {
    return this.name;
  }
}

// Child class that extends the Animal class
class Cat extends Animal {
  // Private member variable
  private int lives;

  // Constructor that calls the parent class constructor
  public Cat(String name, int lives) {
    super(name);
    this.lives = lives;
  }

  // Getter method for the number of lives
  public int getLives() {
    return this.lives;
  }
}

public class ArrayListExample {}
  public static void main(String[] args){
    // Create a Scanner object to read input from the user
    Scanner scanner = new Scanner(System.in);

    // Create an ArrayList of Animal objects
    ArrayList<Animal> animals = new ArrayList<>();

    // Use a for loop to add random Cat objects to the list
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
      String name = "Cat" + i;
      int lives = rand.nextInt(9) + 1;
      animals.add(new Cat(name, lives));
    }

    // Prompt the user to enter some text
    System.out.print("Enter some text: ");

    // Read the user's input from the keyboard
    String input = scanner.nextLine();

    // Print the user's input
    System.out.println("You entered: " + input);

    // Use a while loop to print the names of the cats
    int i = 0;
    while (i < animals.size()) {
      // Use the getName() method to get the name of the current cat
      System.out.println("Name: " + animals.get(i).getName());
      i++;
    }
}

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