Docsity

PersonSet Adapter: Java Implementation of Set Interface for Person Objects - Prof. Paul C., Assignments of Software Engineering

Toggle sidebar
bg1
Adapter Example: PersonSet
1 of 4
/**
* File: PersonSet.java
* Created: 2/26/2008
* Author: P. Grabow
* Modified: 2/27/2008
* By: P. Grabow
*/
package adapter1;
import java.util.*;
public class PersonSet implements SetIntf {
TreeSet theSet;
// Constructors
// **************************************
public PersonSet() {
theSet = new TreeSet();
}
// **************************************
/**
* Inserts Person into set
*
* @param anObject
* to be inserted
* @return boolean
*/
public boolean insert(Object anObject) {
boolean result = false;
if (anObject instanceof Person) {
Person aPerson = (Person) anObject;
if (!aPerson.isNull()) {
result = theSet.add(aPerson);
} else {
result = false;
}
} else {
result = false;
}
return result;
}
// **************************************
/**
* Removes object from set if it is already in set
*
* @param anObject
* to be removed
* @return boolean
*/
public boolean remove(Object anObject) {
// Add element only if element not null
pf3
pf4

Partial preview of the text

Download PersonSet Adapter: Java Implementation of Set Interface for Person Objects - Prof. Paul C. and more Assignments Software Engineering in PDF only on Docsity!

  • File: PersonSet.java
  • Created: 2/26/
  • Author: P. Grabow
  • Modified: 2/27/
  • By: P. Grabow */ package adapter1;

import java.util.*;

public class PersonSet implements SetIntf {

TreeSet theSet;

// Constructors // ************************************** public PersonSet() { theSet = new TreeSet(); }

// ************************************** /**

  • Inserts Person into set
  • @param anObject
  • to be inserted
  • @return boolean */ public boolean insert(Object anObject) { boolean result = false ; if (anObject instanceof Person) { Person aPerson = (Person) anObject;

if (!aPerson.isNull()) { result = theSet.add(aPerson); } else { result = false ; } } else { result = false ; } return result; }

  • Removes object from set if it is already in set
  • @param anObject
  • to be removed
  • @return boolean */ public boolean remove(Object anObject) { // Add element only if element not null

if (anObject instanceof Person) { Person aPerson = (Person) anObject;

// Remove a particular element from set return theSet.remove(aPerson); } else { return false ; } }

  • Is anObject in the set?
  • @param anObject
  • used to answer the question
  • @return boolean */ public boolean isIn(Object anObject) { // Add element only if element not null if (anObject instanceof Person) { Person aPerson = (Person) anObject;

return theSet.contains(aPerson); } else { return false ; } }

  • Is the set empty?
  • @return boolean */ public boolean isEmpty() { return theSet.isEmpty(); }

// ************************************** /**

  • Empties the set */ public void clear() { // Remove all elements of set theSet.clear(); }

// ************************************** /**

  • Returns number of items in set
  • @return int */ public int size() { return theSet.size(); }

Adapter Example: PersonSet 4 of 4

SetIntf

  • File:

SetIntf.java

  • Created:
  • Author:

P. Grabow

  • Modified: 2/26/2008* By:

P. Grabow

package

adapter1;

import

java.util.*;

public

interface

SetIntf {

boolean

insert(Object anObject);

boolean

remove(Object anObject);

boolean

isIn(Object anObject);

boolean

isEmpty();

void

clear();

int

size();

String toString();Iterator iterator();

SetIntf

  • File:

SetIntf2.java

  • Created:
  • Author:

P. Grabow

  • Modified: 2/26/2008* By:

P. Grabow

package adapter1;import java.util.*;public interface SetIntf2 {

boolean insert(T anObject);boolean remove(T anObject);boolean isIn(T anObject);boolean isEmpty();void clear();int size();String toString();Iterator iterator();