Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with this java assignment Array Set package arraysetpackage; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Random; public class ArraySet implements SetADT { private static final int

Help with this java assignment

image text in transcribed

Array Set

package arraysetpackage;

import java.util.Iterator;

import java.util.NoSuchElementException;

import java.util.Random;

public class ArraySet implements SetADT {

private static final int DEFAULT_SIZE = 20;

private int count;

private T[] setValues;

private Random rand;

public ArraySet (){

this(DEFAULT_SIZE);

} // end of constructor

public ArraySet (int size){

count = 0;

setValues = (T[]) new Object[size];

rand = new Random();

} // end of constructor

public void add(T element) {

if (contains(element))

return;

if (count == setValues.length) {

T[] temp = (T[]) new Object[setValues.length*2];

for (int i = 0; i

temp[i] = setValues[i];

}

setValues = temp;

}

setValues[count] = element;

count++;

}

public void addAll(SetADT set) {

// finish: this method adds all of the input sets elements to this array

}

public boolean contains(T target) {

for (int i = 0; i

if (setValues[i].equals(target))

return true;

return false;

}

public String toString () {

String toReturn = "[";

for (int i = 0; i

toReturn += setValues[i] + " ";

}

toReturn +="]";

return toReturn;

}

public boolean equals(SetADT set) {

// finish: tests to see if this set and the input set have exactly the same

// elements

return false; // this is just generic, you need to change the return

}

public boolean isEmpty() {

return count==0;

}

public Iterator iterator() {

return new ArraySetIterator(setValues,count);

}

public T remove(T element) {

for (int i = 0; i

if (setValues[i].equals(element)) {

T toReturn = setValues[i];

setValues[i] = setValues[count-1];

count--;

return toReturn;

}

}

throw new NoSuchElementException("not present");

}

public T removeRandom() {

// finish: remove and return a random element. you will use the

// local rand object

return null; // this is just generic, you need to change the return

}

public int size() {

return count;

}

public SetADT union(SetADT set) {

// finish: a new set is created and returned. This new set will

// contain all of elements from this set and the input parameter set

return null; // this is just generic, you need to change the return

}

}

array set iterator

package arraysetpackage;

import java.util.Iterator;

import java.util.NoSuchElementException;

public class ArraySetIterator implements Iterator {

private int position; //Always points to the next value

private T [] values;

private int count;

public ArraySetIterator (T [] theValues, int aCount) {

position = 0;

values = theValues;

count = aCount;

}

public boolean hasNext() {

return position

}

public T next() {

if (position >= count)

throw new NoSuchElementException("Past " + count + " elements");

position++;

return values[position - 1];

}

public void remove() {

throw new

UnsupportedOperationException("No remove for ArraySet");

}

}

array set tester

package arraysetpackage;

import java.util.Iterator;

public class ArraySetTester {

public static void main(String[] args) {

SetADT mySet = new ArraySet();

for (int i = 0; i

mySet.add(new String("apple"+i));

System.out.println(mySet);

System.out.println("mysize = "+mySet.size()+ " [expect 12]");

mySet.add(new String ("apple0"));

System.out.println("mysize = "+mySet.size()+ " [expect 12]");

System.out.println("contains 11? = "+mySet.contains(new String("11")));

System.out.println("contains apple11? = "+mySet.contains(new String("apple11")));

try {

String removedItem = mySet.remove("apple7");

System.out.println(mySet);

System.out.println(removedItem+ " was removed");

} catch (Exception e) {

System.out.println("item not found, can't remove");

}

try {

String removedItem = mySet.remove("apple17");

System.out.println(mySet);

System.out.println(removedItem+ " was removed");

} catch (Exception e) {

System.out.println("item not found, can't remove");

}

Iterator iter = mySet.iterator();

while (iter.hasNext()){

System.out.println(iter.next());

}

SetADT mySet2 = new ArraySet();

for (int i = 0; i

mySet2.add(new String("orange"+i));

System.out.println(mySet2);

// add code here to test methods you finish in ArraySet

// after you complete the existing methods, do the Case Study

// Approach 1 will be here in the main

// Approach 2 will be here in ArraySetTester, but you will

// create a local static method that you will call from the main

// Approach 3 will start with uncommenting the prototype in SetADT

// and then creating the method in ArraySet. Finally you will write

// code here to test the new method

}

}

Set ADT

package arraysetpackage;

import java.util.Iterator;

public interface SetADT {

public void add (T element); //Adds one element to this set, ignoring duplicates

public void addAll (SetADT set); //Adds all elements in the parameter to this set,

// ignoring duplicates

public T removeRandom (); //Removes and returns a random element from this set

public T remove (T element); //Removes and returns the specified element from this set

public SetADT union (SetADT set); //Returns the union of this set and the

// parameter

public boolean contains (T target); //Returns true if this set contains the parameter

//public boolean contains(SetADT Set); // Returns true if this set contains the parameter

public boolean equals (SetADT set); //Returns true if this set and the parameter

//contain exactly same elements

public boolean isEmpty(); //Returns true if this set contains no elements

public int size(); //Returns the number of elements in this set

public Iterator iterator(); //Returns an iterator for the elements in this set

public String toString(); //Returns a string representation of this set

}

Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B Approach 1: Write code in the main method in a test class fill them with data and write a Add code to the main of ArraySetTester to create SetA and SetB, contains operation that tests if SetA contains SetB Question: What is wrong with this approach? Answer: While the code does the operation for the specific collections SetA and SetB, it only applies to these two collections. The code can't be used for other Sets without repeating it. Approach 2: Write a static method in a test class Comment out the code from approach 1 Add a static method to ArraySetTester with prototype public static boolean contains (SetADT Setl, SetADTKInteger> Set2) Question: Why does contains need to be static? Answer: We aren't instantiating an object to call the contains. Therefore contains has to be associated with the ArraySetTester class itself, rather than a particular instance. Question: Why were Setl and Set2 declared as SetADT rather than as ArraySet? Answer: This way the contains method can be called for any implementation of SetADT Approach 3: Add a prototype to the interface Add the following prototype to the SetADT interface and add an implementation of contains to ArraySet public boolean contains (SetADT Setl, SetADTKInteger> Set2) Question: Why does contains need to be static? Answer: We aren't instantiating an object to call the contains. Therefore contains has to be associated with the ArraySetTester class itself, rather than a particular instance. Question: Why were Setl and Set2 declared as SetADT rather than as ArraySet? Answer: This way the contains method can be called for any implementation of SetADT Approach 3: Add a prototype to the interface Add the following prototype to the SetADT interface and add an implementation of contains to ArraySet public boolean contains (SetADT

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

Simplify the given algebraic expressions. 3t 4s 3t s

Answered: 1 week ago

Question

Do you agree with the results/recommendations?

Answered: 1 week ago