Final Study
Construction
Inheritance and Polymorphism
Create an abstract shape class named Polygon. It should have a private instance variable String name. It should have an abstract method calcArea. It should have a toString method which prints the name.
Create the triangle and rectangle classes to inherit Polygon and override the toString method by first executing polygon's toString and adding the length of the other sides in the shape. Triangle's have 3 instance variables for their sides and rectangles have four. For the rectangle class, add an isSquare method.
Create an interface Colorable that just has the abstract method determineColor that takes in nothing and returns the color name. Make Triangle and Rectangle implement Colorable. If one Triangle side is more than 10 , the color is Red, and otherwise it is blue. For a rectangle, if the area is greater than 20, the color is Red, and otherwise it is blue.
Create another program that creates 3 arraylists: one of Colorable and another of Polygon and one more of Rectangle. Try to put a variable of every type inside of class you have inside the list. Then call all their methods. Guess what will happen and then see what does happen.
FileProcessing:
Read in all the words in a file. Sort them alphabetically (and it is okay to use Collections.sort). Print the sorted list.
Queue Processing:
· Copy everything in a queue to a stack. Print out the Stack while emptying it.
ArrayLIst processing:
Write a static method named arraySum that accepts two arrays of real numbers a1 and a2 as parameters and returns a new array a3 such that each element of a3 at each index i is the sum of the elements at that same index i in a1 and a2. For example, if a1 stores {4.5, 5.0, 6.6} and a2 stores {1.1, 3.4, 0.5}, your method should return {5.6, 8.4, 7.1}, which is obtained by adding 4.5 + 1.1, 5.0 + 3.4, and 6.6 + 0.5.
If the arrays a1 and a2 are not the same length, the result returned by your method should have as many elements as the larger of the two arrays. If a given index i is in bounds of a1 but not a2 (or vice versa), your result array's element at index i should be equal to the value of the element at index i in the longer of a1 or a2. For example, if a1 stores {1.8, 2.9, 9.4, 5.5} and a2 stores {2.4, 5.0}, your method should return {4.2, 7.9, 9.4, 5.5}.
The table below shows some additional calls to your method and the expected values returned:
Arrays |
Call and Value Returned |
ArrayList<Double> a1 = new ArrayList<Double>(Arrays.asList(4.5, 2.8, 3.4, 0.8)); ArrayList<Double> a2 = new ArrayList<Double>(Arrays.asList(1.4, 8.9, -1.0, 2.3)); |
arraySum(a1, a2) returns {5.9, 11.7, 2.4, 3.1} |
ArrayList<Double> ax = new ArrayList<Double>(Arrays.asList(2.4, 3.8)); ArrayList<Double> ay = new ArrayList<Double>(Arrays.asList(0.2, 9.2, 4.3, 2.8, 1.4)); |
arraySum(ax, ay) returns {2.6, 13.0, 4.3, 2.8, 1.4} |
ArrayList<Double> aa = new ArrayList<Double>(Arrays.asList(1.0, 2.0, 3.0)); ArrayList<Double> ab = new ArrayList<Double>(Arrays.asList(4.0, 5.0)); |
arraySum(aa, ab) returns {5.0, 7.0, 3.0} |
ArrayList<Double> ai = new ArrayList<Double>(); ArrayList<Double> aj = new ArrayList<Double>(Arrays.asList(42.0)); |
arraySum(ai, aj) returns {42.0} |
Collections:
Write a method that takes in 1 linkedlist, one arraylist and one map return an arraylist that all the values from either the linkedlist or arraylist, but has all the keys from the map removed from the result. It also has no duplicates and is sorted.
For example:
LInkedList has 1, 3, 5,
ArrayList has 1, 2, 4, 4, 5, 7
map has the following keys: 2,5
The returned arraylist should be 1, 3, 4, 7
Recursion:
Given base and n that are both 1 or
more, compute recursively (no loops) the value of base to the n power, so powerN(3,
2) is 9 (3 squared).
powerN(3,
1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27
public int powerN(int base, int n) {
}
Fill out this chart for Big O Notation
Description |
Big
O Notation |
Selection Sort |
|
Merge Sort |
|
Bubble Sort |
|
Linear Search |
|
Binary Search |
|
A single loop from 0 to the size
of the array |
|
A single loop from 0 to the size
of the array with a loop inside from 3 to the size of the array |
|
A single loop from 0 to the size
of the ArrayList
that gets and sets items |
|
A single loop from 0 to the size
of the LinkedList
that gets and sets items |
|
A single loop from 0 to the size
of the ArrayList
that repeatedly removes the first item |
|
A single loop from 0 to the size
of the Queue that repeatedly
removes the first item |
|
To thoroughly study for search and sort:
· Given a point class, write methods to sort and search for a y value.
· Selection, Bubble, MergeSort , Comparator
· Linear and Binary Search