Question

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 data(int index) {
return (E) data[index];
}
  
private void grow() {
int newCapacity = data.length*2;
    data = Arrays.copyOf(data, newCapacity);
}
  
public Vector() {
   this(10);
}
  
public Vector(int initialCapacity) {
    data = new Object[initialCapacity];
size = 0;
}
  
public E get(int index) {
rangeCheck(index);
return data(index);
}
  
public E set(int index, E element) {
rangeCheck(index);
E oldValue = data(index);
    data[index] = element;
   return oldValue;
}
  
public boolean add(E element) {
    if (size == data.length) grow();
data[size++] = element;
return true;
}
  
public void add(int index, E element) {

**************************************
       // Add element at index.
}
  
public E remove(int index) {
  
*********************************

// Return the removed element
}
  
   public int indexOf(Object o) {

*****************************************
       // Returns the index of the first occurrence of the specified element
       // in this list, or -1 if this list does not contain the element.
}
  
public static void main(String[] args) {
   Vector<Integer> intlist = new Vector<Integer>();
Vector<String> stringlist = new Vector<String>();
Vector<Vector<Integer>> intveclist = new Vector<Vector<Integer>>();

       for (Integer i = 0; i < 10; i++) {
               intlist.add(i);
       }

       System.out.println(intlist.indexOf(7));
       System.out.println(intlist.indexOf("seven"));
}
}

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

import java.util.AbstractList;
import java.util.Arrays;
import java.util.List;
import java.util.RandomAccess;

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 data(int index) {
return (E) data[index];
}
  
private void grow() {
int newCapacity = data.length*2;
data = Arrays.copyOf(data, newCapacity);
}
  
public Vector() {
this(10);
}
  
public Vector(int initialCapacity) {
data = new Object[initialCapacity];
size = 0;
}
  
public E get(int index) {
rangeCheck(index);
return data(index);
}
  
public E set(int index, E element) {
rangeCheck(index);
E oldValue = data(index);
data[index] = element;
return oldValue;
}
  
public boolean add(E element) {
if (size == data.length) grow();
data[size++] = element;
return true;
}
  
public void add(int index, E element) {
   rangeCheck(index);
   int i=data.length-1;
   if((size == data.length-1))
       grow();
   // shifting element to the right
   for(;i>index;i--){
       data[i]=data[i-1];
   }
       data[index]=element;
// Add element at index.
}
  
public E remove(int index) {
   rangeCheck(index);
   E e = (E) data[index];
   // shifting elements to left
   for(int i=index;i<data.length-1;i++){
       data[i]=data[i+1];
   }
   return e;

// Return the removed element
}
  
public int indexOf(Object o) {

   for(int i=0;i<data.length;i++){
       if(o.equals(data[i]))
           return i;
   }
   return -1;
// Returns the index of the first occurrence of the specified element
// in this list, or -1 if this list does not contain the element.
}
  
public static void main(String[] args) {
Vector<Integer> intlist = new Vector<Integer>();
Vector<String> stringlist = new Vector<String>();
Vector<Vector<Integer>> intveclist = new Vector<Vector<Integer>>();

for (Integer i = 0; i < 10; i++) {
intlist.add(i);
}

System.out.println(intlist.indexOf(7));
System.out.println(intlist.indexOf("seven"));
}
}

Add a comment
Know the answer?
Add Answer to:
PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY....
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
  • 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; /**...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

    Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() {     arr = new String[10];     size = 0; } public int size() {     return size; } public void add(String value) {    ...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

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

  • 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...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • 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...

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