Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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

------------------------------

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 = 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 = length) System.err.println("The location of the item to be removed " + "is out of range."); else { for(int i = location; i = 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 = 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

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 // 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. }

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

Step: 3

blur-text-image

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

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions

Question

7. Explain the role of popular culture in stereotyping.

Answered: 1 week ago