Question: [JAVA] I'm having a bit of trouble on this assignment. I need to make an OrderedArrayList class and implement a few methods (shown below) within

[JAVA] I'm having a bit of trouble on this assignment. I need to make an OrderedArrayList class and implement a few methods (shown below) within it. I need to use the OrderedArrayList to read lines from a packet.dat file, add the lines the list, and output the message.

I posted the assignment below, the classes I already have coded, as well as the contents of the packet. Thanks.

[JAVA] I'm having a bit of trouble on this assignment. I need

to make an OrderedArrayList class and implement a few methods (shown below)

package lab6a;

public abstract class ArrayList implements ArrayListADT { /** * The list - generic array of items */ protected T[] data; /** * The number of items in the list */ protected int size; /** * default maximum capacity */ private final int CAPACITY = 100; /** * default constructor - creates a list that can store 100 items; * the size of the list is initialized to 0 */ public ArrayList() { size = 0; data = (T[])new Object[CAPACITY]; }

/** * parameterized constructor - allows the user to specify the maximum capacity of the list. * The list created can store at most capacity items; the size of the list is initialized to 0. * @param capacity indicates the maximum capacity of the list as specified by the user */ public ArrayList(int capacity){ if(capacity true if the list is empty; false otherwise */ public boolean isEmpty() { return size == 0; } /** * full method - determines whether or not the list is full * @return true if the list is full; false otherwise */ public boolean isFull() { return size == CAPACITY; } /** * getSize method - returns the number of items in the list * @return the number of items in the list */ public int getSize() { return size; } /** * getCapacity method - returns the maximum size of the list * @return the maximum size of the list */ public int getCapacity() { return data.length; } /** * clear method - makes the list empty and sets the size to 0 */ public void clear() { size = 0; data = (T[])new Object[data.length]; System.gc(); } /** * toString method - returns the state of the object as a String * @return a String containing the items in the list */ public String toString() { String str = "The list contains " + size + " items. "; if(!isEmpty()) { for(int i=0; i true if item was added, false otherwise */ public abstract boolean add(T insertItem);

/** * Returns the index of the first occurrence of the specified item (key) in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return the location of the first occurrence of the specified item in this list; * if searchItem is not found, -1 is returned */ public abstract int indexOf(T searchItem);

/** * Returns the index of the last occurrence of the specified item (key) in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return the location of the last occurrence of the specified item in this list; * if searchItem is not found, -1 is returned */ public abstract int lastIndexOf(T searchItem); /** * contains method - determines whether or not the searchItem (key)is in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return true if the item is in this list, false otherwise */ public abstract boolean contains(T searchItem); /** * get method - returns the item at the specified location in this list * @param index is the index of the item in this list * @return the item, if it is in this list, otherwise null is returned */ public abstract T get(int index);

/** * Removes the first occurrence of the specified item from this list, if it is present * @param removeItem is a reference to an item whose key-field has been initialized * @return the item, if it is in this list, otherwise null is returned */ public abstract T remove(T removeItem);

/** * Removes the item at the specified location from this list, if it is present * @param index is the index of the item in this list * @return the item, if it is in this list, otherwise null is returned */ public abstract T remove(int index); }

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

package lab6a;

public interface ArrayListADT { /** * add method - adds an item to this list * @param insertItem is a reference to the item to be added * @return true if item was added, false otherwise */ public abstract boolean add(T insertItem);

/** * Returns the index of the first occurrence of the specified item (key) in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return the location of the first occurrence of the specified item in this list; * if searchItem is not found, -1 is returned */ public abstract int indexOf(T searchItem);

/** * Returns the index of the last occurrence of the specified item (key) in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return the location of the last occurrence of the specified item in this list; * if searchItem is not found, -1 is returned */ public abstract int lastIndexOf(T searchItem); /** * contains method - determines whether or not the searchItem (key)is in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return true if the item is in this list, false otherwise */ public abstract boolean contains(T searchItem); /** * get method - returns the item at the specified location in this list * @param index is the index of the item in this list * @return the item, if it is in this list, otherwise null is returned */ public abstract T get(int index);

/** * Removes the first occurrence of the specified item from this list, if it is present * @param removeItem is a reference to an item whose key-field has been initialized * @return the item, if it is in this list, otherwise null is returned */ public abstract T remove(T removeItem);

/** * Removes the item at the specified location from this list, if it is present * @param index is the index of the item in this list * @return the item, if it is in this list, otherwise null is returned */ public abstract T remove(int index); /** * empty method - determines whether or not this list is empty * @return true if this list is empty; false otherwise */ public boolean isEmpty();

/** * full method - determines whether or not this list is full * @return true if this list is full; false otherwise */ public boolean isFull();

/** * getSize method - returns the number of items in this list * @return the number of items in this list */ public int getSize();

/** * getCapacity method - returns the maximum size of this list * @return the maximum size of this list */ public int getCapacity();

/** * clear method - makes this list empty and sets numItems to 0 */ public void clear();

/** * toString method - returns the state of the object as a String * @return a String containing the items in this list */ public String toString(); }

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

package lab6a;

public class UnorderedArrayList extends ArrayList { /** * default constructor - creates a list that can store 100 items; * the size of the list is initialized to 0 */ public UnorderedArrayList() { super(); } /** * parameterized constructor - allows the user to specify the maximum capacity of the list. * The list created can store at most capacity items; the size of the list is initialized to 0. * @param capacity indicates the maximum capacity of the list as specified by the user */ public UnorderedArrayList(int capacity) { super(capacity); } /** * add method - adds an item to this list * @param insertItem is a reference to the item to be added * @return true if item was added, false otherwise */ @Override public boolean add(T insertItem) { if (isFull()) return false; data [size] = insertItem; size++; return true; }

/** * Returns the index of the first occurrence of the specified item (key) in this list * @param searchItem is a reference to an item whose key-field has been initialized * @return the location of the first occurrence of the specified item in this list; * if searchItem is not found, -1 is returned */ @Override public int indexOf(T searchItem) { for (int i = 0; i true if the item is in this list, false if it's not. */ @Override public boolean contains(T searchItem) { for (int i = 0; i

/** * get method - returns the item at the specified location in this list * @param index is the index of the item in this list * @return the item, if it is in this list, otherwise null is returned */ @Override public T get(int index) { if (index >= 0 && index

/** * Removes the first occurrence of the specified item from this list, if it is present * @param removeItem is a reference to an item whose key-field has been initialized * @return the item, if it is in this list, otherwise null is returned */ @Override public T remove(T removeItem) { int i = indexOf(removeItem); if (i == -1) return null; for (int j = i; j size) || (index

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

packet.dat :

1 When a communications site transmits a message through a pac 2 ket-switching network, it does not send the message as a con 4 pieces, called packets. These packets are sent through the n 3 tinuous stream of data. Instead, it divides the message into 6 Packets may be transmitted to the receiving site along diff 5 etwork to a receiving site, which reassembles the message. 7 erent paths. As a result, they are likely to arrive out of s 9 essage correctly, each packet must include the relative pos 8 equence. In order for the receiving site to reassemble the m 10 ition of the packet within the message. 13 in ascending order based on their position numbers. 12 can correctly reassemble the message by placing the packets 11 No matter what order these packets arrive, a receiving site

OrderedArrayList Class When a communications site transmits a message through a packet-switching network, it does not send the message as a continuous stream of data. Instead, it divides the message into pieces, called packets These packets are sent through the network to a receiving site, which reassembles the message Sometimes packets are transmitted to the receiving site along different paths. As a result, they are likely to arrive out of sequence (you have probably seen this occur with long SMS messages.) In order for the receiving site to reassemble the message correctly, each packet must include the relative position of the packet within the message For example, if the message "A SHORT MESSAGE" is broken into packets of five characters long and prefaced with a number denoting the packet's position in the message, the result is the following set of packets. 1 A SHO 2 RT ME 3 SSAGE No matter what order these packets arrive, a receiving site can correctly reassemble the message by placing the packets in ascending order based on their position numbers A. Create a new package called lab6b and copy the filesArrayListADTjava, ArrayList.java UnorderedArrayList.java into it. (From lab6a) B. Write the code for the OrderedArrayList class: Create a new class named OrderdArrayList that extends ArrayList The methods, add(Tinsertltem), indexOf(T searchltem), lastIndexOf(T searchltem), contains(T searchltem), get(int index), remove(T removeltem) and remove(int index) are defined as abstract in the ArrayList class, therefore they must be implemented in the OrderedArrayList class. For the indexOfmethod, write the code for a binary search. C. Change the array-list classes to restrict the generic type, , to extends Comparable D. Create a class for a packet 1. Each packet contains a position number and the rest of the line contains the characters from the message. The class header for the Packet class should be public class Packet implements Comparable and the compareTo method should order the packets by the position number E. In the Lab6bApp, write code to do the following: 1. 2. Create an OrderedArrayList object. Read lines of data, from a text file (packet.dat) representing the packets OrderedArrayList Class When a communications site transmits a message through a packet-switching network, it does not send the message as a continuous stream of data. Instead, it divides the message into pieces, called packets These packets are sent through the network to a receiving site, which reassembles the message Sometimes packets are transmitted to the receiving site along different paths. As a result, they are likely to arrive out of sequence (you have probably seen this occur with long SMS messages.) In order for the receiving site to reassemble the message correctly, each packet must include the relative position of the packet within the message For example, if the message "A SHORT MESSAGE" is broken into packets of five characters long and prefaced with a number denoting the packet's position in the message, the result is the following set of packets. 1 A SHO 2 RT ME 3 SSAGE No matter what order these packets arrive, a receiving site can correctly reassemble the message by placing the packets in ascending order based on their position numbers A. Create a new package called lab6b and copy the filesArrayListADTjava, ArrayList.java UnorderedArrayList.java into it. (From lab6a) B. Write the code for the OrderedArrayList class: Create a new class named OrderdArrayList that extends ArrayList The methods, add(Tinsertltem), indexOf(T searchltem), lastIndexOf(T searchltem), contains(T searchltem), get(int index), remove(T removeltem) and remove(int index) are defined as abstract in the ArrayList class, therefore they must be implemented in the OrderedArrayList class. For the indexOfmethod, write the code for a binary search. C. Change the array-list classes to restrict the generic type, , to extends Comparable D. Create a class for a packet 1. Each packet contains a position number and the rest of the line contains the characters from the message. The class header for the Packet class should be public class Packet implements Comparable and the compareTo method should order the packets by the position number E. In the Lab6bApp, write code to do the following: 1. 2. Create an OrderedArrayList object. Read lines of data, from a text file (packet.dat) representing the packets

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!