SlideShare a Scribd company logo
this file has a complete array-based MultiSet, but not the code needed to support its Iterator.
Complete the Iterator's next() and hasNext() methods.
Solution
Hi, I have implemented required methods.
Please let me know in case of any issue.
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayMultiSet implements Collection {
/** Array in which the elements in this multiset are stored. */
private E[] _store;
/**
* Array indices below this amount contain the active elements in this collection.
*/
private int _size;
/**
* Modification counter used to preserve the fail-fast nature of its iterators.
*/
private long _modCount;
/**
* Create a new empty multiset.
*/
public ArrayMultiSet() {
_modCount = 0;
clear();
}
/**
* Remove all of the elements within the instance and invalidate any current iterators.
*/
@SuppressWarnings("unchecked")
@Override
public void clear() {
_store = (E[]) (new Object[16]);
_size = 0;
// maintains the class invariant
}
/**
* Update the multiset so that it includes all of the elements from before the call AND the given
element.
*
* @param e Item to be added to this collection.
*/
@SuppressWarnings("unchecked")
@Override
public boolean add(E e) {
// Check if we do not have enough space in the underlying array to store the
// new element
if (_size == _store.length) {
// We do not have space, so create a new larger space (doubling the size
// is the most time efficient)
E[] newStore = (E[]) new Object[_store.length * 2];
// Copy all of the references into the new array
for (int i = 0; i < _store.length; i++ ) {
newStore[i] = _store[i];
}
_store = newStore;
// An easier, more efficient way of coding this (but less useful for
// teaching) would instead be:
// _store = Arrays.copyOf(_store, _store.length * 2);
}
// Add the element to the store
_store[_size] = e;
// Finally, we can increase _size, since this change will no longer violate
// any class invariants.
_size += 1;
return true;
}
/**
* Return true if at least one element in the multiset is equal to the given object. When {@code
obj} is null, it must
* use the {@code ==} operator to perform these checks, but when {@code obj} is not null, the
{@link Object#equals}
* method is used.
*
* @param obj Object (or null) for which we will search
* @return {@code true} if {@code obj} was found; {@code false} if a match could not be
found.
*/
@Override
public boolean contains(Object obj) {
// Only scan through _size, since those are the only "real" entries for the
// multiset.
for (int i = 0; i < _size; i++ ) {
// When obj is null, we need to use ==
if ((obj == null) && (_store[i] == null)) {
return true;
}
// Otherwise, we use .equals() to find a match
else if ((obj != null) && obj.equals(_store[i])) {
return true;
}
// No else clause, since the match could be at a higher index!
}
// Checked all VALID indices, so the result must be:
return false;
}
@Override
public int size() {
return _size;
}
/**
* Remove the element found at the given index. This method also acts to maintain the class
invariants.
* Precondition: {@code i} is a valid index within {@code _store}.
*
* @param i Index of the element to remove from the multibag.
*/
private void removeAtIndex(int i) {
// We do not need to check i, since we made that a precondition (assumption)
// for this method.
// Use the last element in the backing store to fill the hole made by the removal.
_store[i] = _store[_size - 1];
// Set that last value to null (this is not necessary, but can help optimize performance)
_store[_size - 1] = null;
// Finally, decrease the value of size
_size -= 1;
}
/**
* Removes a single instance of the given object, if one can be found in the multibag. The
method returns {@code true}
* if a match was found (and removed) and {@code false} if no match was found. Normally,
this uses
* {@link Object#equals} to check if there is a match, but uses {@code ==} when {@code
obj} is {@code null}.
*
* @param obj Object (or null) which we want to remove
* @return {@code true} if {@code obj} was found and an instance removed; {@code false} if
a match could not be found.
*/
@Override
public boolean remove(Object obj) {
for (int i = 0; i < _size; i++ ) {
if ((obj == null) && (_store[i] == null)) {
removeAtIndex(i);
return true;
} else if ((obj != null) && obj.equals(_store[i])) {
removeAtIndex(i);
return true;
}
// No else clause, since a match may still exist at a higher index!
}
// Checked all VALID indices, so the result must be:
return false;
}
@Override
public boolean isEmpty() {
return _size == 0;
}
@Override
public Iterator iterator() {
return new MultiSetIterator();
}
/**
* Instances of this class are used as the iterators for a multiset. We must keep this as an inner-
class, because the
* multiset definition does not include any methods to access its elements.
*
* @author Carl Alphonce
* @author Matthew Hertz
*/
private class MultiSetIterator implements Iterator {
/**
* The index of the _store entry which will be returned by the next call to next()
*/
private int _cursor;
/**
* In keeping with the fail-fast convention, the iterator is only valid while _modCount
remains on this version.
*/
private long _collectionVersion;
/**
* Create a new instance of this class that will go through the (valid) entries in the store.
*/
public MultiSetIterator() {
_cursor = 0;
_collectionVersion = _modCount;
}
public boolean hasNext() {
return _cursor < _size;
}
public E next() {
if(hasNext())
return _store[_cursor++];
else
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/*
* The remaining methods are part of the Collection interface, but are beyond what is necessary
for CSE 116.
* Students who want a complete Multiset implementation should investigate Google's
"Guava" library.
*/
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean containsAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
}

More Related Content

Similar to this file has a complete array-based MultiSet, but not the code need.pdf

Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Ian0J2Bondo
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
alanfhall8953
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
Zidny Nafan
 
using the code below write the public V add(K key, V value); that ad.pdf
using the code below write the public V add(K key, V value); that ad.pdfusing the code below write the public V add(K key, V value); that ad.pdf
using the code below write the public V add(K key, V value); that ad.pdf
amirthagiftsmadurai
 
public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
annaimobiles
 
Write a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdfWrite a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdf
eyebolloptics
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdfA generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
ADITIHERBAL
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdfA generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
adhityafashion
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
seoagam1
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptx
MrMudassir
 
Enumerable
EnumerableEnumerable
Enumerable
mussawir20
 
Maze
MazeMaze
Maze
yito24
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
Create a Dynamic Array container with this user interface Ge.pdf
Create a Dynamic Array container with this user interface  Ge.pdfCreate a Dynamic Array container with this user interface  Ge.pdf
Create a Dynamic Array container with this user interface Ge.pdf
sktambifortune
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
fashiongallery1
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdf
adinathassociates
 

Similar to this file has a complete array-based MultiSet, but not the code need.pdf (20)

Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
using the code below write the public V add(K key, V value); that ad.pdf
using the code below write the public V add(K key, V value); that ad.pdfusing the code below write the public V add(K key, V value); that ad.pdf
using the code below write the public V add(K key, V value); that ad.pdf
 
public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
 
Write a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdfWrite a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdf
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdfA generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
 
A generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdfA generic queue is a general queue storage that can store an.pdf
A generic queue is a general queue storage that can store an.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptx
 
Enumerable
EnumerableEnumerable
Enumerable
 
Maze
MazeMaze
Maze
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Create a Dynamic Array container with this user interface Ge.pdf
Create a Dynamic Array container with this user interface  Ge.pdfCreate a Dynamic Array container with this user interface  Ge.pdf
Create a Dynamic Array container with this user interface Ge.pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Exception
ExceptionException
Exception
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdf
 

More from flashfashioncasualwe

How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdfHow does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
flashfashioncasualwe
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
flashfashioncasualwe
 
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdfFocus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
flashfashioncasualwe
 
Decision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdfDecision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdf
flashfashioncasualwe
 
Explain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdfExplain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdf
flashfashioncasualwe
 
During a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdfDuring a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdf
flashfashioncasualwe
 
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdfExplain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
flashfashioncasualwe
 
Describe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdfDescribe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdf
flashfashioncasualwe
 
Explain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdfExplain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdf
flashfashioncasualwe
 
Develop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdfDevelop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdf
flashfashioncasualwe
 
Describe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdfDescribe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdf
flashfashioncasualwe
 
All answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdfAll answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdf
flashfashioncasualwe
 
C programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdfC programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdf
flashfashioncasualwe
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
flashfashioncasualwe
 
Write an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdfWrite an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdf
flashfashioncasualwe
 
which of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdfwhich of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdf
flashfashioncasualwe
 
2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf
flashfashioncasualwe
 
10. Benefits and costs of International Trade Search for a newspap.pdf
10. Benefits and costs of International Trade  Search for a newspap.pdf10. Benefits and costs of International Trade  Search for a newspap.pdf
10. Benefits and costs of International Trade Search for a newspap.pdf
flashfashioncasualwe
 
Why does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdfWhy does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdf
flashfashioncasualwe
 
Use the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdfUse the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdf
flashfashioncasualwe
 

More from flashfashioncasualwe (20)

How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdfHow does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
 
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdfFocus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
 
Decision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdfDecision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdf
 
Explain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdfExplain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdf
 
During a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdfDuring a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdf
 
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdfExplain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
 
Describe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdfDescribe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdf
 
Explain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdfExplain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdf
 
Develop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdfDevelop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdf
 
Describe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdfDescribe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdf
 
All answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdfAll answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdf
 
C programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdfC programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdf
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
 
Write an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdfWrite an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdf
 
which of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdfwhich of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdf
 
2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf
 
10. Benefits and costs of International Trade Search for a newspap.pdf
10. Benefits and costs of International Trade  Search for a newspap.pdf10. Benefits and costs of International Trade  Search for a newspap.pdf
10. Benefits and costs of International Trade Search for a newspap.pdf
 
Why does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdfWhy does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdf
 
Use the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdfUse the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdf
 

Recently uploaded

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 

Recently uploaded (20)

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 

this file has a complete array-based MultiSet, but not the code need.pdf

  • 1. this file has a complete array-based MultiSet, but not the code needed to support its Iterator. Complete the Iterator's next() and hasNext() methods. Solution Hi, I have implemented required methods. Please let me know in case of any issue. import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; public class ArrayMultiSet implements Collection { /** Array in which the elements in this multiset are stored. */ private E[] _store; /** * Array indices below this amount contain the active elements in this collection. */ private int _size; /** * Modification counter used to preserve the fail-fast nature of its iterators. */ private long _modCount; /** * Create a new empty multiset. */ public ArrayMultiSet() { _modCount = 0; clear(); } /** * Remove all of the elements within the instance and invalidate any current iterators. */ @SuppressWarnings("unchecked") @Override public void clear() { _store = (E[]) (new Object[16]);
  • 2. _size = 0; // maintains the class invariant } /** * Update the multiset so that it includes all of the elements from before the call AND the given element. * * @param e Item to be added to this collection. */ @SuppressWarnings("unchecked") @Override public boolean add(E e) { // Check if we do not have enough space in the underlying array to store the // new element if (_size == _store.length) { // We do not have space, so create a new larger space (doubling the size // is the most time efficient) E[] newStore = (E[]) new Object[_store.length * 2]; // Copy all of the references into the new array for (int i = 0; i < _store.length; i++ ) { newStore[i] = _store[i]; } _store = newStore; // An easier, more efficient way of coding this (but less useful for // teaching) would instead be: // _store = Arrays.copyOf(_store, _store.length * 2); } // Add the element to the store _store[_size] = e; // Finally, we can increase _size, since this change will no longer violate // any class invariants. _size += 1; return true; } /** * Return true if at least one element in the multiset is equal to the given object. When {@code
  • 3. obj} is null, it must * use the {@code ==} operator to perform these checks, but when {@code obj} is not null, the {@link Object#equals} * method is used. * * @param obj Object (or null) for which we will search * @return {@code true} if {@code obj} was found; {@code false} if a match could not be found. */ @Override public boolean contains(Object obj) { // Only scan through _size, since those are the only "real" entries for the // multiset. for (int i = 0; i < _size; i++ ) { // When obj is null, we need to use == if ((obj == null) && (_store[i] == null)) { return true; } // Otherwise, we use .equals() to find a match else if ((obj != null) && obj.equals(_store[i])) { return true; } // No else clause, since the match could be at a higher index! } // Checked all VALID indices, so the result must be: return false; } @Override public int size() { return _size; } /** * Remove the element found at the given index. This method also acts to maintain the class invariants. * Precondition: {@code i} is a valid index within {@code _store}.
  • 4. * * @param i Index of the element to remove from the multibag. */ private void removeAtIndex(int i) { // We do not need to check i, since we made that a precondition (assumption) // for this method. // Use the last element in the backing store to fill the hole made by the removal. _store[i] = _store[_size - 1]; // Set that last value to null (this is not necessary, but can help optimize performance) _store[_size - 1] = null; // Finally, decrease the value of size _size -= 1; } /** * Removes a single instance of the given object, if one can be found in the multibag. The method returns {@code true} * if a match was found (and removed) and {@code false} if no match was found. Normally, this uses * {@link Object#equals} to check if there is a match, but uses {@code ==} when {@code obj} is {@code null}. * * @param obj Object (or null) which we want to remove * @return {@code true} if {@code obj} was found and an instance removed; {@code false} if a match could not be found. */ @Override public boolean remove(Object obj) { for (int i = 0; i < _size; i++ ) { if ((obj == null) && (_store[i] == null)) { removeAtIndex(i); return true; } else if ((obj != null) && obj.equals(_store[i])) { removeAtIndex(i); return true; } // No else clause, since a match may still exist at a higher index!
  • 5. } // Checked all VALID indices, so the result must be: return false; } @Override public boolean isEmpty() { return _size == 0; } @Override public Iterator iterator() { return new MultiSetIterator(); } /** * Instances of this class are used as the iterators for a multiset. We must keep this as an inner- class, because the * multiset definition does not include any methods to access its elements. * * @author Carl Alphonce * @author Matthew Hertz */ private class MultiSetIterator implements Iterator { /** * The index of the _store entry which will be returned by the next call to next() */ private int _cursor; /** * In keeping with the fail-fast convention, the iterator is only valid while _modCount remains on this version. */ private long _collectionVersion; /** * Create a new instance of this class that will go through the (valid) entries in the store. */ public MultiSetIterator() { _cursor = 0; _collectionVersion = _modCount;
  • 6. } public boolean hasNext() { return _cursor < _size; } public E next() { if(hasNext()) return _store[_cursor++]; else throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException(); } } /* * The remaining methods are part of the Collection interface, but are beyond what is necessary for CSE 116. * Students who want a complete Multiset implementation should investigate Google's "Guava" library. */ @Override public Object[] toArray() { // TODO Auto-generated method stub return null; } @Override public T[] toArray(T[] a) { // TODO Auto-generated method stub return null; } @Override public boolean containsAll(Collection c) { // TODO Auto-generated method stub return false; } @Override
  • 7. public boolean addAll(Collection c) { // TODO Auto-generated method stub return false; } @Override public boolean removeAll(Collection c) { // TODO Auto-generated method stub return false; } @Override public boolean retainAll(Collection c) { // TODO Auto-generated method stub return false; } }