import java.io.*; import java.util.*; public class Example3_1 { static BufferedR
ID: 3678656 • Letter: I
Question
import java.io.*;
import java.util.*;
public class Example3_1
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
UnorderedArrayList intList
= new UnorderedArrayList(50); //Line 1
UnorderedArrayList temp =
new UnorderedArrayList(); //Line 2
IntElement num = new IntElement(); //Line 3
int counter; //Line 4
int position; //Line 5
StringTokenizer tokenizer; //Line 6
System.out.println("Line 7: Processing the "
+ "integer list"); //Line 7
System.out.print("Line 8: Enter 8 integers on the "
+ "same line: "); //Line 8
System.out.flush(); //Line 9
tokenizer = new
StringTokenizer(keyboard.readLine()); //Line 10
for(counter = 0; counter < 8; counter++) //Line 11
{
num.setNum(Integer.parseInt(tokenizer.nextToken())); //Line 12
intList.insert(num); //Line 13
}
temp.copyList(intList); //Line 14
System.out.println(); //Line 15
System.out.print("Line 16: The list you "
+ "entered is: "); //Line 16
intList.print(); //Line 17
System.out.println(); //Line 18
System.out.print("Line 19: Enter the num to "
+ "be deleted: "); //Line 19
System.out.flush(); //Line 20
num.setNum(Integer.parseInt(keyboard.readLine())); //Line 21
System.out.println(); //Line 22
intList.remove(num); //Line 23
System.out.println("Line 24: After removing "
+ num
+ " the list is:"); //Line 24
intList.print(); //Line 25
System.out.println(); //Line 26
System.out.print("Line 27: Enter the position of "
+ "the num to be deleted: "); //Line 27
System.out.flush(); //Line 28
position = Integer.parseInt(keyboard.readLine()); //Line 29
System.out.println(); //Line 30
intList.removeAt(position); //Line 31
System.out.println("Line 32: After removing the "
+ "element at position "
+ position
+ ", intList:"); //Line 32
intList.print(); //Line 33
System.out.println(); //Line 34
System.out.print("Line 35: Enter the search "
+ "item: "); //Line 35
System.out.flush(); //Line 36
num.setNum(Integer.parseInt(keyboard.readLine())); //Line 37
System.out.println(); //Line 38
if(intList.seqSearch(num) != -1) //Line 39
System.out.println("Line 40: Item found in "
+ "the list"); //Line 40
else //Line 41
System.out.println("Line 42: Item not found"); //Line 42
System.out.print("List 43: The list temp: "); //Line 43
temp.print(); //Line 44
System.out.println(); //Line 45
}
}
/* OUTPUT
Line 7: Processing the integer list
Line 8: Enter 8 integers on the same line: 23 54 32 78 27 87 45 66
Line 16: The list you entered is: 23 54 32 78 27 87 45 66
Line 19: Enter the num to be deleted: 32
Line 24: After removing 32 the list is:
23 54 78 27 87 45 66
LIne 27: Enter the position of the number to be deleted: 2
Line 32: After removing the element at position 2, intList:
23 54 27 87 45 66
Line 35: Enter the search item: 23
Line 40: Item found in the list
Line 43: The list temp: 23 54 32 78 27 87 45 66
*/
-----------------------------------------------------------------------
public class UnorderedArrayList extends ArrayListClass
{
public UnorderedArrayList(int size)
{
super(size);
}
public UnorderedArrayList()
{
super();
}
//Copy constructor
public UnorderedArrayList(UnorderedArrayList otherList)
{
super(otherList);
}
//Method to determine whether searchItem is in the list.
//Postcondition: If searchItem is found, returns the location
// in the array where the searchItem is found;
// otherwise, returns -1.
public int seqSearch(DataElement searchItem)
{
int loc;
boolean found = false;
for(loc = 0; loc < length; loc++)
if(list[loc].equals(searchItem))
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
} //end seqSearch
//Method to insert insertItem at the end
//of the list. However, first the list is searched to
//see whether the item to be inserted is already in the list.
//Postcondition: list[length] = insertItem and length++
// If insertItem is already in the list or the list
// is full, an appropriate message is output.
public void insert(DataElement insertItem)
{
int loc;
if(length == 0) //list is empty
list[length++] = insertItem.getCopy(); //insert acopy the item
// andincrement the length
else
if(length == maxSize)
System.err.println("Cannot insert in a full list.");
else
{
loc = seqSearch(insertItem);
if(loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem.getCopy();
else
System.err.println("The item to be inserted is already in "
+ "the list. No duplicates are allowed.");
}
} //end insert
//Method to remove an item from the list.
//The parameter removeItem specifies the item to
//be removed.
//Postcondition: If removeItem is found in the list, it is
// removed from the list and length is
// decremented by one.
public void remove(DataElement removeItem)
{
int loc;
if(length == 0)
System.err.println("Cannot delete from an empty list.");
else
{
loc = seqSearch(removeItem);
if(loc != -1)
removeAt(loc);
else
System.out.println("The item to be deleted is "
+ "not in the list.");
}
} //end remove
}
------------------------------
public abstract class ArrayListClass
{
protected int length; //to store the length of the list
protected int maxSize; //to store the maximum size of the list
protected DataElement[] list; //array to hold the list elements
//Default constructor
//Creates an array of size 100
//Postcondition: list points to the array, length = 0,
// and maxSize = 100
public ArrayListClass()
{
maxSize = 100;
length = 0;
list = new DataElement[maxSize];
}
//Constructor with parameter
//Creates an array of size specified by the parameter
//size.
//Postcondition: list points to the array, length = 0,
// and maxSize = size
public ArrayListClass(int size)
{
if(size <= 0)
{
System.err.println("The array size must be positive. "
+ "Creating an array of size 100. ");
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new DataElement[maxSize];
}
//copy constructor
public ArrayListClass(ArrayListClass otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new DataElement[maxSize]; //create the array
for(int j = 0; j < length; j++) //copy otherList
list[j] = otherList.list[j].getCopy();
}//end copy constructor
//Method to determine whether the list is empty.
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
public boolean isEmpty()
{
return (length == 0);
}
//Method to determine whether the list is full.
//Postcondition: Returns true if the list is full;
// otherwise, returns false.
public boolean isFull()
{
return (length == maxSize);
}
//Method to return the number of elements in the list.
//Postcondition: Returns the value of length.
public int listSize()
{
return length;
}
//Method to return the maximum size of the list.
//Postcondition: Returns the value of maxSize.
public int maxListSize()
{
return maxSize;
}
//Method to output the elements of the list.
//Postcondition: Elements of the list are output on the
//standard output device.
public void print()
{
for(int i = 0; i < length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
//Method to determine whether item is the same as the item in
//the list at the position specified by location.
//Postcondition: Returns true if list[location] is
// same as location; otherwise, returns false.
public boolean isItemAtEqual(int location, DataElement item)
{
return(list[location].equals(item));
}
//Method to insert insertItem in the list at the position
//specified by location.
//Postcondition: Starting at location, the elements of the list
// are shifted to make room for the new item,
// list[location] = insertItem;, and
// length++;
// If the list is full or location is out of range,
// an appropriate message is displayed.
public void insertAt(int location, DataElement insertItem)
{
if(location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted "
+ "is out of range");
else
if(length >= maxSize) //list is full
System.err.println("Cannot insert in a full list.");
else
{
for(int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem.getCopy(); //insert the
//item at the specified position
length++; //increment the length
}
} //end insertAt
//Method to inserts insertItem at the end of the list.
//Postcondition: list[length] = insertItem; and length++;
// If the list is full, an appropriate
// message is displayed.
public void insertEnd(DataElement insertItem)
{
if(length >= maxSize) //the list is full
System.err.println("Cannot insert in a full list.");
else
{
list[length] = insertItem.getCopy(); //insert the
//item at the end
length++; //increment the length
}
} //end insertEnd
//Method to remove the item from the list at the position
//specified by location.
//Postcondition: The list element at list[location] is removed
// and length is decremented by 1.
// If location is out of range, an appropriate message
// is printed.
public void removeAt(int location)
{
if(location < 0 || location >= length)
System.err.println("The location of the item to be removed "
+ "is out of range.");
else
{
for(int i = location; i < length - 1; i++)
list[i] = list[i+1];
list[length - 1] = null;
length--;
}
} //end removeAt
//Method to retrieve the element from the list at the
//position specified by location.
//Postcondition: A copy of the element at the position
// specified by location is returned.
// If location is out of range, an
// appropriate message is printed and
// null is returned.
public DataElement retrieveAt(int location)
{
if(location < 0 || location >= length)
{
System.err.println("The location of the item to be "
+ "retrieved is out of range.");
return null;
}
else
return list[location].getCopy();
} //end retrieveAt
//Method to replace the element in the list at
//the position specified by location with repItem.
//Postcondition: list[location] = repItem
// If location is out of range, an appropriate
// message is printed.
public void replaceAt(int location, DataElement repItem)
{
if(location < 0 || location >= length)
System.err.println("The location of the item to be replaced "
+ "is out of range.");
else
list[location].makeCopy(repItem);
} //end replaceAt
//Method to remove all the elements from the list.
//Postcondition: length = 0
public void clearList()
{
for(int i = 0; i < length; i++)
list[i] = null;
length = 0;
System.gc();
} //end clearList
//Method to determine whether searchItem is in the list.
//Postcondition: If searchItem is found, returns the location
// in the array where searchItem is found;
// otherwise, returns -1.
public abstract int seqSearch(DataElement searchItem);
//Method to insert insertItem in the list.
//However, first the list is searched to see whether
//the item to be inserted is already in the list.
//Postcondition: insertItem is inserted and length++
// If insertItem is already in the list or the list
// is full, an appropriate message is output.
public abstract void insert(DataElement insertItem);
//Method to remove an item from the list.
//The parameter removeItem specifies the item to
//be removed.
//Postcondition: If removeItem is found in the list, it is
// removed from the list and length is
// decremented by one.
public abstract void remove(DataElement removeItem);
//Method to make a copy of the other list.
//Postcondition: This list is destroyed and a copy of
// otherList is assigned to this list.
public void copyList(ArrayListClass otherList)
{
if(this != otherList) //avoid self-assignment
{
for(int j = 0; j < length; j++) //destroy this list
list[j] = null;
System.gc();
maxSize = otherList.maxSize;
length = otherList.length;
list = new DataElement[maxSize]; //create the array
for(int j = 0; j < length; j++) //copy otherList
list[j] = otherList.list[j].getCopy();
}
}
}
-----------------------------------------
public class IntElement extends DataElement
{
protected int num;
//default constructor
public IntElement()
{
num = 0;
}
//constructor with a parameter
public IntElement(int x)
{
num = x;
}
//copy constructor
public IntElement(IntElement otherElement)
{
num = otherElement.num;
}
//Method to set the value of the instance variable num.
//Postcondition: num = x;
public void setNum(int x)
{
num = x;
}
//Method to return the value of the instance variable num.
//Postcondition: The value of num is returned.
public int getNum()
{
return num;
}
public boolean equals(DataElement otherElement)
{
IntElement temp = (IntElement) otherElement;
return (num == temp.num);
}
public int compareTo(DataElement otherElement)
{
IntElement temp = (IntElement) otherElement;
return (num - temp.num);
}
public void makeCopy(DataElement otherElement)
{
IntElement temp = (IntElement) otherElement;
num = temp.num;
}
public DataElement getCopy()
{
IntElement temp = new IntElement(num);
return temp;
}
public String toString()
{
return String.valueOf(num);
}
}
-----------------------------------
public abstract class DataElement
{
public abstract boolean equals(DataElement otherElement);
//Method to determine whether two objects contain the
//same data.
//Postcondition: Returns true if this object contains the
// same data as the object otherElement;
// otherwise, it returns false.
public abstract int compareTo(DataElement otherElement);
//Method to compare two objects.
//Postcondition: Returns a value < 0 if this object is
// less than the object otherElement;
// Returns 0 if this object is the same as
// the object otherElement.
// Returns a value > 0 if this object is
// greater than the object otherElement.
public abstract void makeCopy(DataElement otherElement);
//Method to copy otherElement into this object.
//Postcondition: The data of otherElement is copied into
// this object.
public abstract DataElement getCopy();
//Method to return a copy of this object.
//Postcondition: A copy of this object is created and
// a reference of the copy is returned.
}
Explanation / Answer
UnorderedArrayList
public class UnorderedArrayList extends ArrayListClass
{
public UnorderedArrayList(int size)
{
super(size);
}
public UnorderedArrayList()
{
super();
}
//Copy constructor
public UnorderedArrayList(UnorderedArrayList otherList)
{
super(otherList);
}
//Method to determine whether searchItem is in the list.
//Postcondition: If searchItem is found, returns the location
// in the array where the searchItem is found;
// otherwise, returns -1.
public int seqSearch(DataElement searchItem)
{
int loc;
boolean found = false;
for(loc = 0; loc < length; loc++)
if(list[loc].equals(searchItem))
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
} //end seqSearch
//Method to insert insertItem at the end
//of the list. However, first the list is searched to
//see whether the item to be inserted is already in the list.
//Postcondition: list[length] = insertItem and length++
// If insertItem is already in the list or the list
// is full, an appropriate message is output.
public void insert(DataElement insertItem)
{
int loc;
if(length == 0) //list is empty
list[length++] = insertItem.getCopy(); //insert acopy the item
// andincrement the length
else
{ if(length >= maxSize)
expand();
loc = seqSearch(insertItem);
if(loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem.getCopy();
else
System.err.println("The item to be inserted is already in "
+ "the list. No duplicates are allowed.");
}
} //end insert
//Method to remove an item from the list.
//The parameter removeItem specifies the item to
//be removed.
//Postcondition: If removeItem is found in the list, it is
// removed from the list and length is
// decremented by one.
public void remove(DataElement removeItem)
{
int loc;
if(length == 0)
System.err.println("Cannot delete from an empty list.");
else
{
loc = seqSearch(removeItem);
if(loc != -1)
removeAt(loc);
else
System.out.println("The item to be deleted is "
+ "not in the list.");
}
} //end remove
}
ArrayListClass.java
public abstract class ArrayListClass
{
protected int length; //to store the length of the list
protected int maxSize; //to store the maximum size of the list
protected DataElement[] list; //array to hold the list elements
//Default constructor
//Creates an array of size 100
//Postcondition: list points to the array, length = 0,
// and maxSize = 100
public ArrayListClass()
{
maxSize = 100;
length = 0;
list = new DataElement[maxSize];
}
//Constructor with parameter
//Creates an array of size specified by the parameter
//size.
//Postcondition: list points to the array, length = 0,
// and maxSize = size
public ArrayListClass(int size)
{
if(size <= 0)
{
System.err.println("The array size must be positive. "
+ "Creating an array of size 100. ");
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new DataElement[maxSize];
}
//copy constructor
public ArrayListClass(ArrayListClass otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new DataElement[maxSize]; //create the array
for(int j = 0; j < length; j++) //copy otherList
list[j] = otherList.list[j].getCopy();
}//end copy constructor
//Method to determine whether the list is empty.
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
public boolean isEmpty()
{
return (length == 0);
}
//Method to determine whether the list is full.
//Postcondition: Returns true if the list is full;
// otherwise, returns false.
public boolean isFull()
{
return (length == maxSize);
}
//Method to return the number of elements in the list.
//Postcondition: Returns the value of length.
public int listSize()
{
return length;
}
//Method to return the maximum size of the list.
//Postcondition: Returns the value of maxSize.
public int maxListSize()
{
return maxSize;
}
//Method to output the elements of the list.
//Postcondition: Elements of the list are output on the
//standard output device.
public void print()
{
for(int i = 0; i < length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
//Method to determine whether item is the same as the item in
//the list at the position specified by location.
//Postcondition: Returns true if list[location] is
// same as location; otherwise, returns false.
public boolean isItemAtEqual(int location, DataElement item)
{
return(list[location].equals(item));
}
//Method to insert insertItem in the list at the position
//specified by location.
//Postcondition: Starting at location, the elements of the list
// are shifted to make room for the new item,
// list[location] = insertItem;, and
// length++;
// If the list is full or location is out of range,
// an appropriate message is displayed.
public void insertAt(int location, DataElement insertItem)
{
if(location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted "
+ "is out of range");
else
{ if(length >= maxSize) //list is full
expand();
for(int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem.getCopy(); //insert the
//item at the specified position
length++; //increment the length
}
} //end insertAt
//Method to inserts insertItem at the end of the list.
//Postcondition: list[length] = insertItem; and length++;
public void insertEnd(DataElement insertItem)
{
if(length >= maxSize) //the list is full
expand();
list[length] = insertItem.getCopy(); //insert the
//item at the end
length++; //increment the length
} //end insertEnd
//Method to remove the item from the list at the position
//specified by location.
//Postcondition: The list element at list[location] is removed
// and length is decremented by 1.
// If location is out of range, an appropriate message
// is printed.
public void removeAt(int location)
{
if(location < 0 || location >= length)
System.err.println("The location of the item to be removed "
+ "is out of range.");
else
{
for(int i = location; i < length - 1; i++)
list[i] = list[i+1];
list[length - 1] = null;
length--;
}
} //end removeAt
//Method to retrieve the element from the list at the
//position specified by location.
//Postcondition: A copy of the element at the position
// specified by location is returned.
// If location is out of range, an
// appropriate message is printed and
// null is returned.
public DataElement retrieveAt(int location)
{
if(location < 0 || location >= length)
{
System.err.println("The location of the item to be "
+ "retrieved is out of range.");
return null;
}
else
return list[location].getCopy();
} //end retrieveAt
//Method to replace the element in the list at
//the position specified by location with repItem.
//Postcondition: list[location] = repItem
// If location is out of range, an appropriate
// message is printed.
public void replaceAt(int location, DataElement repItem)
{
if(location < 0 || location >= length)
System.err.println("The location of the item to be replaced "
+ "is out of range.");
else
list[location].makeCopy(repItem);
} //end replaceAt
//Method to remove all the elements from the list.
//Postcondition: length = 0
public void clearList()
{
for(int i = 0; i < length; i++)
list[i] = null;
length = 0;
System.gc();
} //end clearList
//Method to determine whether searchItem is in the list.
//Postcondition: If searchItem is found, returns the location
// in the array where searchItem is found;
// otherwise, returns -1.
public abstract int seqSearch(DataElement searchItem);
//Method to insert insertItem in the list.
//However, first the list is searched to see whether
//the item to be inserted is already in the list.
//Postcondition: insertItem is inserted and length++
// If insertItem is already in the list
// an appropriate message is output.
public abstract void insert(DataElement insertItem);
//Method to remove an item from the list.
//The parameter removeItem specifies the item to
//be removed.
//Postcondition: If removeItem is found in the list, it is
// removed from the list and length is
// decremented by one.
public abstract void remove(DataElement removeItem);
//Method to make a copy of the other list.
//Postcondition: This list is destroyed and a copy of
// otherList is assigned to this list.
public void copyList(ArrayListClass otherList)
{
if(this != otherList) //avoid self-assignment
{
for(int j = 0; j < length; j++) //destroy this list
list[j] = null;
System.gc();
maxSize = otherList.maxSize;
length = otherList.length;
list = new DataElement[maxSize]; //create the array
for(int j = 0; j < length; j++) //copy otherList
list[j] = otherList.list[j].getCopy();
}
}
// sort method
public void sort ()
{
// Create temporary DataElement
DataElement temp;
// Outer for loop varying i from 0 to length
for(int i = 0; i < length; i++)
{
// Inner for loop varying j from i+1 to length
for(int j = i + 1; j < length; j++)
{
// compare list[i] to list[j]
if(list[i].compareTo(list[j]) > 0)
{
// swap list[i] with list[j]
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
} }
}
// expand method
public void expand()
{
// Double maxSize
maxSize *= 2;
// Create a temporary DataElement twice as large as it is now
DataElement temp[] = new DataElement[maxSize];
// for loop to copy the existing list to the temp list
for(int i = 0; i < length; i++)
temp[i] = list[i];
// replace list with temp
list = temp;
}
//Method to return the smallest Element in the list.
//Postcondition: Returns the smallest value.
public DataElement min()
{
//set small to first data element value
DataElement small = list[0];
//loop for checking list values
for(int i = 1; i < length; i++)
{
//compare small to list[i] to see which is smaller
if(small.compareTo(list[i]) > 0)
//replace larger value with smaller one
small = list[i];
}
//return smallest value
return small;
}
//Method to return the largest Element in the list.
//Postcondition: Returns the smallest value.
public DataElement max()
{
//set small to first data element value
DataElement large = list[0];
//loop for checking list values
for(int i = 1; i < length; i++)
{
//compare small to list[i] to see which is smaller
if(large.compareTo(list[i]) < 0)
//replace larger value with smaller one
large = list[i];
}
//return smallest value
return large;
}
}
Example3_2.java
//Test Program String Array List
import java.io.*;
import java.util.*;
public class Example3_2
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
UnorderedArrayList stringList =
new UnorderedArrayList(50);
UnorderedArrayList tempList =
new UnorderedArrayList();
StringElement strObject = new StringElement();
int counter;
int position;
StringTokenizer tokenizer;
System.out.println("Processing the "
+ "string list");
System.out.print("Enter 5 strings on "
+ "the same line: ");
System.out.flush();
tokenizer = new
StringTokenizer(keyboard.readLine());
for(counter = 0; counter < 5; counter++)
{
strObject.setString(tokenizer.nextToken());
stringList.insert(strObject);
}
tempList.copyList(stringList);
System.out.println();
System.out.print("Line 16: The list you "
+ "entered is: ");
stringList.print();
System.out.println();
System.out.print("Line 19: Enter the item to "
+ "be deleted: ");
//System.out.flush();
strObject.setString(keyboard.readLine());
System.out.println();
stringList.remove(strObject);
System.out.println("Line 24: After removing "
+ strObject
+ " the list is:");
stringList.print();
System.out.println();
System.out.print("Line 27: Enter the position of "
+ "the string to be deleted: ");
//System.out.flush();
position = Integer.parseInt(keyboard.readLine());
System.out.println();
stringList.removeAt(position);
System.out.println("Line 32: After removing the "
+ "element at position "
+ position
+ ", stringList:");
stringList.print();
System.out.println();
System.out.print("Line 35: Enter the search "
+ "item: ");
//System.out.flush();
strObject.setString(keyboard.readLine());
System.out.println();
if(stringList.seqSearch(strObject) != -1)
System.out.println("Line 40: Item found in "
+ "the list");
else
System.out.println("Line 42: Item not found");
System.out.print("List 43: tempList: ");
tempList.print();
System.out.println();
}
}
sample OUTPUT
Line 7: Processing the string list
Line 8: Enter 5 strings on the same line: Hello Winter Spring Summer Fall
Line 16: The list you entered is: Hello Winter Spring Summer Fall
Line 19: Enter the item to be deleted: Hello
Line 24: After removing Hello the list is :
Winter Spring Summer Fall
Line 27: Entered the position of the string to be deletedL 3
Line 32: After removing the element at position 3, stringList:
Winter Spring Summer
Line 35: Enter the search item: Spring
Line 40: Item found in the list
Line 43: tempList: Hello Winter Spring Summer Fall
StringElement.java
public class StringElement extends DataElement
{
protected String str;
//default constructor
public StringElement()
{
str = null;
}
//constructor with a parameter
public StringElement(String s)
{
str = s;
}
//copy constructor
public StringElement(StringElement otherString)
{
str = otherString.str;
}
//Method to set the value of the instance variable str.
//Postcondition: str = x;
public void setString(String x)
{
str = x;
}
public boolean equals(DataElement otherElement)
{
StringElement temp = (StringElement) otherElement;
return (str.compareTo(temp.str) == 0);
}
public int compareTo(DataElement otherElement)
{
StringElement temp = (StringElement) otherElement;
return (str.compareTo(temp.str));
}
public void makeCopy(DataElement otherElement)
{
StringElement temp = (StringElement) otherElement;
str = new String(temp.str);
}
public DataElement getCopy()
{
StringElement temp = new StringElement(str);
return temp;
}
public String toString()
{
return str;
}
}
DataElement.java
public abstract class DataElement
{
public abstract boolean equals(DataElement otherElement);
//Method to determine whether two objects contain the
//same data.
//Postcondition: Returns true if this object contains the
// same data as the object otherElement;
// otherwise, it returns false.
public abstract int compareTo(DataElement otherElement);
//Method to compare two objects.
//Postcondition: Returns a value < 0 if this object is
// less than the object otherElement;
// Returns 0 if this object is the same as
// the object otherElement.
// Returns a value > 0 if this object is
// greater than the object otherElement.
public abstract void makeCopy(DataElement otherElement);
//Method to copy otherElement into this object.
//Postcondition: The data of otherElement is copied into
// this object.
public abstract DataElement getCopy();
//Method to return a copy of this object.
//Postcondition: A copy of this object is created and
// a reference of the copy is returned.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.