Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete these steps and put all the code in the answers. I have attached screenshots of ArrayBag and LinkedBag below. Please complete the assignment using

Complete these steps and put all the code in the answers. I have attached screenshots of ArrayBag and LinkedBag below. Please complete the assignment using the provided material. Thanks! image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed ================== Code for ArrayBag.java:

import java.util.Arrays;

public class ArrayBag implements Bag { private E[] list; private int numberOfEntries; private static final int DEFAULT_CAPACITY = 25;

public ArrayBag() { this(DEFAULT_CAPACITY); }

@SuppressWarnings("unchecked") public ArrayBag(int initialCapacity) { numberOfEntries = 0; list = (E[]) new Object[initialCapacity]; }

public int getCurrentSize() { return numberOfEntries; }

@Override public boolean isEmpty() { return numberOfEntries == 0; }

@Override public void add(E newEntry) { if (numberOfEntries == list.length) { list = Arrays.copyOf(list, 2 * list.length); } list[numberOfEntries++] = newEntry;

}

@Override public E remove() { E removedEntry = null; if (numberOfEntries > 0) { numberOfEntries--; removedEntry = list[numberOfEntries]; list[numberOfEntries] = null; } return removedEntry; }

@Override public E remove(E anEntry) { int index = getIndexOf(anEntry); E removed = removeEntry(index); return removed ; }

private E removeEntry(int index) { E removed = null; if (!isEmpty() && index >= 0) { removed = list[index]; numberOfEntries--; list[index] = list[numberOfEntries]; list[numberOfEntries] = null; } return removed; }

public int getIndexOf(E anEntry) { int index = -1; for (int i = 0; i

@Override public void clear() { while (!isEmpty()) { remove(); } }

@Override public int getFrequencyOf(E anEntry) { int frequency = 0; for (int i = 0; i

@Override public boolean contains(E anEntry) { return getIndexOf(anEntry) > -1; }

public E[] toArray() { return Arrays.copyOf(list, numberOfEntries); }

@Override public int size() { return this.numberOfEntries; }

@Override public E get(int i) { return this.list[i]; } public String toString(){ String s=""; for(int i=0;i

=================

Code for LinkedBag.java:

/** * this code implements the SinglyLinnkedList and Bag interface to get index * numbers of the array along with methods to remove given indexes * @author isaac.krapp * @version 2/6/23 */

import java.util.Random;

public class LinkedBag implements Bag { private SinglyLinkedList list;

public LinkedBag() { list = new SinglyLinkedList(); }

public LinkedBag(int initialCapacity) { this(); }

public int getCurrentSize() { return list.size(); }

@Override public boolean isEmpty() { return list.isEmpty(); }

@Override public void add(E newEntry) { list.addFirst(newEntry); }

@Override public E remove() { if (isEmpty()) { return null; } else { return list.removeFirst(); } }

@Override public E remove(E anEntry) { return list.remove(anEntry); }

@Override public void clear() { list.clear(); }

@Override public int getFrequencyOf(E anEntry) { int frequency = 0; for (int i = 0; i

@Override public boolean contains(E anEntry) { return list.contains(anEntry); }

public E[] toArray() { return list.toArray(); }

@Override public int size() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public E get(int i) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public String toString(){ E[] e=toArray(); String s=""; for(int i=0;i

=====

Restrictions: You cannot use any predefined Java classes in writing this lab. You CAN import the java.util.Random class. All output in this assignment must use the printf method. System.out.print and System.out.println cannot be used. You must use the System.out.printf method for all output. Create a NetBeans project using the standard naming convention, i.e. Lab04LastFM and save it to a location like the desktop or your flash drive. In the project you will do the following: Copy the following classes from your Lab103 assignment into the source code directory for this assignment: - Bag - ArrayBag - LinkedBag - SinglyLinkedList If your Lab103 assignment is incomplete and/or incorrect you need to, at a minimum, correctly complete the methods needed for this this assignment. In this assignment you are going to try and measure the speed differences between an array-based structure and a linked-based structure. You will use your ArrayBag and LinkedBag for these measurements. You want to be careful that features like the ArrayBag automatically expanding when it needs more space or the ArrayBag shifting items to the left to fill in empty gaps do not impact the measurements. You also do not want to traverse the LinkedBag looking for an item to remove. To avoid these problems, add the following methods to your ArrayBag and/or LinkedBag classes: To the ArrayBag class add: public void addTest( E e ) this method adds to the end of the array. public E removeTest( ) this method removes the last item at the end of the array. Note that by adding and removing from the end of the array no left-shifting of values will be needed. To the LinkedBag class add: public void addTest( E e ) this method adds to the head of the linkedList public E removeTest() this method removes from the head of the linkedList Note by adding and removing from the head of the linkedList it will not be necessary to traverse the list. If you implement the above methods correctly they should all have O(1) performance. Note that these are methods for your ArrayBag and LinkedBag classes. You should not make any changes to the SinglyLinkedList class. Of course, the methods for the LinkedBag class will still need to call the corresponding methods in the SinglyLinkedList class. Write a Client program that does that tests the ArrayBag and LinkedBag as following: - For each structure: - Perform a timing test for each of these data structures. - Each timing test should measure how long it takes to add N Integers to the structure and then remove N integers from the structure. - Time measurements should be made in milliseconds. - A time measurement should include how long it takes to both add all N values to the structure and then remove all N values from the structure. - N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. - Depending on your system you may run out of memory before you reach the maximum value of N. - If you run out of memory, just drop the maximum value of N by a factor of 10 until your program runs successfully. - For each timing test start with a new, empty data structure. - The overload constructor should be called when creating the ArrayBag instance and passed a value of N for that test. This will keep the ArrayBag from having to expand the size of its array during the test. - You must store your test results in a two-dimensional array. - Test results must be displayed in a nicely formatted ASCIl table similar to the examples provided at the end of the assignment. - For the ASCII table: - You must create a method that prints the ASCIl table. - This method should be passed a two-dimensional array that holds the values to be printed. - Each cell is delimited by the vertical pipe symbol 'l' - Values in each cell are padded by 2 blank spaces - Numerical values use the comma thousand separator, e.g. 1,234,567 - Each column is just wide enough to display the widest entry in that column including the cell padding. - There is a separator line between each row of values. - This separator line is also printed at the top and the bottom of the table. - Note that the ' + ' plus symbols in the separator line, align with the vertical pipe symbols in the value rows. - This table can be a static table, i.e. the column widths do not have to automatically adjust to the values being printed. - Future assignments may require that you make the ASCII table dynamic. - An example of the ASCII table for this assignment is given at the end of this assignment. Example of the ASCII table. - Note that the values line up in a tabular format. - You will need to change your font in your windows document to a non-proportional font, e.g. Courier New

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

Students also viewed these Databases questions