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 {
}
In: Computer Science
Write a class that maintains the scores for a game application. Implement the addition and removal function to update the database. The gamescore.txt contains player’ name and score data record fields separated by comma. For Removal function, uses the name field to select record to remove the game score record.
Use the List.java, LList.java, Dlink.java, GameEntry.java and gamescore.txt found below
Read gamescore.txt to initialize the Linked list in sorted order by score.
Provide Remove and Add function for user to update the sorted linked list.
Display “Name exist” when add an exist name to the list.
Display “Name does not exist” when remove a name not on the list.
List.java File:
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
/** List ADT */
public interface List<E>
{
/**
* Remove all contents from the list, so it is once again empty. Client is
* responsible for reclaiming storage used by the list elements.
*/
public void clear();
/**
* Insert an element at the current location. The client must ensure that
* the list's capacity is not exceeded.
*
* @param item
* The element to be inserted.
*/
public void insert(E item);
/**
* Append an element at the end of the list. The client must ensure that
* the list's capacity is not exceeded.
*
* @param item
* The element to be appended.
*/
public void append(E item);
/**
* Remove and return the current element.
*
* @return The element that was removed.
*/
public E remove();
/** Set the current position to the start of the list */
public void moveToStart();
/** Set the current position to the end of the list */
public void moveToEnd();
/**
* Move the current position one step left. No change if already at
* beginning.
*/
public void prev();
/**
* Move the current position one step right. No change if already at end.
*/
public void next();
/** @return The number of elements in the list. */
public int length();
/** @return The position of the current element. */
public int currPos();
/**
* Set current position.
*
* @param pos
* The position to make current.
*/
public void moveToPos(int pos);
/** @return The current element. */
public E getValue();
}
LList.java File:
/**
* Source code example for "A Practical Introduction to Data Structures and
* Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright
* 2008-2011 by Clifford A. Shaffer
*/
// Doubly linked list implementation
class LList<E> implements List<E>
{
private DLink<E> head; // Pointer to list header
private DLink<E> tail; // Pointer to last element in list
protected DLink<E> curr; // Pointer ahead of current element
int cnt; // Size of list
// Constructors
LList(int size)
{
this();
} // Ignore size
LList()
{
curr = head = new DLink<E>(null, null); // Create header node
tail = new DLink<E>(head, null);
head.setNext(tail);
cnt = 0;
}
public void clear()
{ // Remove all elements from list
head.setNext(null); // Drop access to rest of links
curr = head = new DLink<E>(null, null); // Create header node
tail = new DLink<E>(head, null);
head.setNext(tail);
cnt = 0;
}
public void moveToStart() // Set curr at list start
{
curr = head;
}
public void moveToEnd() // Set curr at list end
{
curr = tail.prev();
}
/** Insert "it" at current position */
public void insert(E it)
{
curr.setNext(new DLink<E>(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++;
}
/** Append "it" to list */
public void append(E it)
{
tail.setPrev(new DLink<E>(it, tail.prev(), tail));
tail.prev().prev().setNext(tail.prev());
cnt++;
}
/** Remove and return current element */
public E remove()
{
if (curr.next() == tail)
return null; // Nothing to remove
E it = curr.next().element(); // Remember value
curr.next().next().setPrev(curr);
curr.setNext(curr.next().next()); // Remove from list
cnt--; // Decrement the count
return it; // Return value removed
}
/** Move curr one step left; no change if at front */
public void prev()
{
if (curr != head) // Can't back up from list head
curr = curr.prev();
}
// Move curr one step right; no change if at end
public void next()
{
if (curr != tail.prev())
curr = curr.next();
}
public int length()
{
return cnt;
}
// Return the position of the current element
public int currPos()
{
DLink<E> temp = head;
int i;
for (i = 0; curr != temp; i++)
temp = temp.next();
return i;
}
// Move down list to "pos" position
public void moveToPos(int pos)
{
assert (pos >= 0) && (pos <= cnt) : "Position out of range";
curr = head;
for (int i = 0; i < pos; i++)
curr = curr.next();
}
public E getValue()
{
// Return current element
if (curr.next() == tail)
return null;
return curr.next().element();
}
// reverseList() method that reverses the LList
public void reverseList()
{
LList<E> revList = new LList<E>();
curr = tail.prev();
while (curr != head)
{
revList.append(curr.element());
curr = curr.prev();
}
head.setNext(revList.head.next());
}
// Extra stuff not printed in the book.
/**
* Generate a human-readable representation of this list's contents that
* looks something like this: < 1 2 3 | 4 5 6 >. The vertical bar
* represents the current location of the fence. This method uses
* toString() on the individual elements.
*
* @return The string representation of this list
*/
public String toString()
{
// Save the current position of the list
int oldPos = currPos();
int length = length();
StringBuffer out = new StringBuffer((length() + 1) * 4);
moveToStart();
out.append("< ");
for (int i = 0; i < oldPos; i++)
{
if (getValue() != null)
{
out.append(getValue());
out.append(" ");
}
next();
}
out.append("| ");
for (int i = oldPos; i < length; i++)
{
out.append(getValue());
out.append(" ");
next();
}
out.append(">");
moveToPos(oldPos); // Reset the fence to its original position
return out.toString();
}
}
DLink.java File:
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
/** Doubly linked list node */
class DLink<E>
{
private E element; // Value for this node
private DLink<E> next; // Pointer to next node in list
private DLink<E> prev; // Pointer to previous node
/** Constructors */
DLink(E it, DLink<E> p, DLink<E> n)
{
element = it;
prev = p;
next = n;
}
DLink(DLink<E> p, DLink<E> n)
{
prev = p;
next = n;
}
/** Get and set methods for the data members */
DLink<E> next()
{
return next;
}
DLink<E> setNext(DLink<E> nextval)
{
return next = nextval;
}
DLink<E> prev()
{
return prev;
}
DLink<E> setPrev(DLink<E> prevval)
{
return prev = prevval;
}
E element()
{
return element;
}
E setElement(E it)
{
return element = it;
}
}
GameEntry.java File:
public class GameEntry {
protected String name;
protected int score;
public GameEntry(String n, int s) {
name = n;
score = s;
}
public String getName() {return name;}
public int getScore() {return score;}
public String toString() {
return "("+name+","+score+")";
}
}
gamescore.txt File:
Mike,1105
Rob,750
Paul,720
Anna,660
Rose,590
Jack,510
In: Computer Science
DO NOT HARD CODE ANYTHING!
Write a class that maintains the scores for a game application. Implement the addition and removal function to update the database. The gamescore.txt contains player’ name and score data record fields separated by comma. For Removal function, uses the name field to select record to remove the game score record.
Use the List.java, LList.java, Dlink.java, GameEntry.java and gamescore.txt found below
Read gamescore.txt to initialize the Linked list in sorted order by score.
Important****ASK the user to Remove, Add and update function to the sorted linked list.
Display “Name exist” when add an exist name to the list.
Display “Name does not exist” when remove a name not on the list.
List.java File:
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
/** List ADT */
public interface List<E>
{
/**
* Remove all contents from the list, so it is once again empty. Client is
* responsible for reclaiming storage used by the list elements.
*/
public void clear();
/**
* Insert an element at the current location. The client must ensure that
* the list's capacity is not exceeded.
*
* @param item
* The element to be inserted.
*/
public void insert(E item);
/**
* Append an element at the end of the list. The client must ensure that
* the list's capacity is not exceeded.
*
* @param item
* The element to be appended.
*/
public void append(E item);
/**
* Remove and return the current element.
*
* @return The element that was removed.
*/
public E remove();
/** Set the current position to the start of the list */
public void moveToStart();
/** Set the current position to the end of the list */
public void moveToEnd();
/**
* Move the current position one step left. No change if already at
* beginning.
*/
public void prev();
/**
* Move the current position one step right. No change if already at end.
*/
public void next();
/** @return The number of elements in the list. */
public int length();
/** @return The position of the current element. */
public int currPos();
/**
* Set current position.
*
* @param pos
* The position to make current.
*/
public void moveToPos(int pos);
/** @return The current element. */
public E getValue();
}
LList.java File:
/**
* Source code example for "A Practical Introduction to Data Structures and
* Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright
* 2008-2011 by Clifford A. Shaffer
*/
// Doubly linked list implementation
class LList<E> implements List<E>
{
private DLink<E> head; // Pointer to list header
private DLink<E> tail; // Pointer to last element in list
protected DLink<E> curr; // Pointer ahead of current element
int cnt; // Size of list
// Constructors
LList(int size)
{
this();
} // Ignore size
LList()
{
curr = head = new DLink<E>(null, null); // Create header node
tail = new DLink<E>(head, null);
head.setNext(tail);
cnt = 0;
}
public void clear()
{ // Remove all elements from list
head.setNext(null); // Drop access to rest of links
curr = head = new DLink<E>(null, null); // Create header node
tail = new DLink<E>(head, null);
head.setNext(tail);
cnt = 0;
}
public void moveToStart() // Set curr at list start
{
curr = head;
}
public void moveToEnd() // Set curr at list end
{
curr = tail.prev();
}
/** Insert "it" at current position */
public void insert(E it)
{
curr.setNext(new DLink<E>(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++;
}
/** Append "it" to list */
public void append(E it)
{
tail.setPrev(new DLink<E>(it, tail.prev(), tail));
tail.prev().prev().setNext(tail.prev());
cnt++;
}
/** Remove and return current element */
public E remove()
{
if (curr.next() == tail)
return null; // Nothing to remove
E it = curr.next().element(); // Remember value
curr.next().next().setPrev(curr);
curr.setNext(curr.next().next()); // Remove from list
cnt--; // Decrement the count
return it; // Return value removed
}
/** Move curr one step left; no change if at front */
public void prev()
{
if (curr != head) // Can't back up from list head
curr = curr.prev();
}
// Move curr one step right; no change if at end
public void next()
{
if (curr != tail.prev())
curr = curr.next();
}
public int length()
{
return cnt;
}
// Return the position of the current element
public int currPos()
{
DLink<E> temp = head;
int i;
for (i = 0; curr != temp; i++)
temp = temp.next();
return i;
}
// Move down list to "pos" position
public void moveToPos(int pos)
{
assert (pos >= 0) && (pos <= cnt) : "Position out of range";
curr = head;
for (int i = 0; i < pos; i++)
curr = curr.next();
}
public E getValue()
{
// Return current element
if (curr.next() == tail)
return null;
return curr.next().element();
}
// reverseList() method that reverses the LList
public void reverseList()
{
LList<E> revList = new LList<E>();
curr = tail.prev();
while (curr != head)
{
revList.append(curr.element());
curr = curr.prev();
}
head.setNext(revList.head.next());
}
// Extra stuff not printed in the book.
/**
* Generate a human-readable representation of this list's contents that
* looks something like this: < 1 2 3 | 4 5 6 >. The vertical bar
* represents the current location of the fence. This method uses
* toString() on the individual elements.
*
* @return The string representation of this list
*/
public String toString()
{
// Save the current position of the list
int oldPos = currPos();
int length = length();
StringBuffer out = new StringBuffer((length() + 1) * 4);
moveToStart();
out.append("< ");
for (int i = 0; i < oldPos; i++)
{
if (getValue() != null)
{
out.append(getValue());
out.append(" ");
}
next();
}
out.append("| ");
for (int i = oldPos; i < length; i++)
{
out.append(getValue());
out.append(" ");
next();
}
out.append(">");
moveToPos(oldPos); // Reset the fence to its original position
return out.toString();
}
}
DLink.java File:
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
/** Doubly linked list node */
class DLink<E>
{
private E element; // Value for this node
private DLink<E> next; // Pointer to next node in list
private DLink<E> prev; // Pointer to previous node
/** Constructors */
DLink(E it, DLink<E> p, DLink<E> n)
{
element = it;
prev = p;
next = n;
}
DLink(DLink<E> p, DLink<E> n)
{
prev = p;
next = n;
}
/** Get and set methods for the data members */
DLink<E> next()
{
return next;
}
DLink<E> setNext(DLink<E> nextval)
{
return next = nextval;
}
DLink<E> prev()
{
return prev;
}
DLink<E> setPrev(DLink<E> prevval)
{
return prev = prevval;
}
E element()
{
return element;
}
E setElement(E it)
{
return element = it;
}
}
GameEntry.java File:
public class GameEntry {
protected String name;
protected int score;
public GameEntry(String n, int s) {
name = n;
score = s;
}
public String getName() {return name;}
public int getScore() {return score;}
public String toString() {
return "("+name+","+score+")";
}
}
gamescore.txt File:
Mike,1105
Rob,750
Paul,720
Anna,660
Rose,590
Jack,510
In: Computer Science
Imagine you are a Professor at KPU School of Business.
What are the typical “customers” (i.e. KPU students) like? Create a customer profile for KPU based on the theories we discussed about in this chapter.
In: Economics
In: Statistics and Probability
In: Statistics and Probability
In a committee of 10 students and 5 teachers, how many different groups of five can you send to a conference
a) if there must be only 1 teacher
b) If there must be at least 1 student.
In: Statistics and Probability
A random sample of 134 students has a test score average of 78 with a standard deviation of 10.4. Find the margin of error if the confidence level is 0.99. (Round answer to two decimal places)
In: Statistics and Probability
In: Statistics and Probability
When Ashley was asked to round 345 to the nearest 100, she rounded 345 to 350 and then rounded 350 to 400. Billie claimed the answer was 300. How would you respond to these students?
In: Math