Question

// I need help with the following questions. Please use java programming ECLIPSE language to solve...

// I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT.

I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED"

I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE.

import java.util.ArrayList;

public class CustomArrayList {

//instance variables

public int[] data; //data.length gives the capacity

public int nItems; //nItems gives items currently in the arraylist

//DO NOT MODIFY

public CustomArrayList() {

nItems = 0;

data = new int[10];

}

//DO NOT MODIFY

public boolean isEmpty() {

return nItems == 0;

}

//DO NOT MODIFY

public int currentSize() {

return nItems;

}

//DO NOT MODIFY

public int currentCapacity() {

return data.length;

}

//DO NOT MODIFY

public boolean isFull() {

return nItems == data.length;

}

//DO NOT MODIFY

public String toString() {

String result = "[";

for(int i=0; i < nItems; i++) {

result = result + data[i] +", ";

}

if(result.length() > 1) //not empty

result = result.substring(0, result.length()-2);

return result + "]";

}

/**

* grow the array by 5 items

*/

public void grow() {

this.numberList.ensureCapacity(this.numberList.size() + 5);

}

/**

* grow the array by n items. do nothing if n is less than or equal to 0

* @param n

*/

public void grow(int n) {

//to be completed

if(isFull())

grow();

int [] temp = new int[data.length + n];

for(int i=0; i

{

temp[i] = data[i];

}

data = temp;

}

/**

* add the value to the end of list.

* grow the list if required

* @param value

*/

public void add(int value) {

//to be completed using java

}

/**

*

* @param idx

* @return item at index idx if there, null otherwise

*/

public Integer get(int idx) {

return null; //to be completed using java

}

/**

* set the item at index idx to value newValue,

* returning the value previously stored at index idx

* @param idx

* @param newValue

* @return the value that was overwritten

*

* return null if the index is invalid

*/

public Integer set(int idx, int newValue) {

return null; //to be completed using java

}

/**

*

* @param oldValue

* @param newValue

* @return the index at which oldValue first existed

* and is replaced, -1 otherwise

*/

public int replace(int oldValue, int newValue) {

return 0; //to be completed

}

/**

*

* @param oldValue

* @param newValue

* @return a CustomArrayList object containing all indices

* where oldValue occurred and is now replaced by newValue

*/

public CustomArrayList replaceAll(int oldValue, int newValue) {

return null; //to be completed using java

}

/**

* add item value at index idx

* @param idx

* @param value

* @return true if value CAN be added at index idx,

* false otherwise (if idx is invalid).

* FOR EXAMPLE,

* If a list contains 5 items, we can add a new value

* at any index from 0 (before the first item) to

* 5 (after the last item)

*/

public boolean add(int idx, int value) {

return false; //to be completed

}

/**

* remove the value at index idx (if any) and return value removed.

* if idx is invalid, return null

* @param idx

* @return the value removed

* FOR EXAMPLE,

* If a list contains 5 items, we can remove a value

* at any index from 0 (first item) to

* 4 (last item)

*/

public Integer remove(int idx) {

return null; //to be completed using java

}

/**

*

* @param other

* @return true if calling object and parameter objects

* have the same items in the same order

*

* FOR EXAMPLE,

*

* if

* instance variable data of calling object = [10,70,20,90]

* instance variable data of parameter object = [10,70,20,90]

* return true

*

* if

* instance variable data of calling object = [10,70,20,90]

* instance variable data of parameter object = [90,10,20,70]

* return false

*

* if

* instance variable data of calling object = [10,60,20,90]

* instance variable data of parameter object = [90,10,20,70]

* return false

*

* if

* instance variable data of calling object = [10,20,90]

* instance variable data of parameter object = [90,10,20,70]

* return false

* if

* instance variable data of calling object = [10,20,90]

* instance variable data of parameter object = [10,20]

* return false

*/

public boolean identical(CustomArrayList other) {

return false; //to be completed

}

/**

*

* @param other

* @return CustomArrayList object containing all

* items of calling object followed by all items

* of parameter object

* FOR EXAMPLE:

* If thisdata = [10,70,20]

* otherdata = [50,90]

*

* The data instance variable of list returned should

* be [10,70,20,50,90]

*/

public CustomArrayList join(CustomArrayList other) {

return null; //to be completed

}

/**

*

* @param idx1

* @param idx2

* @return CustomArrayList containing items from index

* idx1 to index idx2 (inclusive on both sides), if valid.

* return null if the range is invalid.

* FOR EXAMPLE:

* If

* thisdata = [10,70,20,50,90,30,80]

* idx1 = 2

* idx2 = 5

*

* The data instance variable of list returned should

* be [20,50,90,30]

*/

public CustomArrayList subList(int idx1, int idx2) {

return null; //to be completed

}

/**

*

* @param other

* @return true if calling object and parameter objects

* have the same items (ok to be in different order)

*

* FOR EXAMPLE,

*

* if

* instance variable data of calling object = [10,70,20,90]

* instance variable data of parameter object = [90,10,20,70]

* return true

*

* if

* instance variable data of calling object = [10,60,20,90]

* instance variable data of parameter object = [90,10,20,70]

* return false

*

* if

* instance variable data of calling object = [10,20,90]

* instance variable data of parameter object = [90,10,20,70]

* return false

* if

* instance variable data of calling object = [10,20,90]

* instance variable data of parameter object = [10,20]

* return false

*/

public boolean same(CustomArrayList other) {

return false; //to be completed

}

//sort in ascending order

public void sort() {

//to be completed

}

}

I believe if you run in on the eclipse then you might be able see. The previous questions are completed, PLEASE COPY IT IN THE ECLIPSE AND RUN IT, IT SHOULD RUN PERFECTLY.

NOTHING IS MISSING.

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

DONE WITH ALL THE METHODS

public class CustomArrayList {

// instance variables

public int[] data; // data.length gives the capacity

public int nItems; // nItems gives items currently in the arraylist

// DO NOT MODIFY

public CustomArrayList() {

nItems = 0;

data = new int[10];

}

// DO NOT MODIFY

public boolean isEmpty() {

return nItems == 0;

}

// DO NOT MODIFY

public int currentSize() {

return nItems;

}

// DO NOT MODIFY

public int currentCapacity() {

return data.length;

}

// DO NOT MODIFY

public boolean isFull() {

return nItems == data.length;

}

// DO NOT MODIFY

public String toString() {

String result = "[";

for (int i = 0; i < nItems; i++) {

result = result + data[i] + ", ";

}

if (result.length() > 1) // not empty

result = result.substring(0, result.length() - 2);

return result + "]";

}

/**

*

* grow the array by 5 items

*

*/

public void grow() {

this.grow(this.data.length + 5);

}

/**

*

* grow the array by n items. do nothing if n is less than or equal to 0

*

* @param n

*

*/

public void grow(int n) {

// to be completed

if (isFull()) {

int[] temp = new int[data.length + n];

for (int i = 0; i < data.length; ++i)

{

temp[i] = data[i];

}

data = temp;

}

}

/**

*

* add the value to the end of list.

*

* grow the list if required

*

* @param value

*

*/

public void add(int value) {

// to be completed using java

if (nItems >= data.length) {

grow();

data[nItems++] = value;

return;

}

else

{

data[nItems++] = value;

}

}

/**

*

*

*

* @param idx

*

* @return item at index idx if there, null otherwise

*

*/

public Integer get(int idx) {

if (idx < 0 || idx >= data.length)

return null; // to be completed using java

return data[idx];

}

/**

*

* set the item at index idx to value newValue,

*

* returning the value previously stored at index idx

*

* @param idx

*

* @param newValue

*

* @return the value that was overwritten

*

*

*

* return null if the index is invalid

*

*/

public Integer set(int idx, int newValue) {

Integer temp = null;

if (idx < 0 || idx >= data.length)

return temp;

else

{

temp = data[idx];

data[idx] = newValue;

}

return temp;

}

/**

*

*

*

* @param oldValue

*

* @param newValue

*

* @return the index at which oldValue first existed

*

* and is replaced, -1 otherwise

*

*/

public int replace(int oldValue, int newValue) {

for (int i = 0; i < data.length; ++i) {

if (data[i] == oldValue) {

data[i] = newValue;

return i;

}

}

return -1;

}

/**

*

*

*

* @param oldValue

*

* @param newValue

*

* @return a CustomArrayList object containing all indices

*

* where oldValue occurred and is now replaced by newValue

*

*/

public CustomArrayList replaceAll(int oldValue, int newValue) {

for (int i = 0; i < data.length; ++i) {

if (data[i] == oldValue) {

data[i] = newValue;

}

}

return this;

}

/**

*

* add item value at index idx

*

* @param idx

*

* @param value

*

* @return true if value CAN be added at index idx,

*

* false otherwise (if idx is invalid).

*

* FOR EXAMPLE,

*

* If a list contains 5 items, we can add a new value

*

* at any index from 0 (before the first item) to

*

* 5 (after the last item)

*

*/

public boolean add(int idx, int value) {

if (idx < 0 || idx >= data.length)

return false;

else

{

if (isFull())

grow();

nItems++;

for (int x = nItems - 1; x > idx; x--) {

data[x] = data[x - 1];

}

data[idx] = value;

}

return false; // to be completed

}

/**

*

* remove the value at index idx (if any) and return value removed.

*

* if idx is invalid, return null

*

* @param idx

*

* @return the value removed

*

* FOR EXAMPLE,

*

* If a list contains 5 items, we can remove a value

*

* at any index from 0 (first item) to

*

* 4 (last item)

*

*/

public Integer remove(int idx) {

if (idx >= nItems || idx < 0)

return null;

else {

int e = data[idx];

for (int x = idx; x < this.data.length - 1; x++) {

data[x] = data[x + 1];

}

nItems--;

return e;

}

}

/**

*

*

*

* @param other

*

* @return true if calling object and parameter objects

*

* have the same items in the same order

*

*

*

* FOR EXAMPLE,

*

*

*

* if

*

* instance variable data of calling object = [10,70,20,90]

*

* instance variable data of parameter object = [10,70,20,90]

*

* return true

*

*

*

* if

*

* instance variable data of calling object = [10,70,20,90]

*

* instance variable data of parameter object = [90,10,20,70]

*

* return false

*

*

*

* if

*

* instance variable data of calling object = [10,60,20,90]

*

* instance variable data of parameter object = [90,10,20,70]

*

* return false

*

*

*

* if

*

* instance variable data of calling object = [10,20,90]

*

* instance variable data of parameter object = [90,10,20,70]

*

* return false

*

* if

*

* instance variable data of calling object = [10,20,90]

*

* instance variable data of parameter object = [10,20]

*

* return false

*

*/

public boolean identical(CustomArrayList other) {

if (other == null)

return false;

if (other.data.length != this.data.length)

return false;

for (int i = 0; i < this.data.length; ++i) {

if (this.data[i] != other.data[i])

return false;

}

return true;

}

/**

*

*

*

* @param other

*

* @return CustomArrayList object containing all

*

* items of calling object followed by all items

*

* of parameter object

*

* FOR EXAMPLE:

*

* If thisdata = [10,70,20]

*

* otherdata = [50,90]

*

*

*

* The data instance variable of list returned should

*

* be [10,70,20,50,90]

*

*/

public CustomArrayList join(CustomArrayList other) {

for (int i = 0; i < other.data.length; ++i) {

this.add(other.data[i]);

}

return this;

}

/**

*

*

*

* @param idx1

*

* @param idx2

*

* @return CustomArrayList containing items from index

*

* idx1 to index idx2 (inclusive on both sides), if valid.

*

* return null if the range is invalid.

*

* FOR EXAMPLE:

*

* If

*

* thisdata = [10,70,20,50,90,30,80]

*

* idx1 = 2

*

* idx2 = 5

*

*

*

* The data instance variable of list returned should

*

* be [20,50,90,30]

*

*/

public CustomArrayList subList(int idx1, int idx2) {

if (idx1 < 0 || idx1 >= data.length)

return null;

if (idx2 < 0 || idx2 >= data.length)

return null;

if (idx2 > idx1)

return null;

int ans[] = new int[idx2 - idx1 + 1];

for (int i = idx1; i <= idx2; ++i) {

ans[i] = data[i];

}

this.data = ans;

return this; // to be completed

}

/**

*

*

*

* @param other

*

* @return true if calling object and parameter objects

*

* have the same items (ok to be in different order)

*

*

*

* FOR EXAMPLE,

*

*

*

* if

*

* instance variable data of calling object = [10,70,20,90]

*

* instance variable data of parameter object = [90,10,20,70]

*

* return true

*

*

*

* if

*

* instance variable data of calling object = [10,60,20,90]

*

* instance variable data of parameter object = [90,10,20,70]

*

* return false

*

*

*

* if

*

* instance variable data of calling object = [10,20,90]

*

* instance variable data of parameter object = [90,10,20,70]

*

* return false

*

* if

*

* instance variable data of calling object = [10,20,90]

*

* instance variable data of parameter object = [10,20]

*

* return false

*

*/

public boolean same(CustomArrayList other) {

sort();

other.sort();

return identical(other);

}

// sort in ascending order

public void sort() {

// to be completed

boolean flag;

for (int i = 0; i < nItems; i++) {

flag = false;

for (int j = 0; j < nItems - i - 1; j++) {

if (data[j] > data[j + 1]) {

int temp = data[j];

data[j] = data[j + 1];

data[j + 1] = temp;

flag = true;

}

}

if (!flag)

break;

}

}

}

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

SEE CODE/ COMPILATION SUCCESS

If a list contains 5 items, we can remove a value at any index from 0 (first item) to 4 (last item) 318 319 320 321 322 323 3

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
// I need help with the following questions. Please use java programming ECLIPSE language to solve...
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
  • I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think...

    I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item)       Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY....

    PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY. THE CODE IS IN JAVA PROGRAMMING LANGUAGE. import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; import java.lang.RuntimeException; import java.util.Arrays; public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess {    protected Object[] data; protected int size; public int size() {     return size; }    private void rangeCheck(int index) {     if (index < 0 || index >= size) throw new IndexOutOfBoundsException(""); }    @SuppressWarnings("unchecked") private E...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Hello in C#. I need help understanding and knwoing what i missed in my program. The...

    Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where...

  • This is for a java lab, in class instruction we are not allowed to use "this"....

    This is for a java lab, in class instruction we are not allowed to use "this". if anyone can help, and explain the importance of "this" and as to why we should or should not use it. Thank you in advanced. Intarraybag.java: public class IntArrayBag implements Cloneable { // instance variables private int[ ] data; private int manyItems; // constructor : behavior #1 public IntArrayBag( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new int[INITIAL_CAPACITY]; }...

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
ADVERTISEMENT