In: Computer Science
1. Practice searching
2. Continue learning the significance of special cases!
3. Learning how to write test to check your implementation
Things you must do:
1. There are many details in this lab. Make sure you read the whole thing carefully before writing any code and closely follow this instruction.
2. Always remember Java is case sensitive.
3. Your file names, class names, and package name must match exactly as they are specified here.
Things you must not do:
1. You must not change the file names, class names, package names.
2. You must not change the signature of any of these methods (name, parameters, …). Just fill in the missing code inside them. However, you are more than welcome to create any helper methods you need as necessary.
Part 1 - BinarySearchSet Class Description For this lab (and assignment), you are asked to construct a class called BinarySearchSet that handles a list(s) of doubles. We have provided the skeleton of this class for you. This class requires: • All of the usual operations for a list, such as the ability to add items, remove items, search for items, grow itself before running out of space.
• You must make your search routine(s) as efficient as possible. You are not allowed to use sort algorithms. Instead, you need to consider the fact that any list will begin as an empty list (which is already in sorted order), and you can simply insert items in the correct order to ensure that the list remains sorted at all times.
• Furthermore, the list must not contain any duplicates.
• The data in BinarySearchSet must be backed by a basic array (do not use a Java List, ArrayList, or any other Java class).
• It is not acceptable for the array to run out of space for new items, nor is it acceptable to create a gigantic array. Start with a modestly-size array, let’s say 6, and increase the capacity as needed (see the grow() function description).
• Unlike previous assignments you are not given any tests as a starting point. You must create your own tests and submit them with your program. (Note that the previous assignment will be very useful in helping you accomplish this).
Part 2 – BinarySearchSet Class Implementation We start by implementing the easier methods. Remember that the list must be sorted at all times. Please pay close attention to the following notes:
• public BinarySearchSet() //default constructor o This constructor will create a storage array of size 6 (an initial size for an empty list) o and set the rest of the field members accordingly. Pay attention to the member variables of the class! Each should be set to some (appropriate) initial value.
• public boolean isEmpty() o returns true only if this list contains no elements • public int size() o returns the number of elements in this list • public void grow() o this function doubles the size of the storage array and modifies member variables accordingly. o consult this week’s slides
• public String toString() o this method will print the elements of the list, its capacity, and its current size. o This method is purely to help you test your implementation. We will NOT test your toString() method when grading.
• private int sequentialFind(double target) o this method will return the index where the element target occurs. This method must make use of the Sequential search we learned in class. If target is not present in the list, then it should return the index where it should be added (-index - 1). Does this formula make sense? o There are three cases to consider. What should be returned in each? § target is equal to storage[index] § target becomes less than storage[index] § the loop terminates without success
• public boolean insert(double newVal) o If the list does not contain newVal, add it to the correct position of the list and return true. If the list already includes the value, return false. o The method should first make sure there is enough room in the list to handle the operation. If not, what should you do? o It should then make a call to findIndex(), which then calls the private sequentialFind you just implemented, to see if newVal already occurs in the list (how do we know?). If it isn’t in the list, the index returned by findIndex specifies the position where newVal is to be inserted (but it is negative! what should you do? :-) o Be careful about which member variables should be updated before returning true.
• public void clear() o Removes all the elements from the list. A call to isEmpty() should return true after this method is called. o Be careful about which member variables should be updated.
Part 3 – BinarySearchSet Class Implementation – Phase 2 THIS PART IS NOT OPTIONAL Unlike previous labs/assignments you are not given any tests as a starting point. You must create your own tests to examine each method you implemented. These tests can be written inside Tester.java. Here is a testing strategy: create an empty list. Then, check its size, check whether it is empty. Then, start adding a couple of numbers to your list and make sure your list adds them properly (that is, they are in the sorted order, the other member variables reflect the changes, etc). Then, continue adding values to your list till you go beyond its original capacity. Make sure you are not having any error in this case and that the list does indeed grow. Finally, test your clear to see if all elements are removed from the list.
package lab05;
public class BinarySearchSet {
/** represent the simple array that holds the list
values */
public double[] storage;
/** holds the length of the storage array */
private int capacity;
/** holds the number of elements in the list */
private int size;
/** keep this TRUE for lab **/
public boolean labFind = true;
/** default constructor */
public BinarySearchSet(){
public double[] storage;
private int capacity;
private int numItems;
}
public BinarySearchSet(double[] input){
// TODO (for assignment, not
lab)
}
public boolean isEmpty(){
// TODO
return false; //placeholder
}
public int size(){
// TODO
return 0; //placeholder
}
public void grow(){
// TODO
}
public String toString(){
// TODO
return null; // placeholder
}
public void clear() {
// TODO
}
public boolean insert(double newVal){
// TODO
return false;
}
public boolean remove(double element){
// TODO
return false; // placeholder
}
private int sequentialFind(double target) {
// TODO
return 0; //placeholder
}
private int binaryFind(double target) {
// TODO
return 0; // placeholder
}
public int findIndex(double target) {
if (labFind) {
return
sequentialFind(target);
} else {
return
binaryFind(target);
}
}
public boolean containsAll(double[]
elements){
// TODO
return false; // placeholder
}
public boolean contains(double element){
// TODO
return false; // placeholder
}
}
package lab05;
public class Tester {
}
import java.util.Arrays;
public class BinarySearchSet {
/** represent the simple array that holds the list
values */
public double[] storage;
/** holds the length of the storage array */
private int capacity;
/** holds the number of elements in the list */
private int size;
/** keep this TRUE for lab **/
public boolean labFind = true;
/** default constructor */
public BinarySearchSet() {
this.capacity = 6;
this.storage = new
double[this.capacity];
this.size = 0;
}
public BinarySearchSet(double[] input) {
// TODO (for assignment, not
lab)
this.storage = input;
}
public boolean isEmpty() {
// TODO
if (this.size == 0)
return true;
return false; //
placeholder
}
public int size() {
// TODO
return this.size; //
placeholder
}
public void grow() {
// TODO
this.capacity = this.capacity *
2;
this.storage =
Arrays.copyOf(this.storage, this.capacity);
}
public String toString() {
// TODO
return "Elements of array are :
" + Arrays.toString(storage) + "\n Capacity is : " +
this.capacity
+ "\nSize of array is : " + this.size; //
placeholder
}
public void clear() {
// TODO
this.capacity=6;
this.storage = new
double[this.capacity];
this.size=0;
}
public boolean insert(double newVal) {
// TODO
int ind = findIndex(newVal);
if(ind==-1) {
if(size==capacity)
grow();
storage[size++]=newVal;
}
else
return
false;
return labFind;
}
public boolean remove(double element) {
// TODO
return false; // placeholder
}
private int sequentialFind(double target) {
for (int index = 0; index <
size; index++) {
if
(storage[index] == target) {
return index;
}
}
return -1;
}
private int binaryFind(double target) {
// TODO
return 0; // placeholder
}
public int findIndex(double target) {
if (labFind) {
return
sequentialFind(target);
} else {
return
binaryFind(target);
}
}
public boolean containsAll(double[] elements)
{
// TODO
return false; // placeholder
}
public boolean contains(double element) {
// TODO
return false; // placeholder
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
// TODO Auto-generated method
stub
BinarySearchSet list = new
BinarySearchSet();
System.out.println("Size of list is
: " + list.size());
System.out.println("Is list empty :
" + list.isEmpty());
System.out.println(list);
list.insert(2);
System.out.println(list);
list.clear();
System.out.println(list);
}
}
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it helpful