Midterm 1 Review Guide

Arrays and ArrayLists
  • How do you write a linear search method for an array (primitives and Strings)?
  • How do you write a binary search method for an array (primitives and Strings)?
  • Which of the above two methods requires an array to be sorted before it can be called? Why?
  • What is the process for inserting data into the middle of an array?
    • Write an insert method to insert a new value into the middle of an array at a specified index
  • What is the process for removing data from the middle of an array?
    • Write a remove method to remove a value stored at a specified index from the middle of an array
  • How does the bubbleSort algorithm for sorting an array of data differ in approach from the insertionSort algorithm for an array of data?
  • Given an unordered array, trace bubbleSort and insertionSort on the array, labeling each comparison (bubbleSort) or insertion (insertionSort) as one pass through the algorithm:
    • A = [10, 3, 4, 1, 7, 2]
  • Write the bubbleSort method for an array of primitives and Strings
  • How do you declare a 2D array  and assign it some values (know at least two distinct options (no loop involved))?
  • Write a method to print a 2D array to the console
  • How do you declare an ArrayList of a specified datatype
    • Given a class named Student, how do you make an ArrayList of Student objects?
  • How do you insert, remove, access the size, access an element at a specific index, determine whether or not a value is stored in the ArrayList, locate the index of an element in an ArrayList, and update the value stored in an ArrayList? Hint: which methods do you call?
Classes and Objects
  • Define abstraction. How does this concept relate to defining a Java class?
  • Define encapsulation. How does this concept relate to defining a Java class?
  • Define information hiding. What is the rationale for information hiding of the member variables of a class?
  • Define a mutator method. Give an example of a mutator method.
  • Define an accessor method. Give an example of a accessor method.
  • Declare a new class named Cat with a String member variable called name and a double member variable called weight, both marked private. Then add the following methods:
    • getters and setters for the member variables
    • default constructor
    • one argument constructor
    • two argument constructor
    • toString()
  • What is shadowing?
    • Given the below method definition for a class called Student, rewrite the method two ways to avoid shadowing.
    • One of the ways must use the keyword this.
public Student(String name, int id, double gpa) {
    name = name;
    id = id;
    gpa = gpa;
}

  • Write the copy constructor for the below Date class:

public class Date {
    private int day;
    private int month;
    private int year;

    public Date() {
        day = 0;
        month = 0;
        year = 0;
    }

    public Date(int d, int m, int y) {
        day = d;
        month = m;
        year = y;
    }

    //assume rest of class defined here
}


  • Next, write a copy constructor for the below class that successfully creates a deep, rather than shallow copy. Note that this Birthday class stores a Date reference variable:
public class Birthday {
    private String name;
    private Date date;

    public Birthday() {
        name = "no name";
        date = new Date();
    }

    public Birthday(String n, Date d) {
        name = n;
        date = d;
    }

    //write the copy constructor here!

    //assume rest of class defined here

}


  • Given the below class:
public class Dog {
    private String name;
    private double weight;

   
    public Dog() {    
    this.name = "No name";
    this.weight = -1;
}
 
    public Dog(String name, double weight) {
        this.name = name;
        this.weight = weight;
    }

    public Dog(Dog d) {
        if(d != null) {
            this.name = d.name;
            this.weight = d.weight;
        }
    }

    public String getName() {
        return name;
    }

    public double getWeight() {
        return weight;
    }

    @Override public String toString() {
        return "Name: " + name + "\nWeight: " + weight;
    }

}
  • How many distinct Dog objects are created in the below program:
public static void main(String[] args) {
    Dog d1 = new Dog("Fluffy", 15);
    Dog d2 = d1;
    System.out.println(d2);
    Dog d3 = new Dog(d2);
    System.out.println(d3.getName());
    Dog d4 = d3;
    Dog d5 = new Dog("James", 45);
    System.out.println(d4.getName());
    Dog d6 = new Dog();
    d6 = d5;
    d6 = new Dog("Chester Chihuahua", 6);
    }