Question

In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without...

In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without the logic. All you are doing in this class is providing the same functionality as UnSortedInts, but using an ArrayList to hold the data. Document appropriately

Thank you,

package arrayalgorithms;

import java.util.ArrayList;

/**
* Title UnSorted Ints stored in an Array List
* Description: Implement all the functionality of the UnSortedInts
* class using an ArrayList
* @author Khalil Tantouri
*/
public class USIntsArrayList {
ArrayList<Integer> ints;
  
}

******Here's the UnSortedInts class, if that helps:

package arrayalgorithms;

/**
* Description: Holds an Array of unsorted ints
* @author Khalil Tantouri
*/
public class UnSortedInts {
private final int DEFAULT_SIZE = 10;
private int[] ints;
private int count;
  
// Constructor
  
public UnSortedInts() {
ints = new int[DEFAULT_SIZE];
count = 0;
}
  
public UnSortedInts(int size) {
ints = new int[size];
count = 0;
}
  
// addData
  
public void addData(int newInt) {
if (count == ints.length - 1) // If Count is at the last location
growData();
ints[count++] = newInt;
}
  
// removeData
// Given a val, it searches the Array for that val. If found
// it returns the val and removes the val from the list
// if not, it returns -1
public int removeData(int target) {
int location = findData(target);
if (location == -1)
return location;
else {
for (int i = location + 1; i < count; i++) {
ints[i-1] = ints[i];
}
count--;
return location;
}
}
  
// findData
// Write a method that searches the array for an int. Returns the location
// of that number. Returns -1 if not found
// Test Question. What kind of search is this?
public int findData(int target) {
for (int i = 0; i < count; i++) {
if (ints[i] == target)
return i;
}
return -1;
}
  
// cloneData
// Create a new array of the same size and copy the data into that array
// return that array
  
public int[] cloneData() {
int[] newInts = new int[count];
for (int i = 0; i < count; i++)
newInts[i] = ints[i];
return newInts;
}
  
// growData
// Write a method that creates a new array 10 ints larger
// then copies all the data to the new array
// THEN... sets the ints reference variable to point at the new array
private void growData() {
int[] newInts = new int[ints.length + 10];
for (int i = 0; i < count; i++) {
newInts[i] = ints[i];
}
ints = newInts; // Point our ints Instance Variable to reference
// the new array
  
}
  
// Sort Data
// How do we sort an Array. Let's use Selection Sort
// This method sorts our internal data and returns a clone of that
// data sorted
  
public int[] sortData() {
int[] clone;
for (int i = 0; i < count-1; i++) {
int smallest = findSmallest(i);
swap(i,smallest);
}
return cloneData();
}
  
public void swap(int val1, int val2) {
int temp = ints[val1];
ints[val1] = ints[val2];
ints[val2] = temp;
}
  
public int findSmallest(int startLoc) {
int smallest = startLoc;
for (int i = startLoc + 1; i < count; i++) {
if (ints[i] < ints[smallest])
smallest = i;
}
return smallest;
}
  
  
  
// toString
public String toString() {
String results = "";
for (int i = 0; i < count; i++) {
results += ints[i] + " ";
}
return results;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* USIntsArrayListInterface.java */
package arrayalgorithms;

import java.util.ArrayList;

public interface USIntsArrayListInterface {
public void addData(int newInt) ;
public int removeData(int target);
public int findData(int target);
public ArrayList cloneData() ;
public void growData();
public ArrayList sortData();
public void swap(int val1, int val2);
public int findSmallest(int startLoc);
  
  
}

/* USIntsArrayList.java */


package arrayalgorithms;

import java.util.ArrayList;

public class USIntsArrayList implements USIntsArrayListInterface{
private ArrayList<Integer> ints;
private final int DEFAULT_SIZE = 10;
private int count;

public USIntsArrayList(){
ints = new ArrayList<>(DEFAULT_SIZE);
count = 0;
}
public USIntsArrayList(int size) {
ints = new ArrayList<>(size);
count = 0;
}
  
@Override
public void addData(int newInt) {
if (count == ints.size() - 1) // If Count is at the last location
growData();
ints.add(count++, newInt);
}

@Override
public int removeData(int target) {
int location = findData(target);
if (location == -1)
return location;
else {
for (int i = location + 1; i < count; i++) {
ints.add(i-1, ints.get(i));
}
count--;
return location;
}
}

@Override
public int findData(int target) {
for (int i = 0; i < count; i++) {
if (ints.get(i) == target)
return i;
}
return -1;
}

@Override
public ArrayList cloneData() {
ArrayList newInts = new ArrayList(count);
for (int i = 0; i < count; i++)
newInts.add(i,ints.get(i));

return newInts;
}

@Override
public void growData() {
ArrayList newInts = new ArrayList(ints.size() + 10);
for (int i = 0; i < count; i++) {
newInts.add(i,ints.get(i));
}
ints = newInts;
}

@Override
public ArrayList sortData() {
int n = ints.size();
  
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (ints.get(j) < ints.get(min_idx))
min_idx = j;
  
// Swap the found minimum element with the first
// element
int temp = ints.get(min_idx);
ints.add(min_idx,ints.get(i));
ints.add(i,temp);

}
return cloneData();
}

@Override
public void swap(int val1, int val2) {
int temp = ints.get(val1);
ints.add(val1, ints.get(val2));
ints.add(val2, temp);

}

@Override
public int findSmallest(int startLoc) {
int smallest = startLoc;
for (int i = startLoc + 1; i < count; i++) {
if (ints.get(i) < ints.get(smallest))
smallest = i;
}
return smallest;
}

@Override
public String toString() {
String results = "";
for (int i = 0; i < count; i++) {
results += ints.get(i) + " ";
}
return results;

}
  
}

/* PLEASE UPVOTE (THANK YOU IN ADVANCE) */

Add a comment
Know the answer?
Add Answer to:
In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,In...

    Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix)    {        LinkedStack stack2 = new LinkedStack();        int val1;        int val2;        int result;        for(int i = 0; i < postFix.length(); i++)        {            char m = postFix.charAt(i);            if(Character.isDigit(m))            {                stack2.push(m);            }            else            {               ...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • I need help with the last method listed in the problem: Implement a class Grid that...

    I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...

  • Please help me finish my code. Before the final pause in the main function, create an...

    Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...

  • 02. Second Assignment – The FacebookUser Class We’re going to make a small change to the...

    02. Second Assignment – The FacebookUser Class We’re going to make a small change to the UserAccount class from the last assignment by adding a new method: public abstract void getPasswordHelp(); This method is abstract because different types of user accounts may provide different types of help, such as providing a password hint that was entered by the user when the account was created or initiating a process to reset the password. Next we will create a subclass of UserAccount...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation...

    Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT